欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线
博主 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神
本章节会给大家带来文本编辑器 Easy Word V1.0框架开发的详解。
QMainWindow 是一个为用户提供主窗口程序的类,包含一个菜单栏 (menu bar)、多个工具栏 (tool bars)、多个描接部件 (dock widgets) 、一个状态栏 ( status bar) 及一个中心部件(central widget) , 是许多应用程序(如文本编辑器、图片编辑器等)的基础。本章将对此进行详细介绍。
菜单是一系列命令的列表。为了实现菜单、工具栏按钮、键盘快捷方式等命令的一致性,Qt 使用动作 (Action) 来表示这些命令。 Qt 的菜单就是由一系列的 QAction 动作对象构成的列表,而菜单栏则是包容菜单的面板,它位于主窗口标题栏的下面。一个主窗口只能有一个菜单栏。
状态栏通常显示 GUI 应用程序的一些状态信息,它位千主窗口的底部。用户可以在状态栏上添加、使用 Qt 窗口部件。一个主窗口只能有一个状态栏。
工具栏是由一系列的类似千按钮的动作排列而成的面板,它通常由一些经常使用的命令(动作)组成。工具栏位千菜单栏的下面、状态栏的上面,可以停靠在主窗口的上、下、左、右四个方向上。一个主窗口可以包含多个工具栏。
错接部件作为一个容器使用,以包容其他窗口部件来实现某些功能 。 例如, Qt 设计器的属性编辑器、对象监视器等都是由锥接部件包容其他的 Qt 窗口部件来实现的。它位于工具栏区的内部,可以作为一个窗口自由地浮动在主窗口上面,也可以像工具栏一样停靠在主窗口的上、下、左、右四个方向上。 一个主窗口可以包含多个描接部件。
中心部件处在描接部件区的内部、主窗口的中心。一个主窗口只能有一个中心部件。
注意:主窗口具有自己的布局管理器,因此在主窗口 QMainWindow 上设置布局管理器或者创建一个父窗口部件作为 QMainWindow 的布局管理器都是不允许的 。但可以在主窗口的中心部件上设置管理器 。
为了控制主窗口工具栏和铀接部件的显隐,在默认情况下,主窗口 QMainWindow 提供了一个上下文菜单 (Context Menu) 。通常,通过在工具栏或铀接部件上单击鼠标右键就可以激活该上下文菜单,也可以通过函数 QMainWindow:: createPopupMenu()激活该菜单。此外,还可以重写 QMainWindow: :createPopupMenu()函数,实现自定义的上下文菜单。
本章通过完成一个文本编辑器应用实例,介绍 QMainWindow 主窗口的创建流程和各种功能的开发。
文本编辑器 Easy Word V1.0 功能需求是文件操作功能、图片处理功能。
(1)文件操作功能:包括新建一个文件,利用标准文件对话框 QFileDialog 类打开一个已存在的文件,利用 QFile 和 QTextStream 读取文件内容,打印文件(分文本打印和图片打印)。通过实例介绍标准打印对话框 QPrintDialog 类的使用方法,以 QPrinter 作为 QPaintDevice 画图工具实现图片打印。
(2)图片处理中的常用功能:包括图片的缩放、旋转、镜像等坐标变换,使用 QMatrix 实现图片的各种坐标变换。
建立项目的框架:
(1) 新建 Qt Widgets Application ,项目名称为 “ImageProcessor”, 基类选择"QMainWindow", 类名命名为 “ImgProcessor”, 取消“创建界面“复选框的选中状态。单击“下一步”按钮,最后单击“完成”按钮,完成该项目工程的建立。
(2) 添加该工程的提供主要显示文本编辑框函数所在的文件,在 “ImageProcessor” 项目名上单击鼠标右键,在弹出的快捷菜单中选择“添加新文件..”选项,在弹出的对话框中选择 “C++Class” 选项,单击 “Choose…”按钮,在弹出的对话框的 “Base class” 下拉列表框中选择基类名 “QWidget”, 在” Class name" 文本框中输入类的名称 “ShowWidget” 。
(3) 单击“下一步”按钮,单击“完成“按钮,添加" showwidget.h’’ 头文件和 "showwidget.cpp"源文件。
工程文件(包含图标)已经上传GitHub,通过git直接拉取即可
git clone https://github.com/dhn111/EasyWord.git
#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H
#include
#include
#include
#include
#include
class ShowWidget : public QWidget
{
Q_OBJECT
public:
explicit ShowWidget(QWidget *parent = nullptr);
QImage img;
QLabel *imageLabel;
QTextEdit *text;
signals:
public slots:
};
#endif // SHOWWIDGET_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "showwidget.h"
#include
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
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; //文件菜单项
QAction *NewFileAction;
QAction *PrintTextAction;
QAction *PrintImageAction;
QAction *exitAction;
QAction *copyAction; //编辑菜单项
QAction *cutAction;
QAction *pasteAction;
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;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//各项菜单栏
fileMenu = new QMenu;
zoomMenu = new QMenu;
rotateMenu = new QMenu;
mirrorMenu = new QMenu;
showWidget = new ShowWidget;
//文件菜单
openFileAction =new QAction;
NewFileAction =new QAction;
PrintTextAction =new QAction;
PrintImageAction =new QAction;
exitAction = new QAction;
//缩放菜单
copyAction =new QAction;
cutAction =new QAction;
pasteAction =new QAction;
aboutAction =new QAction;
zoomInAction =new QAction;
zoomOutAction=new QAction;
//旋转菜单项
rotate90Action =new QAction;
rotate180Action=new QAction;
rotate270Action=new QAction;
//镜像菜单项
mirrorVerticalAction =new QAction;
mirrorHorizontalAction=new QAction;
undoAction =new QAction;
redoAction =new QAction;
//工具栏
fileTool = new QToolBar;
zoomTool = new QToolBar;
rotateTool = new QToolBar;
mirrorTool = new QToolBar;
doToolBar = new QToolBar;
setWindowTitle(tr("Easy Word")); //设置窗体标题
showWidget =new ShowWidget(this); //(a)
setCentralWidget(showWidget);
/*创建动作、菜单、工具栏的函数*/
createActions() ;
createMenus();
createToolBars();
if(img.load(":/src/PKQ.png"))
{
//在 imageLabel 对象中放置图片
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::createActions()
{
//“打开”动作
openFileAction =new QAction(QIcon(":/src/open.png"),tr("打开"),this); //(a)
openFileAction->setShortcut (tr ("Ctrl+O")); //(b)
openFileAction->setStatusTip(tr("打开一个文件 ")); //(c)
//"新建“动作
NewFileAction =new QAction(QIcon(":/src/new.png"),tr("新建"),this);
NewFileAction->setShortcut(tr("Ctrl+N"));
NewFileAction->setStatusTip(tr("新建一个文件"));
// "退出“动作
exitAction =new QAction(tr("退出"),this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("退出程序")) ;
connect (exitAction, SIGNAL (triggered()), this, SLOT (close()));
// "复制”动作
copyAction =new QAction(QIcon(":/src/copy.png"),tr("复制"),this);
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr(" 复制文件")) ;
connect(copyAction,SIGNAL( triggered ()), showWidget->text, SLOT(copy()));
//"剪切“动作
cutAction =new QAction(QIcon(":/src/cut.png"),tr("剪切"),this);
cutAction->setShortcut(tr("Ctrl+X"));
cutAction->setStatusTip(tr("剪切文件")) ;
connect(cutAction,SIGNAL( triggered ()), showWidget->text, SLOT (cut()));
//"粘贴“动作
pasteAction =new QAction(QIcon(":/src/paste.png"),tr("粘贴"),this);
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("粘贴文件")) ;
connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT (paste()));
//"关于“动作
aboutAction =new QAction(tr("关于"),this);
connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt()));
//"打印文本“动作
PrintTextAction =new QAction(QIcon(":/src/printText.png"),tr(" 打印文本"), this);
PrintTextAction->setStatusTip(tr("打印一个文本"));
//"打印图片“动作
PrintImageAction =new QAction(QIcon (":/src/printimage.png" ), tr("打印图片"), this);
PrintImageAction->setStatusTip(tr("打印一幅图片"));
//"放大“动作
zoomInAction =new QAction (QIcon(":/src/zoomin.png"),tr(" 放大 "),this);
zoomInAction->setStatusTip(tr("放大一幅图片"));
//"缩小“动作
zoomOutAction =new QAction(QIcon(":/src/zoomout.png"),tr("缩小 "),this);
zoomOutAction->setStatusTip(tr("缩小一幅图片 "));
//实现图片旋转的动作 (Action)
//旋转 90°
rotate90Action =new QAction (QIcon (":/src/rotate90.png"), tr("旋转 90°") ,this);
rotate90Action->setStatusTip(tr("将一幅图旋转90°"));
//旋转180°
rotate180Action =new QAction (QIcon(":/src/rotate180.png"), tr("旋转 180°"), this);
rotate180Action->setStatusTip(tr("将一幅图旋转180°"));
//旋转270°
rotate270Action =new QAction(QIcon (":/src/rotate270.png"), tr("旋转 270°"), this);
rotate270Action->setStatusTip(tr("将一幅图旋转 270°"));
//实现图片镜像的动作(Action)
//纵向镜像
mirrorVerticalAction =new QAction(QIcon(":/src/mirrorVertical.png"),tr("纵向镜像"), this);
//横向镜像
mirrorHorizontalAction =new QAction(QIcon(":/src/mirrorHorizontal.png"),tr("横向镜像 "),this);
mirrorHorizontalAction->setStatusTip( tr(" 对一幅图做横向镜像")) ;
//实现撤销和重做的动作(ACtion)
//撤销和重做
undoAction =new QAction (QIcon(":/src/undo.png"),"撤销",this);
connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));
redoAction =new QAction(QIcon(":/src/redo.png"),"重做",this);
connect(redoAction, SIGNAL(triggered()),showWidget->text,SLOT(redo()));
}
void MainWindow::createMenus()
{
//文件菜单
fileMenu =menuBar()->addMenu(tr("文件")) ;
fileMenu->addAction(openFileAction);
fileMenu->addAction(NewFileAction);
fileMenu->addAction(PrintTextAction);
fileMenu->addAction(PrintImageAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
//缩放菜单
zoomMenu =menuBar()->addMenu(tr("编辑")) ;
zoomMenu->addAction(copyAction);
zoomMenu->addAction(cutAction);
zoomMenu->addAction(pasteAction);
zoomMenu->addAction(aboutAction);
zoomMenu->addSeparator();
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 MainWindow::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);
}
#include "showwidget.h"
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);
}
窗口构成会在应用程序开发中经常用到的