29.QT主窗口加widget
Posted 喵小喵~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了29.QT主窗口加widget相关的知识,希望对你有一定的参考价值。
运行效果
- widget布局showwidget.h
1 #ifndef SHOWWIDGET_H 2 #define SHOWWIDGET_H 3 4 #include <QWidget> 5 #include <QLabel> 6 #include <QTextEdit> 7 #include <QImage> 8 9 class ShowWidget : public QWidget 10 { 11 Q_OBJECT 12 public: 13 explicit ShowWidget(QWidget *parent = 0); 14 //图片 15 QImage img; 16 //标签 17 QLabel *imageLabel; 18 //编辑框 19 QTextEdit *text; 20 signals: 21 22 public slots: 23 24 }; 25 26 #endif // SHOWWIDGET_H
- showwidget.cpp
1 #include "showwidget.h" 2 #include <QHBoxLayout> 3 ShowWidget::ShowWidget(QWidget *parent) : 4 QWidget(parent) 5 { 6 imageLabel =new QLabel; 7 imageLabel->setScaledContents(true); 8 9 text =new QTextEdit; 10 11 //创建水平布局 12 QHBoxLayout *mainLayout =new QHBoxLayout(this); 13 mainLayout->addWidget(imageLabel); 14 mainLayout->addWidget(text); 15 }
- 主窗口创建(工具栏,菜单项) imgprocessor.h
1 #ifndef IMGPROCESSOR_H 2 #define IMGPROCESSOR_H 3 4 #include <QMainWindow> 5 #include <QImage> 6 #include <QLabel> 7 #include <QMenu> 8 #include <QMenuBar> 9 #include <QAction> 10 #include <QComboBox> 11 #include <QSpinBox> 12 #include <QToolBar> 13 #include <QFontComboBox> 14 #include <QToolButton> 15 #include <QTextCharFormat> 16 #include "showwidget.h" 17 18 class ImgProcessor : public QMainWindow 19 { 20 Q_OBJECT 21 22 public: 23 ImgProcessor(QWidget *parent = 0); 24 ~ImgProcessor(); 25 26 void createActions(); //创建动作 27 void createMenus(); //创建菜单 28 void createToolBars(); //创建工具栏 29 30 void loadFile(QString filename); 31 void mergeFormat(QTextCharFormat); 32 33 private: 34 //创建工具栏动作 35 QMenu *fileMenu; //文件菜单栏 36 QMenu *zoomMenu; //编辑菜单栏 37 QMenu *rotateMenu; //选择菜单栏 38 QMenu *mirrorMenu; //镜像菜单栏 39 40 QImage img; //图片资源 41 QString fileName; //文件名 42 ShowWidget *showWidget; //显示窗口 43 44 QAction *openFileAction; //打开文件 45 QAction *NewFileAction; //新建文件 46 QAction *PrintTextAction; //打印文本 47 QAction *PrintImageAction; //打印图片 48 QAction *exitAction; //退出 49 50 QAction *copyAction; //复制 51 QAction *cutAction; //剪切 52 QAction *pasteAction; //粘贴 53 QAction *aboutAction; //关于 54 QAction *zoomInAction; //放大 55 QAction *zoomOutAction; //缩小 56 57 QAction *rotate90Action; //旋转90度 58 QAction *rotate180Action; //旋转180度 59 QAction *rotate270Action; //旋转270度 60 61 QAction *mirrorVerticalAction; //纵向镜像 62 QAction *mirrorHorizontalAction; //横向镜像 63 64 QAction *undoAction; 65 QAction *redoAction; 66 67 QToolBar *fileTool; //文件工具栏 68 QToolBar *zoomTool; //复制粘贴工具栏 69 QToolBar *rotateTool; //放大缩小工具栏 70 QToolBar *mirrorTool; //旋转工具栏 71 72 QToolBar *doToolBar; //撤回工具栏 73 74 QLabel *fontLabel1; //字体设置项 75 QFontComboBox *fontComboBox; 76 QLabel *fontLabel2; //字号设置项 77 QComboBox *sizeComboBox; 78 QToolButton *boldBtn; //加粗 79 QToolButton *italicBtn; //倾斜 80 QToolButton *underlineBtn; //下划线 81 QToolButton *colorBtn; //颜色 82 83 QToolBar *fontToolBar; //字体工具栏 84 85 QLabel *listLabel; //排序设置项 86 QComboBox *listComboBox; 87 QActionGroup *actGrp; //对齐方式设置 88 QAction *leftAction; //左对齐 89 QAction *rightAction; //右对齐 90 QAction *centerAction; //中间对齐 91 QAction *justifyAction; //两端对齐 92 93 QToolBar *listToolBar; //排序工具栏 94 95 protected slots: 96 //显示文件 97 void ShowNewFile(); 98 //打开文件 99 void ShowOpenFile(); 100 //显示文本 101 void ShowPrintText(); 102 //显示图片 103 void ShowPrintImage(); 104 void ShowZoomIn(); 105 void ShowZoomOut(); 106 //旋转九十度 107 void ShowRotate90(); 108 //旋转180度 109 void ShowRotate180(); 110 //旋转270度 111 void ShowRotate270(); 112 //纵向镜像 113 void ShowMirrorVertical(); 114 //横向镜像 115 void ShowMirrorHorizontal(); 116 //设置字体框 117 void ShowFontComboBox(QString comboStr); 118 //设置大小框 119 void ShowSizeSpinBox(QString spinValue); 120 //设置字体加粗 121 void ShowBoldBtn(); 122 //设置倾斜 123 void ShowItalicBtn(); 124 //设置下划线 125 void ShowUnderlineBtn(); 126 //设置颜色框 127 void ShowColorBtn(); 128 void ShowCurrentFormatChanged(const QTextCharFormat &fmt); 129 130 void ShowList(int); 131 void ShowAlignment(QAction *act); 132 void ShowCursorPositionChanged(); 133 }; 134 135 #endif // IMGPROCESSOR_H
- imgprocessor.cpp
1 #include "imgprocessor.h" 2 #include <QFileDialog> 3 #include <QFile> 4 #include <QTextStream> 5 #include <QtPrintSupport/QPrintDialog> 6 #include <QtPrintSupport/QPrinter> 7 #include <QColorDialog> 8 #include <QColor> 9 #include <QTextList> 10 #include <qpainter.h> 11 12 //构造初始化 13 ImgProcessor::ImgProcessor(QWidget *parent) 14 : QMainWindow(parent) 15 { 16 //设置标题 17 setWindowTitle(tr("Easy Word")); 18 19 //创建显示 20 showWidget =new ShowWidget(this); 21 //设置中心控件为showWidget 22 setCentralWidget(showWidget); 23 24 //在工具栏上嵌入控件 25 //设置字体 26 fontLabel1 =new QLabel(tr("字体:")); 27 fontComboBox =new QFontComboBox; 28 fontComboBox->setFontFilters(QFontComboBox::ScalableFonts); 29 30 //设置字号 31 fontLabel2 =new QLabel(tr("字号:")); 32 sizeComboBox =new QComboBox; 33 QFontDatabase db; 34 foreach(int size,db.standardSizes()) 35 sizeComboBox->addItem(QString::number(size)); 36 37 //设置加粗按钮 38 boldBtn =new QToolButton; 39 //设置图片 40 boldBtn->setIcon(QIcon("bold.png")); 41 //设置可以选中 42 boldBtn->setCheckable(true); 43 44 //设置倾斜 45 italicBtn =new QToolButton; 46 italicBtn->setIcon(QIcon("italic.png")); 47 italicBtn->setCheckable(true); 48 49 //设置下划线 50 underlineBtn =new QToolButton; 51 underlineBtn->setIcon(QIcon("underline.png")); 52 underlineBtn->setCheckable(true); 53 54 //设置颜色 55 colorBtn =new QToolButton; 56 colorBtn->setIcon(QIcon("color.png")); 57 colorBtn->setCheckable(true); 58 59 //排序方式 60 listLabel =new QLabel(tr("排序")); 61 listComboBox =new QComboBox; 62 listComboBox->addItem("Standard"); 63 listComboBox->addItem("QTextListFormat::ListDisc"); 64 listComboBox->addItem("QTextListFormat::ListCircle"); 65 listComboBox->addItem("QTextListFormat::ListSquare"); 66 listComboBox->addItem("QTextListFormat::ListDecimal"); 67 listComboBox->addItem("QTextListFormat::ListLowerAlpha"); 68 listComboBox->addItem("QTextListFormat::ListUpperAlpha"); 69 listComboBox->addItem("QTextListFormat::ListLowerRoman"); 70 listComboBox->addItem("QTextListFormat::ListUpperRoman"); 71 72 //创建Action 73 createActions(); 74 //创建菜单栏 75 createMenus(); 76 //创建工具栏 77 createToolBars(); 78 79 //载入图片 80 if(img.load("image.png")) 81 { 82 showWidget->imageLabel->setPixmap(QPixmap::fromImage(img)); 83 } 84 85 //字体框连接到函数 86 connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(ShowFontComboBox(QString))); 87 //字体大小框连接到函数 88 connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(ShowSizeSpinBox(QString))); 89 //加粗连接到函数 90 connect(boldBtn,SIGNAL(clicked()),this,SLOT(ShowBoldBtn())); 91 //斜体连接到函数 92 connect(italicBtn,SIGNAL(clicked()),this,SLOT(ShowItalicBtn())); 93 //下划线连接到函数 94 connect(underlineBtn,SIGNAL(clicked()),this,SLOT(ShowUnderlineBtn())); 95 //颜色按钮连接到函数 96 connect(colorBtn,SIGNAL(clicked()),this,SLOT(ShowColorBtn())); 97 //文本字体变化连接到函数 98 connect(showWidget->text,SIGNAL(currentCharFormatChanged(QtextCharFormat&)),this,SLOT(ShowCurrentFormatChanged(QTextCharFormat&))); 99 100 //排序方式连接到函数 101 connect(listComboBox,SIGNAL(activated(int)),this,SLOT(ShowList(int))); 102 103 connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool))); 104 connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool))); 105 //文本位置的鼠标变化连接到函数 106 connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(ShowCursorPositionChanged())); 107 } 108 109 ImgProcessor::~ImgProcessor() 110 { 111 112 } 113 114 //创建工具栏的动作 115 void ImgProcessor::createActions() 116 { 117 //"打开"动作 118 openFileAction =new QAction(QIcon("open.png"),tr("打开"),this); 119 //设置快捷键 120 openFileAction->setShortcut(tr("Ctrl+O")); 121 //设置提示 122 openFileAction->setStatusTip(tr("打开一个文件")); 123 //打开选项连接到函数 124 connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile())); 125 126 //"新建"动作 127 NewFileAction =new QAction(QIcon("new.png"),tr("新建"),this); 128 NewFileAction->setShortcut(tr("Ctrl+N")); 129 NewFileAction->setStatusTip(tr("新建一个文件")); 130 connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile())); 131 132 //"退出"动作 133 exitAction =new QAction(tr("退出"),this); 134 exitAction->setShortcut(tr("Ctrl+Q")); 135 exitAction->setStatusTip(tr("退出程序")); 136 connect(exitAction,SIGNAL(triggered()),this,SLOT(close())); 137 138 //"复制"动作 139 copyAction =new QAction(QIcon("copy.png"),tr("复制"),this); 140 copyAction->setShortcut(tr("Ctrl+C")); 141 copyAction->setStatusTip(tr("复制文件")); 142 connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy())); 143 144 //"剪切"动作 145 cutAction =new QAction(QIcon("cut.png"),tr("剪切"),this); 146 cutAction->setShortcut(tr("Ctrl+X")); 147 cutAction->setStatusTip(tr("剪切文件")); 148 connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut())); 149 150 //"粘贴"动作 151 pasteAction =new QAction(QIcon("paste.png"),tr("粘贴"),this); 152 pasteAction->setShortcut(tr("Ctrl+V")); 153 pasteAction->setStatusTip(tr("粘贴文件")); 154 connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste())); 155 156 //"关于"动作 157 aboutAction =new QAction(tr("关于"),this); 158 connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt())); 159 160 //"打印文本"动作 161 PrintTextAction =new QAction(QIcon("printText.png"),tr("打印文本"), this); 162 PrintTextAction->setStatusTip(tr("打印一个文本")); 163 connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText())); 164 165 //"打印图像"动作 166 PrintImageAction =new QAction(QIcon("printImage.png"),tr("打印图像"), this); 167 PrintImageAction->setStatusTip(tr("打印一幅图像")); 168 connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage())); 169 170 //"放大"动作 171 zoomInAction =new QAction(QIcon("zoomin.png"),tr("放大"),this); 172 zoomInAction->setStatusTip(tr("放大一张图片")); 173 connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn())); 174 175 //"缩小"动作 176 zoomOutAction =new QAction(QIcon("zoomout.png"),tr("缩小"),this); 177 zoomOutAction->setStatusTip(tr("缩小一张图片")); 178 connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut())); 179 180 //实现图像旋转的动作(Action) 181 //旋转90° 182 rotate90Action =new QAction(QIcon("rotate90.png"),tr("旋转90°"),this); 183 rotate90Action->setStatusTip(tr("将一幅图旋转90°")); 184 connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90())); 185 186 //旋转180° 187 rotate180Action =new QAction(QIcon("rotate180.png"),tr("旋转180°"), this); 188 rotate180Action->setStatusTip(tr("将一幅图旋转180°")); 189 connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180())); 190 191 //旋转270° 192 rotate270Action =new QAction(QIcon("QT 主窗口 添加滚动条
Qt学习笔记2.窗体Widget && 屏幕坐标 && 布局