• QT基础第三天(3)widget,dialog和mainwindow


    首先我们先来看三大白板:

    1.widget (小部件): widget主要是在上面放置布局控件

    2.dialog (对话框): dialog有exec函数,如果是dialog窗口,后面的窗口是不能选取的

    3.mainwindow (主窗口):可以显示菜单,工具栏,状态栏,托盘等功能

    tips:

    (1.widget和dialog都有show函数,如果通过这个函数显示这两种类型的窗口,则两个窗口都是可选的

    (2.mainwindow和widget的主要区别就是能不能直接创建菜单栏等几种行为(注意:widget也可以只是要自己去定义实现,mainwidow是直接就有)

    (3.dialog和mainwindow两者之间没有直接关系

    一.widget

    QWidgt 类是所有用户界面对象的基类。 窗口部件是用户界面的一个基本单元:它从窗 口系统接收鼠标、键盘和其它事件,并且在屏幕上绘制自己。每一个窗口部件都是矩形的, 并且它们按 Z 轴顺序排列。一个窗口部件可以被它的父窗口部件或者它前面的窗口部件盖住 一部分。

     二.dialog

    QDialog 类是对话框窗口的基类。对话框窗口是主要用于短期任务以及和用户进行简要 通讯的顶级窗口。QDialog 可以是模态对话框也可以是非模态对话框。QDialog 支持扩展性并 且可以提供返回值。它们可以有默认按钮。

     例子1:

    mydialog.h

    1. #ifndef MYQDIALOG_H
    2. #define MYQDIALOG_H
    3. #include
    4. #include
    5. class myQDialog : public QDialog
    6. {
    7. Q_OBJECT //申明当前类支持信号与槽机制,QT c++特有语法
    8. public:
    9. myQDialog(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
    10. private:
    11. QPushButton *bt;
    12. };
    13. #endif // MYQDIALOG_H

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. class Widget : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. Widget(QWidget *parent = 0);
    9. ~Widget();
    10. };
    11. #endif // WIDGET_H

    .cpp

    1. #include "myqdialog.h"
    2. myQDialog::myQDialog(QWidget *parent, Qt::WindowFlags f)
    3. :QDialog(parent, f)
    4. {
    5. bt = new QPushButton("登录");
    6. bt->setParent(this);
    7. bt->setGeometry(150, 100, 100, 50);
    8. connect(bt, SIGNAL(clicked(bool)), this, SLOT(close()));
    9. }

    .cpp

    1. #include "widget.h"
    2. #include
    3. #include
    4. #include "myqdialog.h"
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. // QWidget *w = new QWidget;
    9. // w->setFixedSize(300, 200);
    10. // w->show();
    11. myQDialog *w = new myQDialog;
    12. w->setFixedSize(300, 200);
    13. w->exec(); //模态显示: 比show()高级
    14. }
    15. Widget::~Widget()
    16. {
    17. }

    必须点击第一个才能显示出第二个

     例子2:

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. class Widget : public QWidget
    14. {
    15. Q_OBJECT
    16. public slots:
    17. void get_font();
    18. void get_color();
    19. void get_filepath();
    20. void get_input();
    21. void show_msg();
    22. void show_errmsg();
    23. void show_progress();
    24. public:
    25. Widget(QWidget *parent = 0);
    26. ~Widget();
    27. private:
    28. QPushButton *bt_color;
    29. QPushButton *bt_errmsg;
    30. QPushButton *bt_file;
    31. QPushButton *bt_font;
    32. QPushButton *bt_input;
    33. QPushButton *bt_msg;
    34. QPushButton *bt_progress;
    35. QTextEdit *te;
    36. };
    37. #endif // WIDGET_H

    .cpp

    1. #include "widget.h"
    2. #include
    3. #include
    4. #include
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. bt_color = new QPushButton("颜色");
    9. bt_errmsg = new QPushButton("错误消息");
    10. bt_file = new QPushButton("文件路径选择");
    11. bt_font = new QPushButton("字体选择");
    12. bt_input = new QPushButton("输入框");
    13. bt_msg = new QPushButton("消息");
    14. bt_progress = new QPushButton("进度");
    15. QVBoxLayout *vbox = new QVBoxLayout;
    16. vbox->addWidget(bt_color);
    17. vbox->addWidget(bt_errmsg);
    18. vbox->addWidget(bt_file);
    19. vbox->addWidget(bt_font);
    20. vbox->addWidget(bt_input);
    21. vbox->addWidget(bt_msg);
    22. vbox->addWidget(bt_progress);
    23. te = new QTextEdit;
    24. QHBoxLayout *hbox = new QHBoxLayout;
    25. hbox->addLayout(vbox);
    26. hbox->addWidget(te);
    27. setLayout(hbox);
    28. connect(bt_font, &QPushButton::clicked, this, &Widget::get_font);
    29. connect(bt_color, &QPushButton::clicked, this, &Widget::get_color);
    30. connect(bt_file, SIGNAL(clicked(bool)), this, SLOT(get_filepath()));
    31. connect(bt_input, SIGNAL(clicked(bool)), this, SLOT(get_input()));
    32. connect(bt_msg, SIGNAL(clicked(bool)), this, SLOT(show_msg()));
    33. connect(bt_errmsg, SIGNAL(clicked(bool)), this, SLOT(show_errmsg()));
    34. connect(bt_progress, SIGNAL(clicked(bool)), this, SLOT(show_progress()));
    35. }
    36. void Widget::show_msg()
    37. {
    38. QMessageBox msgBox;
    39. msgBox.setText("The document has been modified.");
    40. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    41. qDebug() << msgBox.exec();
    42. }
    43. void Widget::show_errmsg()
    44. {
    45. QErrorMessage emg; //TODO
    46. emg.showMessage("aaaaaaaaaaaaaaaaaaaaaa");
    47. emg.exec();
    48. }
    49. void Widget::show_progress()
    50. {
    51. QProgressDialog progress("正在下载", "取消", 0, 100);
    52. progress.setValue(60);
    53. progress.exec();
    54. }
    55. void Widget::get_input()
    56. {
    57. QString name = QInputDialog::getText(this, "xxx", "你的名字");
    58. te->append(name);
    59. }
    60. void Widget::get_filepath()
    61. {
    62. //QString filepath = QFileDialog::getOpenFileName(); //弹出一个文件选择对话框,并且返回文件路径
    63. QStringList filepaths = QFileDialog::getOpenFileNames(this, "打开文件", "C://Users//ThinkPad T490//Desktop", "Images (*.png *.bmp *.jpg)"); //弹出一个文件选择对话框,并且返回一堆文件路径
    64. for(int i=0; ilength(); i++)
    65. te->append(filepaths[i]);
    66. }
    67. void Widget::get_color()
    68. {
    69. QColor color = QColorDialog::getColor(); //弹出一个颜色对话框,并且返回用户选择的颜色
    70. te->setTextColor(color);
    71. }
    72. void Widget::get_font()
    73. {
    74. // QFontDialog *d = new QFontDialog;
    75. // d->exec();
    76. bool ok;
    77. QFont font = QFontDialog::getFont(&ok); //弹出字体对话框,并且界面消失的时候反馈用户选择的字体
    78. if(ok)
    79. {
    80. //te->setFont(font); //设置整个文本框的字体
    81. te->setCurrentFont(font); //设置被选中的文字的字体
    82. }
    83. }
    84. Widget::~Widget()
    85. {
    86. }

    效果:

    三.mainwindow

    QMainWindow 类提供一个有菜单条、锚接窗口(例如工具条)和一个状态条的主应用程序窗口。主窗口通常用在提供一个大的中央窗口部件(例如文本编辑或者绘制画布)以及周围菜单、工具条和一个状态条。QMainWindow 常常被继承,因为这使得封装中央部件、菜单和工具条以及窗口状态条变得更容易,当用户点击菜单项或者工 具条按钮时,槽会被调用。

    例子实现简单的文件编辑器:

    .h

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public slots:
    10. void new_file();
    11. void open_file();
    12. void save_file();
    13. void saveas_file();
    14. void close_file();
    15. void set_font();
    16. void set_color();
    17. public:
    18. MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. private:
    21. QTextEdit *te;
    22. QLabel *lb;
    23. };
    24. #endif // MAINWINDOW_H

    .cpp

    1. #include "mainwindow.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. MainWindow::MainWindow(QWidget *parent)
    14. : QMainWindow(parent)
    15. {
    16. //0. 构造所有的action
    17. QAction *new_action = new QAction(QIcon(":/img/new.png"), "新建");
    18. new_action->setShortcut(QKeySequence("Ctrl+N"));
    19. connect(new_action, SIGNAL(triggered(bool)), this, SLOT(new_file()));
    20. QAction *open_action = new QAction(QIcon(":/img/open.png"), "打开");
    21. open_action->setShortcut(QKeySequence("Ctrl+O"));
    22. connect(open_action, SIGNAL(triggered(bool)), this, SLOT(open_file()));
    23. //open_action->setEnabled(false);
    24. QAction *saveas_action = new QAction(QIcon(":/img/saveas.png"), "另存为");
    25. saveas_action->setShortcut(QKeySequence("Ctrl+Y"));
    26. connect(saveas_action, SIGNAL(triggered(bool)), this, SLOT(saveas_file()));
    27. QAction *save_action = new QAction(QIcon(":/img/save.png"), "保存");
    28. save_action->setShortcut(QKeySequence("Ctrl+s"));
    29. connect(save_action, SIGNAL(triggered(bool)), this, SLOT(save_file()));
    30. QAction *close_action = new QAction(QIcon(":/img/close.png"), "保存");
    31. close_action->setShortcut(QKeySequence("Ctrl+X"));
    32. connect(close_action, SIGNAL(triggered(bool)), this, SLOT(close_file()));
    33. QAction *font_action = new QAction(QIcon(":/img/font.png"), "字体");
    34. font_action->setShortcut(QKeySequence("Ctrl+f"));
    35. connect(font_action, SIGNAL(triggered(bool)), this, SLOT(set_font()));
    36. QAction *color_action = new QAction(QIcon(":/img/color.png"), "颜色");
    37. color_action->setShortcut(QKeySequence("Ctrl+f"));
    38. connect(color_action, SIGNAL(triggered(bool)), this, SLOT(set_color()));
    39. //1. 获取菜单栏menuBar(),添加菜单->addMenu,添加选项
    40. QMenu *fileMenu = menuBar()->addMenu("&File"); //&F: 用键盘Alt+f
    41. fileMenu->addAction(new_action);
    42. fileMenu->addAction(open_action);
    43. fileMenu->addAction(save_action);
    44. fileMenu->addAction(saveas_action);
    45. fileMenu->addAction(close_action);
    46. QMenu *editMenu = menuBar()->addMenu("&Edit"); //&E: 用键盘Alt+e
    47. editMenu->addAction(font_action);
    48. editMenu->addAction(color_action);
    49. //2. 工具栏
    50. QToolBar *filetoolbar = addToolBar("file"); //添加一个工具栏,并且放入常用的action
    51. filetoolbar->addAction(new_action);
    52. filetoolbar->addAction(open_action);
    53. filetoolbar->addAction(save_action);
    54. filetoolbar->addAction(saveas_action);
    55. filetoolbar->addAction(close_action);
    56. QToolBar *edittoolbar = addToolBar("edit"); //添加一个工具栏,并且放入常用的action
    57. edittoolbar->addAction(font_action);
    58. edittoolbar->addAction(color_action);
    59. QToolBar *xxxtoolbar = addToolBar("编译"); //添加一个工具栏,并且放入常用的action
    60. QToolButton *tb = new QToolButton;
    61. tb->setText("编译");
    62. xxxtoolbar->addWidget(tb);
    63. // tb->setDisabled(true);
    64. // tb->setEnabled(false);
    65. //3. 设置中央部件
    66. te = new QTextEdit;
    67. te->setMinimumSize(640, 480);
    68. this->setCentralWidget(te);
    69. te->setDisabled(true);
    70. //4. 状态栏
    71. lb = new QLabel;
    72. QStatusBar *st = statusBar();
    73. st->addWidget(lb);
    74. connect(te, &QTextEdit::textChanged, [&]{
    75. if(!lb->text().contains('*'))
    76. lb->setText(lb->text()+'*');
    77. });
    78. }
    79. void MainWindow::set_font()
    80. {
    81. qDebug() << "set font........";
    82. bool ok;
    83. QFont font = QFontDialog::getFont(&ok);
    84. if(ok)
    85. te->setCurrentFont(font);
    86. }
    87. void MainWindow::set_color()
    88. {
    89. qDebug() << "set color........";
    90. QColor color = QColorDialog::getColor();
    91. te->setTextColor(color);
    92. }
    93. void MainWindow::new_file()
    94. {
    95. qDebug() << "new........";
    96. te->setEnabled(true);
    97. if(lb->text().contains('*')) //有文件在编辑中
    98. {
    99. close_file();
    100. }
    101. else
    102. {
    103. //清除工作区
    104. te->clear();
    105. lb->clear();
    106. }
    107. }
    108. void MainWindow::open_file()
    109. {
    110. qDebug() << "open........";
    111. te->setEnabled(true);
    112. //1. 提取文件路径
    113. QString path = QFileDialog::getOpenFileName();
    114. //2. 提取出文件内容 QFile
    115. QFile f(path);
    116. f.open(QIODevice::ReadOnly);
    117. QByteArray buf = f.readAll();
    118. f.close();
    119. //3. 将内容显示在文本编辑框
    120. te->setText(buf);
    121. //4. 显示文件路径在状态栏
    122. lb->setText(path);
    123. }
    124. void MainWindow::save_file()
    125. {
    126. //1. 提取文件路径
    127. QString str = lb->text();
    128. if(!str.contains('*')) //如果文件未有改动
    129. return;
    130. str.chop(1);
    131. if(str.isEmpty()) //这是一个新建的文件
    132. {
    133. saveas_file();
    134. return;
    135. }
    136. //2. 提取文本编辑框的内容
    137. QString buf = te->toPlainText();
    138. //3. 将内容写入文件
    139. QFile f(str);
    140. f.open(QIODevice::WriteOnly);
    141. f.write(buf.toStdString().c_str());
    142. f.close();
    143. //4. 更新状态栏
    144. lb->setText(str);
    145. }
    146. void MainWindow::saveas_file()
    147. {
    148. //1. 提取文件路径
    149. QString str = QFileDialog::getSaveFileName();
    150. if(str.isEmpty())
    151. return;
    152. //2. 提取文本编辑框的内容
    153. QString buf = te->toPlainText();
    154. //3. 将内容写入文件
    155. QFile f(str);
    156. f.open(QIODevice::WriteOnly);
    157. f.write(buf.toStdString().c_str());
    158. f.close();
    159. //3. 更新状态栏
    160. lb->setText(str);
    161. }
    162. void MainWindow::close_file()
    163. {
    164. qDebug() << "close........";
    165. if(lb->text().contains('*')) //有文件被编辑中
    166. {
    167. //1. 弹框提示是否需要保存
    168. QMessageBox msgBox;
    169. msgBox.setText("The document has been modified.");
    170. msgBox.setInformativeText("Do you want to save your changes?");
    171. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    172. msgBox.setDefaultButton(QMessageBox::Save);
    173. int ret = msgBox.exec();
    174. //2. 如果要保存
    175. if(QMessageBox::Save == ret)
    176. save_file();
    177. else if(QMessageBox::Cancel == ret)
    178. return;
    179. }
    180. //清除工作区
    181. te->clear();
    182. lb->clear();
    183. }
    184. MainWindow::~MainWindow()
    185. {
    186. }

    效果:

    用的时候怎么选择?

    窗口类型介绍:QMainWindow、QWidget、QDialog三个类都可以用来创建窗口,可以直接使用,也可以继承后使用。
      1.如果是主窗口,就使用QMainWindow类;
      2.如果是对话框,就使用QDialog类;
      3.如果不确定,有可能作为顶层窗口,也有可能嵌入到其他窗口,就使用QWidget类。

  • 相关阅读:
    Golang中的闭包详解
    SpringSecurity知识点总结-DX的笔记
    北大肖臻老师《区块链技术与应用》系列课程学习笔记[13]以太坊-状态树
    代理IP和Socks5代理:跨界电商与爬虫的智能引擎
    C语言整理(待更新)
    Linux 环境搭建一步到位,看这篇就够了!
    Scala基础【入门及安装】
    mysql(六)------数据库的范式和约束字段
    城市大脑 术语
    stp文件转stl
  • 原文地址:https://blog.csdn.net/fuyuyf/article/details/126050729