├── QtCustomTitleBar ├── pic.png ├── titlebar.h └── titlebar.cpp ├── QCheckboxUsedbyQTreeViewandQTableView ├── gif.gif ├── main.cpp ├── mainwindow.h ├── mainwindow.ui ├── using_checkbox_item.h ├── QCheckboxUsedbyQTreeViewandQTableView.pro ├── mainwindow.cpp └── using_checkbox_item.cpp ├── QMdiAreaExample ├── main.cpp ├── mainwindow.h ├── QMdiAreaExample.pro ├── mainwindow.cpp └── mainwindow.ui ├── QTextDocumentExample ├── main.cpp ├── mainwindow.h ├── mainwindow.ui ├── QTextDocumentExample.pro └── mainwindow.cpp ├── QWidgetLifeExample ├── main.cpp ├── mainwindow.h ├── mainwindow.cpp ├── mainwindow.ui └── QWidgetLifeExample.pro ├── .gitignore ├── LICENSE └── README.md /QtCustomTitleBar/pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coologic/QtWidgetsExamples/HEAD/QtCustomTitleBar/pic.png -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coologic/QtWidgetsExamples/HEAD/QCheckboxUsedbyQTreeViewandQTableView/gif.gif -------------------------------------------------------------------------------- /QMdiAreaExample/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /QTextDocumentExample/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /QWidgetLifeExample/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /QTextDocumentExample/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | private: 19 | Ui::MainWindow *ui; 20 | }; 21 | 22 | #endif // MAINWINDOW_H 23 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | private: 19 | Ui::MainWindow *ui; 20 | }; 21 | 22 | #endif // MAINWINDOW_H 23 | -------------------------------------------------------------------------------- /QWidgetLifeExample/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | #include 4 | namespace Ui { 5 | class MainWindow; 6 | } 7 | class MainWindow : public QMainWindow { 8 | Q_OBJECT 9 | public: 10 | explicit MainWindow(QWidget *parent = 0); 11 | ~MainWindow(); 12 | protected: 13 | bool event(QEvent *event) Q_DECL_OVERRIDE; 14 | private: 15 | Ui::MainWindow *ui; 16 | }; 17 | #endif // MAINWINDOW_H 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | moc_*.h 24 | qrc_*.cpp 25 | ui_*.h 26 | Makefile* 27 | *build-* 28 | 29 | # QtCreator 30 | 31 | *.autosave 32 | 33 | # QtCtreator Qml 34 | *.qmlproject.user 35 | *.qmlproject.user.* 36 | 37 | # QtCtreator CMake 38 | CMakeLists.txt.user* 39 | 40 | -------------------------------------------------------------------------------- /QWidgetLifeExample/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | 6 | MainWindow::MainWindow(QWidget *parent) : 7 | QMainWindow(parent), 8 | ui(new Ui::MainWindow) { 9 | qDebug()<<"Befor ui->setupUi(this)"; 10 | ui->setupUi(this); 11 | qDebug()<<"After ui->setupUi(this)"; 12 | 13 | } 14 | 15 | MainWindow::~MainWindow() { 16 | delete ui; 17 | } 18 | 19 | bool MainWindow::event(QEvent *event) { 20 | qDebug()<type(); 21 | QMainWindow::event(event); 22 | } 23 | -------------------------------------------------------------------------------- /QMdiAreaExample/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | private slots: 19 | void on_pushButton_clicked(); 20 | 21 | void on_pushButton_deletecurrent_clicked(); 22 | 23 | void on_pushButton_deleteall_clicked(); 24 | 25 | void on_pushButton_change1_clicked(); 26 | 27 | void on_pushButton_change2_clicked(); 28 | 29 | private: 30 | Ui::MainWindow *ui; 31 | }; 32 | 33 | #endif // MAINWINDOW_H 34 | -------------------------------------------------------------------------------- /QWidgetLifeExample/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | MainWindow 3 | 4 | 5 | 6 | 0 7 | 0 8 | 400 9 | 300 10 | 11 | 12 | 13 | MainWindow 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /QTextDocumentExample/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/using_checkbox_item.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file using_checkbox_item.h 3 | * @brief 本文件包含支持复选框item类声明。 4 | * @version 1.0.0.0 5 | * @date 2017.12.18 6 | * @author Techie亮 7 | */ 8 | #ifndef _H_USINGCHECKBOXITEM_ 9 | #define _H_USINGCHECKBOXITEM_ 10 | #include 11 | #include 12 | /** 13 | * @brief 支持复选框item类 14 | * 支持复选框三态转变-全选对勾、全不选空白、半选黑点 15 | * 子类会自动通知父子节点item,若不符合设计需要可仿照此方式在model中的setDate重现 16 | */ 17 | class UsingCheckboxItem : public QStandardItem { 18 | public: 19 | /** 20 | * @brief 构造函数 21 | * @param item显示内容 22 | */ 23 | explicit UsingCheckboxItem(const QString &text); 24 | /** 25 | * @brief setData重写 26 | * @param value data值 27 | * @param role data类型 28 | */ 29 | virtual void setData(const QVariant &value, int role = Qt::UserRole + 1); 30 | }; 31 | #endif // _H_USINGCHECKBOXITEM_ 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Techie Liang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /QMdiAreaExample/QMdiAreaExample.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-12-20T20:42:54 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QMdiAreaExample 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp 29 | 30 | HEADERS += \ 31 | mainwindow.h 32 | 33 | FORMS += \ 34 | mainwindow.ui 35 | -------------------------------------------------------------------------------- /QWidgetLifeExample/QWidgetLifeExample.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-12-20T10:47:38 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QWidgetLifeExample 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp 29 | 30 | HEADERS += \ 31 | mainwindow.h 32 | 33 | FORMS += \ 34 | mainwindow.ui 35 | -------------------------------------------------------------------------------- /QTextDocumentExample/QTextDocumentExample.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-12-20T11:49:53 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QTextDocumentExample 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp 29 | 30 | HEADERS += \ 31 | mainwindow.h 32 | 33 | FORMS += \ 34 | mainwindow.ui 35 | -------------------------------------------------------------------------------- /QtCustomTitleBar/titlebar.h: -------------------------------------------------------------------------------- 1 | #ifndef TITLEBAR_H 2 | #define TITLEBAR_H 3 | #include 4 | class TitleBarPrivate; 5 | class TitleBar : public QToolBar { 6 | Q_OBJECT 7 | public: 8 | explicit TitleBar(QWidget *parent = Q_NULLPTR); 9 | protected: 10 | /** 11 | * @brief 被双击--标题栏被双击,放大还原 12 | * @param event 事件 13 | */ 14 | virtual void mouseDoubleClickEvent(QMouseEvent *event); 15 | /** 16 | * @brief 鼠标按下--拖拽移动效果 17 | * @param event 事件 18 | */ 19 | virtual void mousePressEvent(QMouseEvent *event); 20 | /** 21 | * @brief 鼠标抬起--拖拽移动效果 22 | * @param event 事件 23 | */ 24 | virtual void mouseReleaseEvent(QMouseEvent *event); 25 | /** 26 | * @brief 鼠标移动--拖拽移动效果 27 | * @param event 事件 28 | */ 29 | virtual void mouseMoveEvent(QMouseEvent *event); 30 | /** 31 | * @brief 事件过滤--修改题目、图标等操作 32 | * @param obj 33 | * @param event 34 | * @return 35 | */ 36 | virtual bool eventFilter(QObject *obj, QEvent *event); 37 | private slots: 38 | /** 39 | * @brief 最大化按钮被点击槽 40 | * 当前已经最大化则还原,未最大化则最大化 41 | * 系统还原和最大化分为两个槽不能直接connect故建立此槽 42 | */ 43 | void MaximizeButtonClicked(); 44 | private: 45 | TitleBarPrivate *m_private; 46 | }; 47 | 48 | #endif // TITLEBAR_H 49 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/QCheckboxUsedbyQTreeViewandQTableView.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-12-20T11:09:27 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QCheckboxUsedbyQTreeViewandQTableView 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | mainwindow.cpp \ 29 | using_checkbox_item.cpp 30 | 31 | HEADERS += \ 32 | mainwindow.h \ 33 | using_checkbox_item.h 34 | 35 | FORMS += \ 36 | mainwindow.ui 37 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include "using_checkbox_item.h" 5 | MainWindow::MainWindow(QWidget *parent) : 6 | QMainWindow(parent), 7 | ui(new Ui::MainWindow) { 8 | ui->setupUi(this); 9 | QStandardItemModel *m = new QStandardItemModel(this); 10 | ui->treeView->setModel(m); 11 | auto i1 = new UsingCheckboxItem("1"); 12 | auto i2 = new UsingCheckboxItem("2"); 13 | m->appendRow(i1); 14 | m->appendRow(i2); 15 | auto i11 = new UsingCheckboxItem("1-1"); 16 | auto i12 = new UsingCheckboxItem("1-2"); 17 | i1->appendRow(i11); 18 | i1->appendRow(i12); 19 | auto i111 = new UsingCheckboxItem("1-1-1"); 20 | auto i112 = new UsingCheckboxItem("1-1-2"); 21 | i11->appendRow(i111); 22 | i11->appendRow(i112); 23 | auto i121 = new UsingCheckboxItem("1-2-1"); 24 | auto i122 = new UsingCheckboxItem("1-2-2"); 25 | i12->appendRow(i121); 26 | i12->appendRow(i122); 27 | 28 | auto i21 = new UsingCheckboxItem("2-1"); 29 | auto i22 = new UsingCheckboxItem("2-2"); 30 | i2->appendRow(i21); 31 | i2->appendRow(i22); 32 | auto i211 = new UsingCheckboxItem("2-1-1"); 33 | auto i212 = new UsingCheckboxItem("2-1-2"); 34 | i21->appendRow(i211); 35 | i21->appendRow(i212); 36 | auto i221 = new UsingCheckboxItem("2-2-1"); 37 | auto i222 = new UsingCheckboxItem("2-2-2"); 38 | i22->appendRow(i221); 39 | i22->appendRow(i222); 40 | } 41 | MainWindow::~MainWindow() 42 | { 43 | delete ui; 44 | } 45 | -------------------------------------------------------------------------------- /QTextDocumentExample/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | #include 6 | MainWindow::MainWindow(QWidget *parent) : 7 | QMainWindow(parent), 8 | ui(new Ui::MainWindow) { 9 | ui->setupUi(this); 10 | QTextDocument* doc = ui->textEdit->document(); 11 | QTextFrame *root_frame = doc->rootFrame(); 12 | QTextFrameFormat root_frame_format = root_frame->frameFormat();//创建框架格式 13 | root_frame_format.setBorderBrush(Qt::darkBlue);//设置边界颜色 14 | root_frame_format.setBorder(5);//设置边界宽度 15 | root_frame->setFrameFormat(root_frame_format); //给框架使用格式 16 | QTextFrameFormat frame_format; 17 | frame_format.setBackground(Qt::darkRed);//设置背景色 18 | frame_format.setMargin(10);//设置边距 19 | frame_format.setPadding(5);//设置填充 20 | frame_format.setBorder(2);//设置边界宽度 21 | frame_format.setBorderStyle( 22 | QTextFrameFormat::BorderStyle_Solid);//设置边框样式 23 | frame_format.setPosition(QTextFrameFormat::FloatRight);//右侧 24 | frame_format.setWidth(QTextLength( 25 | QTextLength::PercentageLength, 40));//宽度设置 26 | QTextCursor cursor = ui->textEdit->textCursor(); 27 | cursor.insertText("A company"); 28 | cursor.insertBlock(); 29 | cursor.insertText("321 City Street"); 30 | cursor.insertBlock(); 31 | cursor.insertFrame(frame_format); 32 | cursor.insertText("Industry Park"); 33 | cursor.insertBlock(); 34 | cursor.insertText("Another country"); 35 | } 36 | 37 | MainWindow::~MainWindow() { 38 | delete ui; 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QtWidgetsExamples 2 | QtWidgets模块相关类库范例 3 | * 范例详解请见个人博客--Blog:[http://techieliang.com](http://techieliang.com/) 4 | 5 | ## QWidgetLifeExample 6 | QWidget创建过程的事件顺序 7 | * blog:[QWidget一生,从创建到销毁事件流](http://techieliang.com/2017/11/319/) 8 | 9 | ## QtCustomTitleBar 10 | Qt自定义标题栏 11 | * Blog:[Qt自定义标题栏](http://techieliang.com/2017/11/326/) 12 | ![pic](https://github.com/TechieL/QtWidgetsExamples/blob/master/QtCustomTitleBar/pic.png) 13 | 使用方法: 14 | 直接在主窗口(QMainWindow)调用: 15 | TitleBar *titlebar = new TitleBar(this); 16 | 如果主窗口不是QMainWindow,将对应注释位置修改即可 17 | 18 | ## QCheckboxUsedbyQTreeViewandQTableView 19 | 在QTreeView和QTableView中使用复选框实现三态变化 20 | * Blog:[QTreeView/QTableView中利用QStandardItem实现复选框三种形态变化](http://techieliang.com/2017/12/729/) 21 | ![pic](https://github.com/TechieL/QtWidgetsExamples/blob/master/QCheckboxUsedbyQTreeViewandQTableView/gif.gif) 22 | 23 | ## QTextDocumentExample 24 | Qt富文本编辑器使用 25 | * Blog:[Qt富文本编辑器QTextDocument](http://techieliang.com/2017/12/726/) 26 | ![pic](https://github.com/TechieL/MyBlogPictureBackup/blob/master/%E5%9B%BE%E7%89%87/%E6%96%87%E7%AB%A0%E5%9B%BE%E7%89%87/Qt%E5%AF%8C%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8QTextDocument/1.png) 27 | 28 | ## QMdiAreaExample 29 | Qt子窗口QMdiArea和QMdiSubWindow的使用 30 | * Blog:[QMdiArea及QMdiSubWindow实现父子窗口及布局方法](http://techieliang.com/2017/12/756/) 31 | ![pic](https://github.com/TechieL/MyBlogPictureBackup/blob/master/%E5%9B%BE%E7%89%87/%E6%96%87%E7%AB%A0%E5%9B%BE%E7%89%87/QMdiArea%E5%8F%8AQMdiSubWindow%E5%AE%9E%E7%8E%B0%E7%88%B6%E5%AD%90%E7%AA%97%E5%8F%A3%E5%8F%8A%E5%B8%83%E5%B1%80%E6%96%B9%E6%B3%95/gif.gif) 32 | 33 | -------------------------------------------------------------------------------- /QMdiAreaExample/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | #include 6 | MainWindow::MainWindow(QWidget *parent) : 7 | QMainWindow(parent), 8 | ui(new Ui::MainWindow) 9 | { 10 | ui->setupUi(this); 11 | ui->mdiArea->setTabsMovable(true); 12 | ui->mdiArea->setTabShape(QTabWidget::Triangular); 13 | //默认不全屏,否则在TabbedView就只显示一个窗口而且没有标题栏 14 | ui->mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation); 15 | } 16 | 17 | MainWindow::~MainWindow() 18 | { 19 | delete ui; 20 | } 21 | 22 | void MainWindow::on_pushButton_clicked() { 23 | QMdiSubWindow *mdi = new QMdiSubWindow; 24 | ui->mdiArea->addSubWindow(mdi); 25 | //关闭之后不会自动释放空间,需要此函数让其在close后自动释放 26 | mdi->setAttribute(Qt::WA_DeleteOnClose); 27 | //add以后也不自动显示,需要主动show 28 | mdi->show(); 29 | } 30 | 31 | void MainWindow::on_pushButton_deletecurrent_clicked() { 32 | ui->mdiArea->closeActiveSubWindow(); 33 | } 34 | 35 | void MainWindow::on_pushButton_deleteall_clicked() { 36 | auto subs = ui->mdiArea->subWindowList(); 37 | qDebug()<<"before delete sub size:"<mdiArea->closeAllSubWindows(); 39 | subs = ui->mdiArea->subWindowList(); 40 | //可以查看在setAttribute是否设置状态下list是否变动 41 | //使用以后此处为0,不使用为新建窗口数且内存不会减少 42 | qDebug()<<"after delete sub size:"<mdiArea->viewMode() == QMdiArea::TabbedView) 47 | ui->mdiArea->setViewMode(QMdiArea::SubWindowView); 48 | else 49 | ui->mdiArea->setViewMode(QMdiArea::TabbedView); 50 | } 51 | 52 | void MainWindow::on_pushButton_change2_clicked() { 53 | delete ui->mdiArea->layout(); 54 | QHBoxLayout *mLayout=new QHBoxLayout(ui->mdiArea); 55 | auto subs = ui->mdiArea->subWindowList(); 56 | for(auto sub : subs) { 57 | sub->hide(); 58 | mLayout->addWidget(sub); 59 | //不隐藏显示就不能生效,update不管用 60 | sub->show(); 61 | } 62 | ui->mdiArea->setLayout(mLayout); 63 | } 64 | -------------------------------------------------------------------------------- /QMdiAreaExample/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 777 10 | 518 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | add 24 | 25 | 26 | 27 | 28 | 29 | 30 | delete current 31 | 32 | 33 | 34 | 35 | 36 | 37 | delete all 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | change1 49 | 50 | 51 | 52 | 53 | 54 | 55 | layout 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /QCheckboxUsedbyQTreeViewandQTableView/using_checkbox_item.cpp: -------------------------------------------------------------------------------- 1 | #include "using_checkbox_item.h" 2 | //构造函数 3 | UsingCheckboxItem::UsingCheckboxItem(const QString &text) 4 | : QStandardItem(text) { 5 | setCheckable(true); 6 | } 7 | //setData重写 8 | void UsingCheckboxItem::setData(const QVariant &value, int role) { 9 | if(role == Qt::CheckStateRole) {//针对复选框变动做操作 10 | Qt::CheckState check_state = (Qt::CheckState)value.toInt(); 11 | QString mtext=text(); 12 | switch (check_state) { 13 | case Qt::Unchecked: {//取消 14 | for(int i = 0, num = rowCount(); i < num; i++) { 15 | child(i)->setData(Qt::Unchecked, Qt::CheckStateRole); 16 | } 17 | //修改内容-必须先修改自己再通知父节点 18 | QStandardItem::setData(value,role); 19 | //通知父节点,我取消了选择,直接告诉父节点半选即可 20 | if(parent())parent()->setData(Qt::PartiallyChecked, role); 21 | } 22 | return;//此事件已完成直接return 23 | case Qt::PartiallyChecked: {//半选 24 | Qt::CheckState current_state = checkState();//当前状态 25 | int checked_num = 0;//被选择的数量 26 | int unchecked_num = 0;//未选择的数量 27 | bool is_partially = false; 28 | Qt::CheckState child_state; 29 | int m_rowCount = rowCount(); 30 | //遍历所有子节点 31 | for(int i = 0; i < m_rowCount; i++) { 32 | child_state = child(i)->checkState(); 33 | //子节点半选,则直接半选 34 | switch (child_state) { 35 | case Qt::PartiallyChecked:is_partially = true;break; 36 | case Qt::Unchecked:unchecked_num++;break; 37 | case Qt::Checked:checked_num++;break; 38 | default:checked_num++;break; 39 | } 40 | } 41 | //根据子节点状态确定当前节点应该设置的状态 42 | Qt::CheckState now_state; 43 | if(is_partially) 44 | now_state = Qt::PartiallyChecked; 45 | else if(checked_num == m_rowCount) 46 | now_state = Qt::Checked; 47 | else if(unchecked_num == m_rowCount) 48 | now_state = Qt::Unchecked; 49 | else 50 | now_state = Qt::PartiallyChecked; 51 | //修改状态并通知父节点 52 | if(current_state != now_state) { 53 | //修改内容-必须先修改自己再通知父节点 54 | QStandardItem::setData(now_state,role); 55 | //通知父节点,我的状态更改,也就是父节点进入半选 56 | if(parent())parent()->setData(Qt::PartiallyChecked, role); 57 | } 58 | } 59 | return;//此事件已完成直接return 60 | case Qt::Checked: {//全选 61 | for(int i = 0, num = rowCount(); i < num; i++) { 62 | child(i)->setData(Qt::Checked, Qt::CheckStateRole); 63 | } 64 | //修改内容-必须先修改自己再通知父节点 65 | QStandardItem::setData(value,role); 66 | //通知父节点,我被选了,也就是父节点进入半选 67 | if(parent()) { 68 | parent()->setData(Qt::PartiallyChecked, role); 69 | } 70 | } 71 | return;//此事件已完成直接return 72 | default://如果出现此情况就是错了,可以加错误处理 73 | break; 74 | } 75 | } 76 | QStandardItem::setData(value,role); 77 | } 78 | -------------------------------------------------------------------------------- /QtCustomTitleBar/titlebar.cpp: -------------------------------------------------------------------------------- 1 | #include "titlebar.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | class TitleBarPrivate { 11 | public: 12 | bool mouse_left_pressing = false;//鼠标左键按下状态记录 13 | QPoint move_start_point;//窗口移动时起始位置记录 14 | QMainWindow *w;//获取主窗口指针,默认为QMainWindow,如果是QWidget请修改 15 | QLabel *icon_label;//窗口图标 16 | QLabel *title_label;//窗口标题 17 | QWidget* toolBar_seat;//标题中部占位 18 | QToolButton *toolButton_mini;//最小化 19 | QToolButton *toolButton_max;//最大化 20 | QToolButton *toolButton_close;//关闭 21 | }; 22 | 23 | TitleBar::TitleBar(QWidget *parent) : QToolBar(parent) { 24 | m_private = new TitleBarPrivate; 25 | m_private->w = (QMainWindow *)window(); 26 | //自定义标题初始化--直接操作主窗口,降低使用复杂性 27 | m_private->w->setWindowFlags(Qt::FramelessWindowHint);//主窗口隐藏标题栏 28 | m_private->w->addToolBar(this);//把标题栏添加到主窗口 29 | m_private->w->installEventFilter(this);//事件过滤器 30 | //设置此标题位置 31 | setMovable(false);//禁止拖动 32 | //左侧显示 33 | m_private->icon_label = new QLabel(this); 34 | m_private->title_label = new QLabel(this); 35 | m_private->icon_label->setFixedSize(m_private->w->iconSize()); 36 | m_private->icon_label->setScaledContents(true); 37 | m_private->icon_label->setPixmap(m_private->w->windowIcon(). 38 | pixmap(m_private->icon_label->size())); 39 | m_private->title_label->setText(m_private->w->windowTitle()); 40 | addWidget(m_private->icon_label); 41 | addWidget(m_private->title_label); 42 | //中部占位 43 | m_private->toolBar_seat = new QWidget; 44 | m_private->toolBar_seat->setSizePolicy(QSizePolicy::Expanding, 45 | QSizePolicy::Expanding);//长宽自动扩展 46 | addWidget(m_private->toolBar_seat); 47 | //右侧显示 48 | m_private->toolButton_mini = new QToolButton(this); 49 | m_private->toolButton_max = new QToolButton(this); 50 | m_private->toolButton_close = new QToolButton(this); 51 | m_private->toolButton_mini->setIcon(style()-> 52 | standardPixmap(QStyle::SP_TitleBarMinButton)); 53 | m_private->toolButton_max->setIcon(style()-> 54 | standardPixmap(QStyle::SP_TitleBarMaxButton)); 55 | m_private->toolButton_close->setIcon(style()-> 56 | standardPixmap(QStyle::SP_TitleBarCloseButton)); 57 | addWidget(m_private->toolButton_mini); 58 | addWidget(m_private->toolButton_max); 59 | addWidget(m_private->toolButton_close); 60 | connect(m_private->toolButton_mini, SIGNAL(clicked(bool)), 61 | m_private->w, SLOT(showMinimized())); 62 | connect(m_private->toolButton_max, SIGNAL(clicked(bool)), 63 | this, SLOT(MaximizeButtonClicked())); 64 | connect(m_private->toolButton_close, SIGNAL(clicked(bool)), 65 | m_private->w, SLOT(close())); 66 | } 67 | 68 | void TitleBar::mouseDoubleClickEvent(QMouseEvent *event) { 69 | if(Qt::LeftButton == event->button()) 70 | MaximizeButtonClicked(); 71 | event->ignore(); 72 | QToolBar::mouseDoubleClickEvent(event); 73 | } 74 | 75 | void TitleBar::mousePressEvent(QMouseEvent *event) { 76 | event->button(); 77 | if(Qt::LeftButton == event->button()) { 78 | m_private->mouse_left_pressing = true; 79 | m_private->move_start_point = event->globalPos(); 80 | } 81 | event->ignore(); 82 | QToolBar::mousePressEvent(event); 83 | } 84 | 85 | void TitleBar::mouseReleaseEvent(QMouseEvent *event) { 86 | if(Qt::LeftButton == event->button()) 87 | m_private->mouse_left_pressing = false; 88 | event->ignore(); 89 | QToolBar::mouseReleaseEvent(event); 90 | } 91 | 92 | void TitleBar::mouseMoveEvent(QMouseEvent *event) { 93 | auto w = window();//获取主窗口指针 94 | if(m_private->mouse_left_pressing) { 95 | QPoint movePoint = event->globalPos() - m_private->move_start_point; 96 | QPoint widgetPos = w->pos(); 97 | m_private->move_start_point = event->globalPos(); 98 | w->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y()); 99 | } 100 | event->ignore(); 101 | QToolBar::mouseMoveEvent(event); 102 | } 103 | 104 | bool TitleBar::eventFilter(QObject *obj, QEvent *event) { 105 | switch (event->type()) {//只做了部分事件处理,其余可另行补充 106 | case QEvent::WindowTitleChange: {//标题修改 107 | m_private->title_label->setText(m_private->w->windowTitle()); 108 | return QToolBar::eventFilter(obj, event); 109 | } 110 | case QEvent::WindowIconChange: {//图标修改 111 | 112 | m_private->icon_label->setFixedSize(m_private->w->iconSize()); 113 | m_private->icon_label->setScaledContents(true); 114 | m_private->icon_label->setPixmap(m_private->w->windowIcon(). 115 | pixmap(m_private->icon_label->size())); 116 | return QToolBar::eventFilter(obj, event); 117 | } 118 | default: 119 | return QToolBar::eventFilter(obj, event); 120 | } 121 | } 122 | 123 | void TitleBar::MaximizeButtonClicked() { 124 | auto w = window();//获取主窗口指针 125 | if(w->isMaximized()) 126 | w->showNormal(); 127 | else 128 | w->showMaximized(); 129 | } 130 | --------------------------------------------------------------------------------