Qt标准对话框
- 文件对话框
- 静态函数
- 颜色对话框
- 静态函数
- 字体对话框
- 静态函数
- 输入对话框
- 静态函数
- 消息框
- 静态函数
- 实战
- 题目
- 实现
文件对话框
类名:QFileDialog
头文件: #include 《QFileDialog》
静态函数
- QString getOpenFileName(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = Q_NULLPTR,
Options options = Options());
1 2 3 4 | QString path = QFileDialog::getOpenFileName(this, "文件标准对话框", ".", "C++ files(*.cpp);;C files(*.c);;Header files(*.h),png格式(*.png)"); |
参数说明:
| 参数名 | 说明 |
|---|---|
| parent | 父组件 |
| caption | 对话框的标题 |
| dir | 对话框打开时的默认路径。 “.“表示程序运行目录,“/”表示当前盘符的根目录。如果想选择程序运行目录下的image文件,则应写成”./image/”;如果想默认选择image目录下的bg.png文件,则应写成"./image/bg.png"。 |
| filter | 文件后缀名过滤器。定义形式:“C++ files(星号.cpp)”。如果想要在一个过滤器内放多种文件后缀名,则应定义成"C++ files(星号.cpp),C files(星号.c)",即每个过滤器后加逗号;如果想要多个过滤器,则应定义成"“C++ files(星号.cpp);C files(星号.c)”,即每个过滤器后加分号。 |
| selectedFilter | 默认选择的过滤器 |
| Options | 对话框的参数设定 |
- QStringList getOpenFileNames(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = Q_NULLPTR,
Options options = Options());
1 2 3 4 | QStringList path = QFileDialog::getOpenFileNames(this, "文件标准对话框", ".", "C++文件(*.cpp);; C 文件(*.c);;头文件(*.h)"); |
参数说明:
| 参数名 | 说明 |
|---|---|
| parent | 父组件 |
| caption | 对话框的标题 |
| dir | 对话框打开时的默认路径。 “.“表示程序运行目录,“/”表示当前盘符的根目录。如果想选择程序运行目录下的image文件,则应写成”./image/”;如果想默认选择image目录下的bg.png文件,则应写成"./image/bg.png"。 |
| filter | 文件后缀名过滤器。定义形式:“C++ files(星号.cpp)”。如果想要在一个过滤器内放多种文件后缀名,则应定义成"C++ files(星号.cpp),C files(星号.c)",即每个过滤器后加逗号;如果想要多个过滤器,则应定义成"“C++ files(星号.cpp);C files(星号.c)”,即每个过滤器后加分号。 |
| selectedFilter | 默认选择的过滤器 |
| Options | 对话框的参数设定 |
- QString getSaveFileName(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = Q_NULLPTR,
Options options = Options());
1 2 3 4 | QString path = QFileDialog::getSaveFileName(this, "获取文件保存路径", ".", "C++文件(*.cpp);;C文件(*.c);;头文件(*.h)"); |
参数说明:
| 参数名 | 说明 |
|---|---|
| parent | 父组件 |
| caption | 对话框的标题 |
| dir | 对话框打开时的默认路径。 “.“表示程序运行目录,“/”表示当前盘符的根目录。如果想选择程序运行目录下的image文件,则应写成”./image/”;如果想默认选择image目录下的bg.png文件,则应写成"./image/bg.png"。 |
| filter | 文件后缀名过滤器。定义形式:“C++ files(星号.cpp)”。如果想要在一个过滤器内放多种文件后缀名,则应定义成"C++ files(星号.cpp),C files(星号.c)",即每个过滤器后加逗号;如果想要多个过滤器,则应定义成"“C++ files(星号.cpp);C files(星号.c)”,即每个过滤器后加分号。 |
| selectedFilter | 默认选择的过滤器 |
| Options | 对话框的参数设定 |
- QString getExistingDirectory(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QString &dir = QString(),
Options options = ShowDirsOnly);
1 2 3 | QString path = QFileDialog::getExistingDirectory(this, "选择一个已有目录", "."); |
- QUrl getOpenFileUrl(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QUrl &dir = QUrl(),
const QString &filter = QString(),
QString *selectedFilter = Q_NULLPTR,
Options options = Options(),
const QStringList &supportedSchemes = QStringList());
1 2 3 4 5 | QUrl url; url = QFileDialog::getOpenFileUrl(this, "URL", QUrl(), "C++ files(*.cpp)"); |
颜色对话框
类名:QColorDialog
头文件:#include 《QColorDialog》
静态函数
- QColor getColor(const QColor &initial = Qt::white,
QWidget *parent = Q_NULLPTR,
const QString &title = QString(),
ColorDialogOptions options = ColorDialogOptions());
1 | QColor color = QColorDialog::getColor(Qt::yellow, this); |
字体对话框
类名:QFontDialog
头文件:#include 《QFontDialog》
静态函数
- QFont getFont(bool *ok, QWidget *parent = Q_NULLPTR);
- QFont getFont(bool *ok, const QFont &initial, QWidget *parent = Q_NULLPTR, const QString &title = QString(),
FontDialogOptions options = FontDialogOptions());
输入对话框
类名:QInputDialog
头文件:#include 《QInputDialog》
静态函数
- 输入单行文字:QString getText();
1 2 3 4 5 | QString text = QInputDialog::getText(this, "修改姓名", "更新姓名", QLineEdit::Normal, "佳人"); |
- 输入整数:int getInt();
1 | int age = QInputDialog::getInt(this, "修改年龄", "更新年龄", 18, 0, 200, 1); |
- 输入浮点数:double getDouble();
1 | double score = QInputDialog::getDouble(this, "修改分数", "更新分数", 80.00, 0, 100, 1, &ok); |
- 在下拉列表框中选择输入:QString getItem();
1 2 3 4 5 6 7 | QStringList gender = {"男", "女", "保密"}; QString text = QInputDialog::getItem(this, "修改性别", "更新性别", gender, 2); |
- 输入多行字符串:QString getMultiLineText();
1 2 3 4 | QString text = QInputDialog::getMultiLineText(this, "修改描述信息", "更新描述信息", "描述信息"); |
消息框
类名:QMessageBox
头文件:#include 《QMessageBox》
静态函数
- 信息提示对话框:int information();
1 | int ret = QMessageBox::information(this, "提示","你以获取此道具,无法重复获取"); |
- 问题对话框:int question();
1 | int ret = QMessageBox::question(this, "问题", "是否确定?"); |
- 警告信息对话框:int warning();
1 | int ret = QMessageBox::warning(this, "警告", "这样做可能导致无法预知的错误,是否继续?", QMessageBox::Ok | QMessageBox::No); |
- 错误信息对话框:int critical();
1 | QMessageBox::critical(this, "错误", "发生未知错误!"); |
- 关于对话框:void about();
1 | QMessageBox::about(this, "关于", "关于我!"); |
- 关于Qt对话框:void aboutQt();
1 | QMessageBox::aboutQt(this, "关于Qt"); |
实战
题目
-
实现[如图1-1]界面。实现点击[文件标准对话框]按钮,弹出[如图1-2]文件标准对话框界面;点击[颜色标准对话框]按钮,弹出[如图1-3]颜色标准对话框界面;点击[字体标准对话框],弹出[如图1-4]字体标准对话框界面;点击[显示输入框]按钮,弹出[如图1-5]界面;点击[显示提示框]按钮,弹出[如图1-6]界面;
-
输入框相关:实现[如图1-5]界面后,实现其对应功能。点击[修改姓名]按钮,弹出[如图1-7]输入单行文字界面;点击[修改性别]按钮,弹出[如图1-8]在下拉列表框中输入界面;点击[修改年龄]按钮,弹出[如图1-9]所示输入整数界面;点击[修改得分]按钮,弹出[1-10]所示输入整数界面;点击
[修改描述]按钮,弹出[1-11]所示输入多行字符串界面。 -
提示框相关:实现[如图1-6]界面后,实现对应功能。点击[信息提示对话框]按钮,弹出[如图1-12]所示信息提示对话框;点击[问题对话框]按钮,弹出[如图1-13]所示问题对话框;点击[警告对话框]按钮,弹出[如图1-14]所示警告信息对话框;点击[错误对话框]按钮,弹出[如图1-15]所示错误信息对话框;点击[关于对话框]按钮,弹出[如图1-16]所示关于对话框;点击[关于Qt对话框]按钮,弹出[如图1-17]所示关于Qt对话框。
图1-1

