Qt标准对话框[QFileDialog、QColorDialog、QFontDialog、QInputDialog、QMessageBox]

    技术2022-07-10  175

    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()); 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()); 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()); 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); 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()); 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()); 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(); QString text = QInputDialog::getText(this, "修改姓名", "更新姓名", QLineEdit::Normal, "佳人"); 输入整数:int getInt(); int age = QInputDialog::getInt(this, "修改年龄", "更新年龄", 18, 0, 200, 1); 输入浮点数:double getDouble(); double score = QInputDialog::getDouble(this, "修改分数", "更新分数", 80.00, 0, 100, 1, &ok); 在下拉列表框中选择输入:QString getItem(); QStringList gender = {"男", "女", "保密"}; QString text = QInputDialog::getItem(this, "修改性别", "更新性别", gender, 2); 输入多行字符串:QString getMultiLineText(); QString text = QInputDialog::getMultiLineText(this, "修改描述信息", "更新描述信息", "描述信息");

    消息框

    类名:QMessageBox 头文件:#include 《QMessageBox》

    静态函数

    信息提示对话框:int information(); int ret = QMessageBox::information(this, "提示","你以获取此道具,无法重复获取"); 问题对话框:int question(); int ret = QMessageBox::question(this, "问题", "是否确定?"); 警告信息对话框:int warning(); int ret = QMessageBox::warning(this, "警告", "这样做可能导致无法预知的错误,是否继续?", QMessageBox::Ok | QMessageBox::No); 错误信息对话框:int critical(); QMessageBox::critical(this, "错误", "发生未知错误!"); 关于对话框:void about(); QMessageBox::about(this, "关于", "关于我!"); 关于Qt对话框:void aboutQt(); 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头文件如下: #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文件如下: #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头文件如下: #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文件如下: #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头文件如下: #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文件如下: #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如下[自动生成,不用更改]: #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
    Processed: 0.027, SQL: 8