Action、Menus、ToolBars主窗体构成的实现
来源:互联网 发布:史玉柱巨人网络股份 编辑:程序博客网 时间:2024/06/06 00:00
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一
个状态栏(status bar)及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器、图片编辑器等。本章将对此进行详细介绍。其
界面布局如图所示
1.动作(Action)的实现
以下是实现基本文件操作的动作(Action)的代码。
以下是实现打印文本和图像、图像缩放、旋转和镜像的动作(Action)的代码。
2.菜单(Menus)的实现
在实现了各个动作之后,需要将它们通过菜单、工具栏或快捷键的方式体现出来,以下是菜单的实现函数createMenus()代码。
3.工具栏(ToolBars)的实现
接下来实现相对应的工具栏createToolBars(),主窗口的工具栏上可以有多个工具条,通常采用一个菜单对应一个工具条的方式,也可根据需要进行工具条的划分。
项目运行效果
showwidget.h
#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H
#include <QWidget>
#include <QImage>
#include <QTextEdit>
#include <QLabel>
class ShowWidget : public QWidget
{
Q_OBJECT
public:
explicit ShowWidget(QWidget *parent = 0);
QImage img;
QLabel *imageLabel;
QTextEdit *text;
};
#endif // SHOWWIDGET_H
showwidget.cpp
#include "showwidget.h"
#include <QHBoxLayout>
/*
* 水平方向添加一个imageLabel,text
* 其中imageLabel设置显示图片自动缩放
*/
ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)
{
imageLabel = new QLabel;
imageLabel->setScaledContents (true);
text = new QTextEdit;
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget (imageLabel);
mainLayout->addWidget (text);
}
imageprocessor.h
#ifndef IMAGEPROCESSOR_H
#define IMAGEPROCESSOR_H
#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
#include "showwidget.h"
class ImageProcessor : public QMainWindow
{
Q_OBJECT
public:
ImageProcessor(QWidget *parent = 0);
~ImageProcessor();
void createActions(); //创建动作
void createMenus(); //创建菜单
void createToolBars(); //创建工具栏
void loadFile(QString filename); //加载文件
void mergeFormat(QTextCharFormat); //合并板式
private:
//各项菜单栏
QMenu *fileMenu; //文件
QMenu *zoomMenu; //编辑
QMenu *rotateMenu; //旋转
QMenu *mirrorMenu; //镜像
QImage img;
QString fileName;
ShowWidget *showWidget;
//文件菜单项
QAction *openFileAction; //打开文件 Ctrl + O
QAction *NewFileAction; //新建文件 Ctrl + N
QAction *PrintTextAction; //打印字体
QAction *PrintImageAction; //打印图像
QAction *exitAction; //退出 Ctrl + Q
//编辑菜单项
QAction *copyAction; //复制 Ctrl + C
QAction *cutAction; //剪切 Ctrl + V
QAction *pasteAction; //粘贴 Ctrl + P
QAction *aboutAction; //关于
QAction *zoomInAction; //放大
QAction *zoomOutAction; //缩小
//旋转菜单
QAction *rotate90Action;
QAction *rotate180Action;
QAction *rotate270Action;
//镜像菜单
QAction *mirrorVerticalAction;
QAction *mirrorHorizontalAction;
//上一步,下一步
QAction *undoAction;
QAction *redoAction;
//工具栏
QToolBar *fileTool;
QToolBar *zoomTool;
QToolBar *rotateTool;
QToolBar *mirrorTool;
QToolBar *doToolBar;
//字体设置项
QLabel *fontLabel1;
QFontComboBox *fontComboBox;
QLabel *fontLabel2;
QComboBox *sizeComboBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underlineBtn;
QToolButton *colorBtn;
//字体工具栏
QToolBar *fontToolBar;
QLabel *listLabel;
QComboBox *listComboBox;
QActionGroup *actGrp;
QAction *leftAction;
QAction *rightAction;
QAction *centerAction;
QAction *justifyAction;
QToolBar *listToolBar;
protected slots:
void ShowNewFile();
void ShowOpenFile();
void ShowPrintText();
void ShowPrintImage();
void ShowZoomIn();
void ShowZoomOut();
void ShowRotate90();
void ShowRotate180();
void ShowRotate270();
void ShowMirrorVertical();
void ShowMirrorHorizontal();
void ShowFontComboBox(QString comboStr);
void ShowSizeSpinBox(QString spinValue);
void ShowBoldBtn();
void ShowItalicBtn();
void ShowUnderlineBtn();
void ShowColorBtn();
void ShowCurrentFormatChanged(const QTextCharFormat &fmt);
void ShowList(int index);
void ShowAlignment(QAction *act);
void ShowCursorPositionChanged();
};
#endif // IMAGEPROCESSOR_H
imageprocessor.cpp
#include "imageprocessor.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>
#include <QTextCharFormat>
#include <QTextList>
#include <QColorDialog>
#include <QColor>
/*
* QFontComboBox的setFontFilters接口可过滤只在下拉框中显示某一类字体,默认情况QFontComboBox::AllFonts列出所有字体
* QFontDatabase实现在字号下拉列表框中填充各种不同的字号数目,QFontDatabase类用于表示当前系统中所有可用格式信息,主要是
* 字体和字号大小,standardSizes()函数返回可用标准字号的列表,并将它们插入到字号下拉列表框中,本实例只列出字号
*/
ImageProcessor::ImageProcessor(QWidget *parent)
: QMainWindow(parent)
{
//设置主窗口大小
this->resize (830, 260);
setWindowTitle (tr("Easy Word"));
showWidget = new ShowWidget(this);
setCentralWidget (showWidget);
//在工具栏上嵌入式控件
//设置字体
fontLabel1 =new QLabel(tr("字体:"));
fontComboBox =new QFontComboBox;
fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
fontLabel2 =new QLabel(tr("字号:"));
sizeComboBox =new QComboBox;
QFontDatabase db;
foreach(int size,db.standardSizes())
sizeComboBox->addItem(QString::number(size));
boldBtn =new QToolButton;
boldBtn->setIcon(QIcon("bold.png"));
boldBtn->setCheckable(true);
italicBtn =new QToolButton;
italicBtn->setIcon(QIcon("italic.png"));
italicBtn->setCheckable(true);
underlineBtn =new QToolButton;
underlineBtn->setIcon(QIcon("underline.png"));
underlineBtn->setCheckable(true);
colorBtn =new QToolButton;
colorBtn->setIcon(QIcon("color.png"));
colorBtn->setCheckable(true);
//排序
listLabel = new QLabel(tr("排序"));
listComboBox = new QComboBox;
listComboBox->addItem("Standard");
listComboBox->addItem("QTextListFormat::ListDisc");
listComboBox->addItem("QTextListFormat::ListCircle");
listComboBox->addItem("QTextListFormat::ListSquare");
listComboBox->addItem("QTextListFormat::ListDecimal");
listComboBox->addItem("QTextListFormat::ListLowerAlpha");
listComboBox->addItem("QTextListFormat::ListUpperAlpha");
listComboBox->addItem("QTextListFormat::ListLowerRoman");
listComboBox->addItem("QTextListFormat::ListUpperRoman");
createActions();
createMenus();
createToolBars();
//在imageLabel对象中放置一个图像
if(img.load ("image.png"))
{
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
connect (fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString)));
connect (sizeComboBox, SIGNAL(activated(QString)), this, SLOT(ShowSizeSpinBox(QString)));
connect (boldBtn, SIGNAL(clicked(bool)), this, SLOT(ShowBoldBtn()));
connect (italicBtn, SIGNAL(clicked(bool)), this, SLOT(ShowItalicBtn()));
connect (underlineBtn, SIGNAL(clicked(bool)), this, SLOT(ShowUnderlineBtn()));
connect (colorBtn, SIGNAL(clicked(bool)), this, SLOT(ShowColorBtn()));
connect (showWidget->text, SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(ShowCurrentFormatChanged(QTextCharFormat)));
connect (listComboBox, SIGNAL(activated(int)), this, SLOT(ShowList(int)));
connect (showWidget->text->document (), SIGNAL(undoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
connect (showWidget->text->document (), SIGNAL(redoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
connect (showWidget->text, SIGNAL(cursorPositionChanged()), this, SLOT(ShowCursorPositionChanged()));
}
ImageProcessor::~ImageProcessor()
{
}
void ImageProcessor::createActions ()
{
//“打开”动作
openFileAction = new QAction(QIcon("open.png"),tr("打开"),this);
openFileAction->setShortcut(tr("Ctrl+O"));
openFileAction->setStatusTip(tr("打开一文件"));
connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));
//“新建”动作
NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this);
NewFileAction->setShortcut (tr("Ctrl+N"));
NewFileAction->setStatusTip (tr("新建一个文件"));
connect (NewFileAction, SIGNAL(triggered(bool)), this, SLOT(ShowNewFile()));
//"打印文本"动作
PrintTextAction = new QAction(QIcon("printText.png"), tr("打印文本"), this);
PrintTextAction->setStatusTip (tr("打印一个文本"));
connect (PrintTextAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintText()));
//“打印图像”动作
PrintImageAction = new QAction(QIcon("printImage.png"), tr("打印图像"), this);
PrintImageAction->setStatusTip (tr("打印一个图像"));
connect (PrintImageAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintImage()));
//"退出"动作
exitAction = new QAction(tr("退出"), this);
exitAction->setShortcut (tr("Ctrl+Q"));
exitAction->setStatusTip (tr("退出程序"));
connect (exitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
//"复制"动作
copyAction = new QAction(QIcon("copy.png"), tr("复制"), this);
copyAction->setShortcut (tr("Ctrl+C"));
copyAction->setStatusTip (tr("复制文件"));
connect (copyAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(copy()));
//"剪切"动作
cutAction = new QAction(QIcon("cut.png"), tr("剪切"), this);
cutAction->setShortcut (tr("Ctrl+X"));
cutAction->setStatusTip (tr("剪切文件"));
connect (cutAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(cut()));
//"粘贴"动作
pasteAction = new QAction(QIcon("paste.png"), tr("粘贴"), this);
pasteAction->setShortcut (tr("Ctrl+V"));
pasteAction->setStatusTip (tr("粘贴文件"));
connect (pasteAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(paste()));
//"关于"动作
aboutAction = new QAction(tr("关于"), this);
connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt()));
//“放大”动作
zoomInAction = new QAction(QIcon("zoomin.png"), tr("放大"), this);
zoomInAction->setStatusTip (tr("放大一张图片"));
connect (zoomInAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomIn()));
//"缩小"动作
zoomOutAction = new QAction(QIcon("zoomout.png"), tr("放大"), this);
zoomOutAction->setStatusTip (tr("缩小一张图片"));
connect (zoomOutAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomOut()));
//实现图像旋转的动作
//旋转90°
rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋转90°"), this);
rotate90Action->setStatusTip (tr("将一幅图旋转90°"));
connect (rotate90Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate90()));
//旋转180°
rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋转180°"), this);
rotate180Action->setStatusTip (tr("将一幅图旋转180°"));
connect (rotate180Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate180()));
//旋转270°
rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270°"), this);
rotate270Action->setStatusTip (tr("将一幅图旋转270°"));
connect (rotate270Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate270()));
//实现图像镜像的动作
//纵向镜像
mirrorVerticalAction = new QAction(tr("纵向镜像"), this);
mirrorVerticalAction->setStatusTip (tr("对一张图坐标纵向镜像"));
connect (mirrorVerticalAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorVertical()));
//横向镜像
mirrorHorizontalAction = new QAction(tr("横向镜像"), this);
mirrorHorizontalAction->setStatusTip (tr("对一张图坐标横向镜像"));
connect (mirrorHorizontalAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorHorizontal()));
//实现撤销和重做的动作
//撤销
undoAction = new QAction(QIcon("undo.png"), tr("撤销"), this);
connect (undoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(undo()));
//重做
redoAction = new QAction(QIcon("redo.png"), tr("重做"), this);
connect (redoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(redo()));
//实现撤销和重做的动作(Action)
actGrp = new QActionGroup(this);
leftAction = new QAction(QIcon("left.png"), "左对齐", actGrp);
leftAction->setCheckable (true);
rightAction = new QAction(QIcon("right.png"), "右对齐", actGrp);
rightAction->setCheckable (true);
centerAction = new QAction(QIcon("center.png"), "右对齐", actGrp);
centerAction->setCheckable (true);
justifyAction = new QAction(QIcon("justify.png"), "右对齐", actGrp);
justifyAction->setCheckable (true);
connect (actGrp, SIGNAL(triggered(QAction*)), this, SLOT(ShowAlignment(QAction*)));
}
void ImageProcessor::createMenus ()
{
//文件菜单
fileMenu =menuBar()->addMenu(tr("文件"));
fileMenu->addAction(openFileAction);
fileMenu->addAction(NewFileAction);
fileMenu->addAction(PrintTextAction);
fileMenu->addAction(PrintImageAction);
fileMenu->addAction(exitAction);
//编辑菜单
zoomMenu = menuBar ()->addMenu (tr("编辑"));
zoomMenu->addAction(copyAction);
zoomMenu->addAction(cutAction);
zoomMenu->addAction(pasteAction);
zoomMenu->addAction(aboutAction);
zoomMenu->addAction(zoomInAction);
zoomMenu->addAction(zoomOutAction);
//旋转菜单
rotateMenu = menuBar ()->addMenu (tr("旋转"));
rotateMenu->addAction(rotate90Action);
rotateMenu->addAction(rotate180Action);
rotateMenu->addAction(rotate270Action);
//镜像菜单
mirrorMenu = menuBar ()->addMenu (tr("镜像"));
mirrorMenu->addAction(mirrorVerticalAction);
mirrorMenu->addAction(mirrorHorizontalAction);
}
void ImageProcessor::createToolBars ()
{
//文件工具条
fileTool = addToolBar ("File");
fileTool->addAction(openFileAction);
fileTool->addAction(NewFileAction);
fileTool->addAction(PrintTextAction);
fileTool->addAction(PrintImageAction);
//编辑工具条
zoomTool = addToolBar ("Edit");
zoomTool->addAction(copyAction);
zoomTool->addAction(cutAction);
zoomTool->addAction(pasteAction);
zoomTool->addSeparator ();
zoomTool->addAction(zoomInAction);
zoomTool->addAction(zoomOutAction);
//旋转工具条
rotateTool = addToolBar ("rotate");
rotateTool->addAction(rotate90Action);
rotateTool->addAction(rotate180Action);
rotateTool->addAction(rotate270Action);
//撤消和重做工具条
doToolBar = addToolBar ("doEdit");
doToolBar->addAction(undoAction);
doToolBar->addAction(redoAction);
//字体工具条
fontToolBar = addToolBar("Font");
fontToolBar->addWidget(fontLabel1);
fontToolBar->addWidget(fontComboBox);
fontToolBar->addWidget(fontLabel2);
fontToolBar->addWidget(sizeComboBox);
fontToolBar->addSeparator(); //添加分隔符
fontToolBar->addWidget(boldBtn);
fontToolBar->addWidget(italicBtn);
fontToolBar->addWidget(underlineBtn);
fontToolBar->addSeparator();
fontToolBar->addWidget(colorBtn);
//排序工具条
listToolBar = addToolBar ("list");
listToolBar->addWidget (listLabel);
listToolBar->addWidget (listComboBox);
listToolBar->addSeparator ();
listToolBar->addActions(actGrp->actions ());
}
/*
* 显示新建文件
*/
void ImageProcessor::ShowNewFile()
{
ImageProcessor *newImageProcessor = new ImageProcessor;
newImageProcessor->show ();
}
/*
* fileName获取打开文件的(父窗口,标题名,默认的目录,过滤文件类型)
*/
void ImageProcessor::ShowOpenFile()
{
fileName = QFileDialog::getOpenFileName (this, "打开");
if(!fileName.isEmpty ())
{
/*判断显示的内容空时,从新加载*/
if(showWidget->text->document ()->isEmpty ())
{
loadFile (fileName);
}
else
{
ImageProcessor *newImageProcessor = new ImageProcessor;
newImageProcessor->show ();
newImageProcessor->loadFile (fileName);
}
}
}
void ImageProcessor::loadFile (QString filename)
{
printf ("file name: %s\n", filename.data ());
QFile file(filename);
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textStream(&file);
while(!textStream.atEnd ())
{
showWidget->text->append (textStream.readLine ());
printf ("read line\n");
}
printf("end\n");
}
}
/*
* QPrintDialog是Qt提供的标准打印对话框
*/
void ImageProcessor::ShowPrintText()
{
QPrinter printer;
QPrintDialog printDialog(&printer, this);
/*判断打印机对话框显示后用户是否单击“打印”按钮*/
if(printDialog.exec ())
{
QTextDocument *doc = showWidget->text->document ();
doc->print (&printer);
}
}
/*
* QPrintDialog是Qt提供的标准打印对话框
*/
void ImageProcessor::ShowPrintImage()
{
QPrinter printer;
QPrintDialog printDialog(&printer, this);
/*判断打印机对话框显示后用户是否单击“打印”按钮*/
if(printDialog.exec ())
{
QPainter painter(&printer); //指定绘图设备为一个QPrinter对象
QRect rect = painter.viewport(); //获取QPainter对象的视图矩形区域
QSize size = img.size ();
/*按照图形的大小重新设置视图矩形的区域*/
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport(rect.x (), rect.y (), rect.width (), rect.height ());
painter.setWindow(img.rect ()); //设置QPainter窗口大小为图像的大小
painter.drawImage(0, 0, img); //打印图像
}
}
/*
* QMatrix类提供了世界坐标系统的二维转换功能setScaledContents用来设置控件的
* scaledContents属性确定是否根据其大小自动调节内容大小,以便使内容充满整个有效
* 区域按照2倍比例对水平和垂直方向进行放大,将当前显示的图形按照该坐标矩阵进行转换
*/
void ImageProcessor::ShowZoomIn()
{
if(img.isNull ())
return;
QMatrix martix;
martix.scale (2, 2);
img = img.transformed (martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
void ImageProcessor::ShowZoomOut()
{
if(img.isNull ())
return;
QMatrix martix;
martix.scale (0.5, 0.5);
img = img.transformed (martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
void ImageProcessor::ShowRotate90()
{
if(img.isNull ())
return;
QMatrix martix;
martix.rotate (90);
img = img.transformed (martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
void ImageProcessor::ShowRotate180()
{
if(img.isNull ())
return;
QMatrix martix;
martix.rotate (180);
img = img.transformed (martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
void ImageProcessor::ShowRotate270()
{
if(img.isNull ())
return;
QMatrix martix;
martix.rotate (270);
img = img.transformed (martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
/* 镜像函数
* mirror(bool horizontal, bool vertical)
*/
void ImageProcessor::ShowMirrorVertical()
{
if(img.isNull ())
return;
img = img.mirrored (false, true);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
void ImageProcessor::ShowMirrorHorizontal()
{
if(img.isNull ())
return;
img = img.mirrored (true, false);
showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
}
/*
* 设置字体的格式
* 选择的字体名称设置给QTextCharFormat对象
* 将新的格式应用到光标选区内的字符
*/
void ImageProcessor::ShowFontComboBox(QString comboStr)
{
QTextCharFormat fmt;
fmt.setFontFamily (comboStr);
mergeFormat(fmt);
}
/*
* 设置选中的区域
* 获得编辑框中的光标
* 若光标没有高亮选区则将光标所在处的词作为选区
* 将参数format所表示的格式应用到光标所在处的字符应用到选中区字符
*/
void ImageProcessor::mergeFormat (QTextCharFormat formt)
{
QTextCursor cursor = showWidget->text->textCursor ();
if(!cursor.hasSelection ())
{
cursor.select (QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat (formt);
showWidget->text->mergeCurrentCharFormat (formt);
}
/*
* 设置字号
* 将参数format所表示的格式应用到光标所在处的字符应用到选中区字符
*/
void ImageProcessor::ShowSizeSpinBox(QString spinValue)
{
QTextCharFormat fmt;
fmt.setFontPointSize (spinValue.toFloat ());
showWidget->text->mergeCurrentCharFormat (fmt);
}
/*
* 设置粗体 QFont::Light(25) QFont::Normal(50) QFont::DemiBold(63) QFont::Bold(75) QFont::Black(87)
* 通常在QFont::Bold(75) 和 QFont::Normal(50)之间交换
*/
void ImageProcessor::ShowBoldBtn()
{
QTextCharFormat fmt;
fmt.setFontWeight (boldBtn->isChecked () ? QFont::Bold : QFont::Normal);
showWidget->text->mergeCurrentCharFormat (fmt);
}
/*
* 设置文字显示斜体
*/
void ImageProcessor::ShowItalicBtn()
{
QTextCharFormat fmt;
fmt.setFontItalic (italicBtn->isChecked ());
showWidget->text->mergeCurrentCharFormat (fmt);
}
/*
* 设置文字加下划线
*/
void ImageProcessor::ShowUnderlineBtn()
{
QTextCharFormat fmt;
fmt.setFontUnderline (underlineBtn->isChecked ());
showWidget->text->mergeCurrentCharFormat (fmt);
}
/*
* 设置文字的颜色
* QColor color = QColorDialog::getColor (Qt::blue, this);使用了标准颜色对话框的方式,
* 当单击触发颜色按钮时,弹出标准颜色对话框选择颜色
* color.isValid()可以判断用户选择颜色是否有效
*/
void ImageProcessor::ShowColorBtn()
{
QColor color = QColorDialog::getColor (Qt::blue, this);
if(color.isValid ())
{
QTextCharFormat fmt;
fmt.setForeground (color);
showWidget->text->mergeCurrentCharFormat (fmt);
}
}
/*
* 设置字符格式
*/
void ImageProcessor::ShowCurrentFormatChanged(const QTextCharFormat &fmt)
{
fontComboBox->setCurrentIndex (fontComboBox->findText (fmt.fontFamily()));
sizeComboBox->setCurrentIndex (sizeComboBox->findText (QString::number (fmt.FontPointSize)));
boldBtn->setChecked (fmt.font ().bold ());
italicBtn->setChecked (fmt.fontItalic ());
underlineBtn->setChecked (fmt.fontUnderline ());
}
/*
* 实现文本排序QTextCursor文本光标
* 首先获取光标的指针
* 设置光标处的风格
*/
void ImageProcessor::ShowList(int index)
{
//获取编辑框的QTextCursor对象指针
QTextCursor cursor = showWidget->text->textCursor ();
if(index)
{
QTextListFormat::Style style = QTextListFormat::ListDisc; //黑色实心圆点
switch(index)
{
case 1:
style = QTextListFormat::ListDisc; break; //黑色实心圆点
case 2:
style = QTextListFormat::ListCircle; break; //黑色空心圆点
case 3:
style = QTextListFormat::ListSquare; break; //黑色实心方框
case 4:
style = QTextListFormat::ListDecimal; break; //序列1.
case 5:
style = QTextListFormat::ListLowerAlpha; break; //序列a.
case 6:
style = QTextListFormat::ListUpperAlpha; break; //序列A.
case 7:
style = QTextListFormat::ListLowerRoman; break; //序列i.
case 8:
style = QTextListFormat::ListUpperRoman; break; //序列ii.
default:
break;
}
//设置缩进值
cursor.beginEditBlock ();
QTextBlockFormat blockFmt = cursor.blockFormat (); //获得段落的缩进值
QTextListFormat listFmt; //定义QTextListFormat的缩进值
if(cursor.currentList ())
{
listFmt = cursor.currentList ()->format ();
}
else
{
listFmt.setIndent (blockFmt.indent () + 1);
blockFmt.setIndent (0);
cursor.setBlockFormat (blockFmt);
}
listFmt.setStyle (style);
cursor.createList (listFmt);
cursor.endEditBlock ();
}
else
{
QTextBlockFormat bfmt;
bfmt.setObjectIndex (-1);
cursor.mergeBlockFormat (bfmt);
}
}
/*
* 设置QTextEdit的当前段落的对齐调整
*/
void ImageProcessor::ShowAlignment(QAction *act)
{
if(act == leftAction)
showWidget->text->setAlignment (Qt::AlignLeft);
if(act == rightAction)
showWidget->text->setAlignment (Qt::AlignRight);
if(act == centerAction)
showWidget->text->setAlignment (Qt::AlignCenter);
if(act == justifyAction)
showWidget->text->setAlignment (Qt::AlignJustify);
}
/*
* 响应文本光标位置处发生改变的信号
*/
void ImageProcessor::ShowCursorPositionChanged()
{
if(showWidget->text->alignment () == Qt::AlignLeft)
leftAction->setChecked (true);
if(showWidget->text->alignment () == Qt::AlignRight)
rightAction->setChecked (true);
if(showWidget->text->alignment () == Qt::AlignCenter)
centerAction->setChecked (true);
if(showWidget->text->alignment () == Qt::AlignJustify)
justifyAction->setChecked (true);
}
main.cpp
#include "imageprocessor.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ImageProcessor w;
w.show();
return a.exec();
}
0 0
- Action、Menus、ToolBars主窗体构成的实现
- Menus and toolbars in PyQt5
- (转)3.2 创建菜单和工具栏(Creating Menus and Toolbars)
- Firefox os Action menus
- 实现Thumbnail Toolbars,对最小化任务栏的图标点击按钮
- ViewPager&ToolBars的使用
- Toolbars
- 3-2 创建菜单和工具条(Creating Menus and Toolbars)
- Menus
- menus
- 实现完整版的管理员主窗体和学生主窗体
- C# WinForm 关闭登陆窗体,打开主窗体的实现.
- C# WinForm 关闭登陆窗体,打开主窗体的实现
- C# WinForm 关闭登陆窗体,打开主窗体的实现.
- JavaScript实现的三个构成部分
- JavaScript的三大实现构成介绍
- ArcEngine的Commands/Menus/Tools
- ArcEngine的Commands/Menus/Tools
- 【C++】primer plus 练习7.3
- C++中虚析构函数的作用
- 第二章 循环结构程序设计
- poj 3748 水题位操作
- JProfiler入门使用教程:Eclipse集成
- Action、Menus、ToolBars主窗体构成的实现
- 【C++】primer plus 练习7.5
- tomcat pipline设计模式
- HDU - 5119 Happy Matt Friends(DP)
- 广播与服务day2笔记
- MySQL数据备份之mysqldump使用
- gitbub的使用
- Eclipse Xml编译错误Referenced file contains errors - spring-beans-4.0.xsd
- IOS代理协议与委托