图1-2

图1-3

图1-4

图1-5

图1-6

图1-7

图1-8

图1-9

图1-10

图1-11

图1-12

图1-13

图1-14

图1-15

图1-16

图1-17

实现
新建桌面应用程序,编译器minGW;类名Widget,继承自QDialog;不勾选界面文件。
- Widget头文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #ifndef WIDGET_H #define WIDGET_H #include <QDialog> #include <QPushButton> #include <QLabel> #include <QGridLayout> #include <QColorDialog> #include <QFrame> #include <QLineEdit> #include <QInputDialog> #include "input.h" #include "message.h" class Widget : public QDialog { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QPushButton *m_fileBtn; // 打开文件对话框按钮 QLabel *m_showPath; // 显示文件路径 // 颜色相关 QPushButton *m_colorBtn; // 打开颜色对话框按钮 QFrame *m_showColor; // 显示所选颜色效果 // 字体相关 QPushButton *m_fontBtn; // 打开字体对话框 QLineEdit *m_showFont; // 显示字体效果 // 输入框相关 QPushButton *m_inputBtn; // 打开输入框对话框 Input *m_showInput; // 输入框类 // 提示对话框相关 QPushButton *m_tipBtn; // 打开提示对话框 Message *m_showMsg; // 提示框类 QGridLayout *m_layout; // 布局管理器 private slots: void fileDialog(); void colorDialog(); void fontDialog(); void inputDialog(); void msgDialog(); }; #endif // WIDGET_H |
- Widget.cpp文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include "widget.h" #include <QFileDialog> #include <QFontDialog> Widget::Widget(QWidget *parent) : QDialog(parent) { setWindowTitle("对话框"); // 文件对话框 m_fileBtn = new QPushButton("文件标准对话框"); m_fileBtn->setFixedSize(150, 30); QLabel *filePathLabel = new QLabel("文件路径:"); filePathLabel->setFixedSize(80, 30); m_showPath = new QLabel("文件路径"); m_showPath->setFixedSize(400, 30); m_showPath->setFrameShape(QFrame::Box); m_layout = new QGridLayout(this); m_layout->setMargin(10); m_layout->setSpacing(10); m_layout->addWidget(filePathLabel, 0, 0, 1, 1); m_layout->addWidget(m_showPath, 0, 1, 1, 1); m_layout->addWidget(m_fileBtn, 0, 2, 1, 1); // 颜色对话框 m_colorBtn = new QPushButton("颜色标准对话框"); m_colorBtn->setFixedSize(150, 30); m_showColor = new QFrame(); m_showColor->setFixedSize(490, 30); // 设置边框风格 m_showColor->setFrameStyle(QFrame::Box); // 设置填充背景属性 m_showColor->setAutoFillBackground(true); m_layout->addWidget(m_showColor, 1, 0, 1, 2); m_layout->addWidget(m_colorBtn, 1, 2, 1, 1); // 字体对话框 m_fontBtn = new QPushButton("字体标准对话框"); m_fontBtn->setFixedSize(150, 30); m_showFont = new QLineEdit; m_showFont->setFixedSize(490, 30); m_showFont->setText("字体选择效果"); m_showFont->setReadOnly(true); m_layout->addWidget(m_showFont, 2, 0, 1, 2); m_layout->addWidget(m_fontBtn, 2, 2, 1, 1); // 输入框相关 m_inputBtn = new QPushButton("显示输入框"); m_inputBtn->setFixedSize(650, 40); m_layout->addWidget(m_inputBtn, 3, 0, 1, 3); // 提示框相关 m_tipBtn = new QPushButton("显示提示框"); m_tipBtn->setFixedSize(650, 40); m_layout->addWidget(m_tipBtn, 4, 0, 1, 3); this->setLayout(m_layout); // 文件对话框信号与槽 connect(m_fileBtn, &QPushButton::clicked, this, &Widget::fileDialog); // 颜色对话框信号与槽 connect(m_colorBtn, &QPushButton::clicked, this, &Widget::colorDialog); // 字体对话框信号与槽 connect(m_fontBtn, &QPushButton::clicked, this, &Widget::fontDialog); // 输入框对话框信号与槽 connect(m_inputBtn, &QPushButton::clicked, this, &Widget::inputDialog); // 提示框对话框信号与槽 connect(m_tipBtn, &QPushButton::clicked, this, &Widget::msgDialog); } Widget::~Widget() { } void Widget::fileDialog() { QString path = QFileDialog::getOpenFileName(this, "文件标准对话框", ".", "C++ files(*.cpp);;C files(*.c);;Header files(*.h);;所有文件()"); if(path.length() > 0) m_showPath->setText(path); } void Widget::colorDialog() { QColor color = QColorDialog::getColor(Qt::yellow, this,"颜色标准对话框"); // isValid()方法 检查color是否有效 if(color.isValid())m_showColor->setPalette(QPalette(color)); } void Widget::fontDialog() { bool ok; QFont font; font = QFontDialog::getFont(&ok); if(ok){ m_showFont->setFont(font); } } void Widget::inputDialog() { m_showInput = new Input(this); m_showInput->show(); } void Widget::msgDialog() { m_showMsg = new Message; m_showMsg->show(); } |
- 添加新类,类名Input,继承自QDialog。
- input头文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #ifndef INPUT_H #define INPUT_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QGridLayout> class Input : public QDialog { enum _INFORMATION { INFORMATION_NAME, INFORMATION_GENDER, INFORMATION_AGE, INFORMATION_SCORE, INFORMATION_DESCRIBTION, INFORMATION_ALL }; public: Input(QWidget *parent = 0); private slots: void modifyName(); // 修改姓名 void modifyGender(); // 修改性别 void modifyAge(); // 修改年龄 void modifyScore(); // 修改分数 void modifyDescription(); // 修改描述信息 private: QLabel *m_tipLabels[INFORMATION_ALL]; // 提示标签 QLabel *m_showLabels[INFORMATION_ALL]; // 显示标签 QPushButton *m_btns[INFORMATION_ALL]; // 按钮 QGridLayout *m_layout; // 布局管理器 }; #endif // INPUT_H |
- input.cpp文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include "input.h" #include <QInputDialog> Input::Input(QWidget *parent) : QDialog(parent) { setWindowTitle("输入框"); QStringList info = {"姓名", "性别", "年龄", "得分", "描述"}; for(int i = 0; i < INFORMATION_ALL; i++){ m_tipLabels[i] = new QLabel(info.at(i)+":"); m_tipLabels[i]->setFixedSize(50, 30); m_showLabels[i] = new QLabel; m_showLabels[i]->setFixedSize(300, 30); m_showLabels[i]->setFrameStyle(QLabel::Box); m_btns[i] = new QPushButton("修改" + info.at(i)); m_btns[i]->setFixedSize(120, 30); } m_layout = new QGridLayout(this); for(int i = 0; i < INFORMATION_ALL; i++) { m_layout->addWidget(m_tipLabels[i], i, 0, 1, 1); m_layout->addWidget(m_showLabels[i], i, 1, 1, 1); m_layout->addWidget(m_btns[i], i, 2, 1, 1); } setLayout(m_layout); // 信号连接 connect(m_btns[INFORMATION_NAME], &QPushButton::clicked, this, &Input::modifyName); connect(m_btns[INFORMATION_GENDER], &QPushButton::clicked, this, &Input::modifyGender); connect(m_btns[INFORMATION_AGE], &QPushButton::clicked, this, &Input::modifyAge); connect(m_btns[INFORMATION_SCORE], &QPushButton::clicked, this, &Input::modifyScore); connect(m_btns[INFORMATION_DESCRIBTION], &QPushButton::clicked, this, &Input::modifyDescription); } void Input::modifyName() { QString text = QInputDialog::getText(this, "修改姓名", "更新姓名", QLineEdit::Normal, m_showLabels[INFORMATION_NAME]->text()); if(text.length() > 0) m_showLabels[INFORMATION_NAME]->setText(text); } void Input::modifyGender() { QStringList gender = {"男", "女", "保密"}; QString text = QInputDialog::getItem(this, "修改性别", "更新性别", gender, 2); if(text.length() > 0) m_showLabels[INFORMATION_GENDER]->setText(text); } void Input::modifyAge() { int age = QInputDialog::getInt(this, "修改年龄", "更新年龄", 18, 0, 200, 1); m_showLabels[INFORMATION_AGE]->setText(QString::number(age)); } void Input::modifyScore() { bool ok; double score = QInputDialog::getDouble(this, "修改分数", "更新分数", 80.00, 0, 100, 1, &ok); if(ok) { m_showLabels[INFORMATION_SCORE]->setText(QString("%1").arg(score)); } } void Input::modifyDescription() { QString text = QInputDialog::getMultiLineText(this, "修改描述信息", "更新描述信息", m_showLabels[INFORMATION_DESCRIBTION]->text()); m_showLabels[INFORMATION_DESCRIBTION]->setText(text); } |
- 添加新类Message,继承自QDialog。
- message头文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #ifndef MESSAGE_H #define MESSAGE_H #include <QLabel> #include <QDialog> #include <QMessageBox> #include <QPushButton> #include <QGridLayout> class Message : public QDialog { enum MessageBoxKinds{ MessageBoxKinds_INFORMATION, MessageBoxKinds_QUESTION, MessageBoxKinds_WAMING, MessageBoxKinds_CRITICAL, MessageBoxKinds_ABOUT, MessageBoxKinds_ABOUT_QT, MessageBoxKinds_ALL }; public: Message(QWidget *parent = 0); private: QPushButton *m_btns[MessageBoxKinds_ALL]; // 弹出信息提示框按钮 QGridLayout *m_layout; // 布局管理器 private slots: void showInformationMsg(); void showQuestionMsg(); void showWamingMsg(); void showCriticalMsg(); void showAboutMsg(); void showAboutQtMsg(); }; #endif // MESSAGE_H |
- message.cpp文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include "message.h" Message::Message(QWidget *parent) : QDialog(parent) { setWindowTitle("提示框"); QStringList name = {"信息提示", "问题", "警告", "错误", "关于", "关于Qt"}; m_layout = new QGridLayout(this); m_layout->setMargin(10); m_layout->setSpacing(10); for(int i = 0; i < MessageBoxKinds_ALL; i++) { m_btns[i] = new QPushButton(name.at(i) + "对话框"); m_btns[i]->setFixedSize(150, 35); m_layout->addWidget(m_btns[i], i / 2, i % 2, 1, 1); } connect(m_btns[MessageBoxKinds_INFORMATION], &QPushButton::clicked, this, &Message::showInformationMsg); connect(m_btns[MessageBoxKinds_QUESTION], &QPushButton::clicked, this, &Message::showQuestionMsg); connect(m_btns[MessageBoxKinds_WAMING], &QPushButton::clicked, this, &Message::showWamingMsg); connect(m_btns[MessageBoxKinds_CRITICAL], &QPushButton::clicked, this, &Message::showCriticalMsg); connect(m_btns[MessageBoxKinds_ABOUT], &QPushButton::clicked, this, &Message::showAboutMsg); connect(m_btns[MessageBoxKinds_ABOUT_QT], &QPushButton::clicked, this, &Message::showAboutQtMsg); } void Message::showInformationMsg() { int ret = QMessageBox::information(this, "提示","你以获取此道具,无法重复获取"); } void Message::showQuestionMsg() { int ret = QMessageBox::question(this, "问题", "是否确定?"); } void Message::showWamingMsg() { int ret = QMessageBox::warning(this, "警告", "这样做可能导致无法预知的错误,是否继续?", QMessageBox::Ok | QMessageBox::No); } void Message::showCriticalMsg() { QMessageBox::critical(this, "错误", "发生未知错误!"); } void Message::showAboutMsg() { QMessageBox::about(this, "关于", "关于我!"); } void Message::showAboutQtMsg() { QMessageBox::aboutQt(this, "关于Qt"); } |
- main.cpp如下[自动生成,不用更改]:
1 2 3 4 5 6 7 8 9 10 11 | #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); } |
















