Qt5——基本对话框(1)

简介: Qt5——基本对话框(1)

文章目录

Qt中的各种对话框应用

各个静态函数详细说明

实例实现

工具盒类

示例“qq界面实例”

实现步骤

界面效果


Qt中的各种对话框应用

通过一个实例来介绍Qt5中对于各类对话框的应用。这个实例中具有以下这几种QT的标准对话框:


  • 标准文件对话框(QFileDialog)
  • 标准颜色对话框(QColorDialog)
  • 标准字体对话框(QFontDialog)
  • 标准输入对话框(QInputDialog)
  • 标准消息对话框(QMessageDialog)

各种基本对话框都是通过调用各自不同的静态函数来完成其功能。具体如下:


相关类 类说明 静态函数 函数说明

QFileDialog类 标准文件对话框 getOpenFileName 获得用户选择的文件名

– – getSaveFileName 获得用户保存的文件名

– – getExistingDirectory 获得用户选择的已存在的目录名

– – getOpenFileNames 获得用户选择的文件名列表

QColorDialog类 标准颜色对话框 getColor 获得用户选择的颜色值

QFontDialog类 标准字体对话框 getFont 获得用户选择的字体

QInputDialog类 标准输入对话框 getText 标准字符串输入对话框

– – getItem 下拉表条目输入框

– – getInt int类型数据输入对话框

– – getDouble double类型数据输入对话框

QMessageBox类 消息对话框 QMessageBox::question question消息框

– – QMessageBox::information information消息框

– – QMessageBox::warning warning消息框

– – QMessageBox::critical critical消息框

– – QMessageBox::about about消息框

– – QMessageBox::aboutQt about Qt消息框

各个静态函数详细说明

  • 标准文件对话框QFileDialog::getOpenFileName()

 

static QString getOpenFileName(QWidget *parent = nullptr, //标准文件对话框的父窗口
                                   const QString &caption = QString(),  //标准文件对话框的标题名
                                   const QString &dir = QString(),  //指定默认目录
                                   const QString &filter = QString(),//制定对文件类型的过滤
                                   QString *selectedFilter = nullptr,//用户选择的过滤器通过此函数返回
                                   Options options = Options());//选择显示文件名的个数,默认是同时是目录与文件名
void Dialog::showFile()
{
    //设置标准文件对话框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}

20200303211633429.png

  • 标准颜色对话框QColor::getColor()

 

static QColor getColor(const QColor &initial = Qt::white, //默认选中的颜色
                           QWidget *parent = nullptr, //对话框的父窗口
                           const QString &title = QString(),  //对话框的标题名
                           ColorDialogOptions options = ColorDialogOptions());
void Dialog::showColor()
{
    //设置标准颜色对话框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

20200303211650215.png


  • 标准字体对话框QFontDialog::getFont()

 

static QFont getFont(bool *ok, //若用户选择“ok”则该参数为true,函数返回所选择的字体。否则设为false,返回默认字体。
          QWidget *parent = nullptr);//父窗口
void Dialog::showFont()
{
    //设置标准字体对话框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}

20200303211658998.png


  • 标准输入对话框类:标准字符串输入对话框QInputDialog::getText()

 

static QString getText(QWidget *parent, //父窗口
          const QString &title, //标题名
          const QString &label,//标签提示
                           QLineEdit::EchoMode echo = QLineEdit::Normal,//指定输入对话框中的QLineEdit控件输入模式
                           const QString &text = QString(), //制定QLineEdit控件出现的文字
                           bool *ok = nullptr,//用户选中“ok”则返回true,否则为false
                           Qt::WindowFlags flags = Qt::WindowFlags(),
                           Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
void InputDlg::ChangeName()
{
    //标准字符串输入对话框
    bool ok;
    QString text = QInputDialog::getText(this, tr("标准字符串输入对话框"), tr("请输入姓名:"), QLineEdit::Normal, nameLabel2->text(), &ok);
    if(ok && !text.isEmpty())
    {
        nameLabel2->setText(text);
    }
}

20200303211757932.png

  • 标准输入对话框类:标准条目选择对话框QInputDialog::getItem()

 

static QString getItem(QWidget *parent, //父窗口
          const QString &title, //对话框标题名
          const QString &label,//标签提示
                           const QStringList &items, //指定控件显示的可选条目为一个QStringList对象
                           int current = 0, //对话框弹出时默认显示的条目序号。
                           bool editable = true,//指定控件中显示的文字是否可编辑
                           bool *ok = nullptr, //提示按钮触发,如果是ok按钮则为true,否则为false。
                           Qt::WindowFlags flags = Qt::WindowFlags(),//指明窗口标识。
                           Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
void InputDlg::ChangeSex()
{
    //标准条目选择对话框
    QStringList SexItems;
    SexItems << tr("男") << tr("女");
    bool ok;
    QString SexItem = QInputDialog::getItem(this, tr("标准条目选择对话框"), tr("请选择性别:"), SexItems, 0, false, &ok);
    if(ok && !SexItem.isEmpty())
    {
        sexLabel2->setText(SexItem);
    }
}

2020030321181525.png

  • 标准输入对话框类:标准int类型输入对话框QInputDialog::getInt()

 

static int getInt(QWidget *parent, //父窗口
          const QString &title, 标题
          const QString &label, 标签提示
          int value = 0,//默认显示值
                      int minValue = -2147483647, //数值范围最小值
                      int maxValue = 2147483647,//数值范围最大值
                      int step = 1, //指定控件步进值
                      bool *ok = nullptr, //提示按钮触发情况反馈
                      Qt::WindowFlags flags = Qt::WindowFlags());
void InputDlg::ChangeAge()
{
    //标准int类型输入对话框
    bool ok;
    int age = QInputDialog::getInt(this, tr("标准int类型输入对话框"), tr("请输入年龄:"), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);
    if(ok)
    {
        ageLabel2->setText(QString(tr("%1")).arg(age));
    }
}

20200303211825315.png

  • 标准输入对话框类:标准double类型输入对话框QInputDialog::getDouble()

 

static double getDouble(QWidget *parent, 
          const QString &title, 
          const QString &label, 
          double value = 0,
                            double minValue = -2147483647, 
                            double maxValue = 2147483647,
                            int decimals = 1, 
                            bool *ok = nullptr, 
                            Qt::WindowFlags flags = Qt::WindowFlags());
void InputDlg::ChangeScore()
{
    //标准double类型输入对话框
    bool ok;
    double score = QInputDialog::getDouble(this, tr("标准double类型输入对话框"), tr("请输入成绩:"), scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
    if(ok)
    {
        scoreLabel2->setText(QString(tr("%1")).arg(score));
    }
}

20200303211836226.png

  • 消息对话框:question消息框QMessageBox::question()

 

static int question(QWidget *parent, 
          const QString &title,
                        const QString& text,
                        int button0, 
                        int button1 = 0, 
                        int button2 = 0);
void MsgBoxDlg::showQuestionMsg()
{
    //question消息对话框
    label->setText(tr("question message box"));
    switch (QMessageBox::question(this, tr("question消息框"), tr("您现在已经修改完成,是否结束程序"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)) {
    case QMessageBox::Ok:
        label->setText("Question button/OK");
        break;
    case QMessageBox::Cancel:
        label->setText("Question button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

20200303211854519.png

  • 消息对话框:information消息框QMessageBox::information()

 

static int information(QWidget *parent, 
          const QString &title,
                           const QString& text,
                           int button0, 
                           int button1 = 0, 
                           int button2 = 0);
void MsgBoxDlg::showInformationMsg()
{
    //information消息框
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this, tr("information消息框"), tr("这个information消息框测试,欢迎您!"));
    return ;
}

20200303211906660.png

  • 消息对话框:warning消息框QMessageBox::warning()

 

static int warning(QWidget *parent, 
          const QString &title,
                       const QString& text,
                       int button0, 
                       int button1, 
                       int button2 = 0);
void MsgBoxDlg::showWarningMsg()
{
    //warning消息框
    label->setText(tr("warning message box"));
    switch (QMessageBox::warning(this, tr("warning消息框"), tr("您修改的内容还未保存,是否要保存对文档的修改?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save)) {
    case QMessageBox::Save:
        label->setText("Warning button/Save");
        break;
    case QMessageBox::Discard:
        label->setText("Warning button/Discard");
        break;
    case QMessageBox::Cancel:
        label->setText("Warning button/Cancel");
        break;
    default:
        break;
    }
    return ;
}

20200303211926271.png

  • 消息对话框:critical消息框QMessageBox::critical()

 

static int critical(QWidget *parent, 
          const QString &title,
                        const QString& text,
                        int button0, 
                        int button1, 
                        int button2 = 0);
void MsgBoxDlg::showCriticalMsg()
{
    //critical消息框
    label->setText(tr("critical message box"));
    QMessageBox::critical(this, tr("critical消息框"), tr("这是一个critical消息框测试!"));
    return ;
}

20200303211937504.png

  • 消息对话框:about消息框QMessageBox::about()

 

static void about(QWidget *parent, //父窗口
          const QString &title, //对话框标题
          const QString &text);//标签提示字符串
void MsgBoxDlg::showAboutMsg()
{
    //about消息框
    label->setText(tr("about message box"));
    QMessageBox::about(this, tr("about消息框"), tr("这是一个about消息框测试!"));
    return ;
}

20200303211949210.png

  • 消息对话框:aboutQt消息框QMessageBox::aboutQt()

 

static void aboutQt(QWidget *parent, 
          const QString &title = QString());
void MsgBoxDlg::showAboutQtMsg()
{
    //about Qt消息框
    label->setText(tr("about message box"));
    QMessageBox::aboutQt(this, tr("about Qt消息框"));
    return ;
}

20200303211956696.png

实例实现

新建Qt项目“DialogExample”,基类选择“QDialog”,类名不变,取消“创建界面”复选框。


  • dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFrame>
#include "inputdlg.h"
#include "msgboxdlg.h"
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
private slots:
    //标准文件对话框槽函数
    void showFile();
    //标准颜色对话框槽函数
    void showColor();
    //标准字体对话框槽函数
    void showFont();
    //标准输入对话框槽函数
    void showInputDlg();
    //各种消息对话框槽函数
    void showMsgDlg();
    //自定义消息对话框槽函数
    void showCustomDlg();
private:
    //主体布局
    QGridLayout *mainLayout;
    //标准文件对话框
    QPushButton *fileBtn;
    QLineEdit *fileLineEdit;
    //标准颜色对话框
    QPushButton *colorBtn;
    QFrame *colorFrame;
    //标准字体对话框
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;
    //标准输入对话框
    InputDlg *inputDlg;
    QPushButton *inputBtn;
    //各种消息对话框
    QPushButton *MsgBtn;
    MsgBoxDlg *msgDlg;
    //自定义消息框
    QPushButton *CustomBtn;
    QLabel *label;
};
#endif // DIALOG_H


  • dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //设置窗口标题
    setWindowTitle(tr("各种标准对话框实例"));
    //标准文件对话框
    fileBtn = new QPushButton(tr("文件标准对话框实例"));
    fileLineEdit = new QLineEdit;
    //标准延时对话框
    colorBtn = new QPushButton(tr("标准颜色对话框实例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    //改变背景颜色
    colorFrame->setAutoFillBackground(true);
    //标准字体对话框
    fontBtn = new QPushButton(tr("标准字体对话框实例"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));
    //标准输入对话框
    inputBtn = new QPushButton(tr("标准输入对话框实例"));
    //各种消息对话框
    MsgBtn = new QPushButton(tr("标准消息对话框实例"));
    //自定义消息对话框
    CustomBtn = new QPushButton(tr("自定义消息对话框实例"));
    label = new QLabel;
    label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    //主体布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);
    mainLayout->addWidget(inputBtn, 3, 0);
    mainLayout->addWidget(MsgBtn, 3, 1);
    mainLayout->addWidget(CustomBtn, 4, 0);
    mainLayout->addWidget(label, 4, 1);
    //实现槽函数链接
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDlg()));
    connect(MsgBtn, SIGNAL(clicked()), this, SLOT(showMsgDlg()));
    connect(CustomBtn, SIGNAL(clicked()), this, SLOT(showCustomDlg()));
}
Dialog::~Dialog()
{
}
void Dialog::showFile()
{
    //设置标准文件对话框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}
void Dialog::showColor()
{
    //设置标准颜色对话框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}
void Dialog::showFont()
{
    //设置标准字体对话框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}
void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}
void Dialog::showMsgDlg()
{
    msgDlg = new MsgBoxDlg(this);
    msgDlg->show();
}
void Dialog::showCustomDlg()
{
    label->setText(tr("Custom Message Box"));
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(tr("用户自定义消息对话框"));
    QPushButton *yesBtn = customMsgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton(tr("No"), QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton(tr("Cancel"), QMessageBox::ActionRole);
    customMsgBox.setText(tr("这是一个用户自定义消息对话框!"));
    customMsgBox.setIconPixmap(QPixmap("Qt.png"));
    customMsgBox.exec();
    if(customMsgBox.clickedButton() == yesBtn)
        label->setText(tr("Custom message Box/yes"));
    if(customMsgBox.clickedButton() == noBtn)
        label->setText(tr("Custom message Box/no"));
    if(customMsgBox.clickedButton() == cancelBtn)
        label->setText(tr("Custom message Box/cancel"));
    return ;
}


  • 添加标准输入对话框的各项实例,添加新文件->C++类,命令为InpuDlg,继承自QDialog类,具体代码如下:
  • inputdlg.h
#ifndef INPUTDLG_H
#define INPUTDLG_H
#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QInputDialog>
class InputDlg : public QDialog
{
    Q_OBJECT
public:
    InputDlg(QWidget *parent = 0);
private slots:
    void ChangeName();
    void ChangeSex();
    void ChangeAge();
    void ChangeScore();
private:
    QLabel *nameLabel1;
    QLabel *sexLabel1;
    QLabel *ageLabel1;
    QLabel *scoreLabel1;
    QLabel *nameLabel2;
    QLabel *sexLabel2;
    QLabel *ageLabel2;
    QLabel *scoreLabel2;
    QPushButton *nameBtn;
    QPushButton *sexBtn;
    QPushButton *ageBtn;
    QPushButton *scoreBtn;
    QGridLayout *mainLayout;
};
#endif // INPUTDLG_H


  • inputdlg.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //设置窗口标题
    setWindowTitle(tr("各种标准对话框实例"));
    //标准文件对话框
    fileBtn = new QPushButton(tr("文件标准对话框实例"));
    fileLineEdit = new QLineEdit;
    //标准延时对话框
    colorBtn = new QPushButton(tr("标准颜色对话框实例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    //改变背景颜色
    colorFrame->setAutoFillBackground(true);
    //标准字体对话框
    fontBtn = new QPushButton(tr("标准字体对话框实例"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));
    //标准输入对话框
    inputBtn = new QPushButton(tr("标准输入对话框实例"));
    //各种消息对话框
    MsgBtn = new QPushButton(tr("标准消息对话框实例"));
    //自定义消息对话框
    CustomBtn = new QPushButton(tr("自定义消息对话框实例"));
    label = new QLabel;
    label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    //主体布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);
    mainLayout->addWidget(inputBtn, 3, 0);
    mainLayout->addWidget(MsgBtn, 3, 1);
    mainLayout->addWidget(CustomBtn, 4, 0);
    mainLayout->addWidget(label, 4, 1);
    //实现槽函数链接
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDlg()));
    connect(MsgBtn, SIGNAL(clicked()), this, SLOT(showMsgDlg()));
    connect(CustomBtn, SIGNAL(clicked()), this, SLOT(showCustomDlg()));
}
Dialog::~Dialog()
{
}
void Dialog::showFile()
{
    //设置标准文件对话框
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}
void Dialog::showColor()
{
    //设置标准颜色对话框
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}
void Dialog::showFont()
{
    //设置标准字体对话框
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}
void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}
void Dialog::showMsgDlg()
{
    msgDlg = new MsgBoxDlg(this);
    msgDlg->show();
}
void Dialog::showCustomDlg()
{
    label->setText(tr("Custom Message Box"));
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(tr("用户自定义消息对话框"));
    QPushButton *yesBtn = customMsgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton(tr("No"), QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton(tr("Cancel"), QMessageBox::ActionRole);
    customMsgBox.setText(tr("这是一个用户自定义消息对话框!"));
    customMsgBox.setIconPixmap(QPixmap("Qt.png"));
    customMsgBox.exec();
    if(customMsgBox.clickedButton() == yesBtn)
        label->setText(tr("Custom message Box/yes"));
    if(customMsgBox.clickedButton() == noBtn)
        label->setText(tr("Custom message Box/no"));
    if(customMsgBox.clickedButton() == cancelBtn)
        label->setText(tr("Custom message Box/cancel"));
    return ;
}

  • 最后添加消息对话框类的各项示例,添加新文件->选择C++类->命令为MsgBoxDlg,同样选择继承自QDialog,具体代码如下:
  • msgboxdlg.h
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include <QDialog>
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
class MsgBoxDlg : public QDialog
{
    Q_OBJECT
public:
    MsgBoxDlg(QWidget *parent = 0);
private slots:
    void showQuestionMsg();
    void showInformationMsg();
    void showWarningMsg();
    void showCriticalMsg();
    void showAboutMsg();
    void showAboutQtMsg();
private:
    QLabel *label;
    QPushButton *questionBtn;
    QPushButton *informationBtn;
    QPushButton *warningBtn;
    QPushButton *criticalBtn;
    QPushButton *aboutBtn;
    QPushButton *aboutQtBtn;
    QGridLayout *mainLayout;
};
#endif // MSGBOXDLG_H


  • msgboxdlg.cpp
#include "msgboxdlg.h"
MsgBoxDlg::MsgBoxDlg(QWidget *parent)
{
    setWindowTitle(tr("标准对话框实例"));
    label = new QLabel(tr("请选择一种消息框"));
    questionBtn = new QPushButton(tr("QusetionMsg"));
    informationBtn = new QPushButton(tr("InformationMsg"));
    warningBtn = new QPushButton(tr("warningMsg"));
    criticalBtn = new QPushButton(tr("criticalMsg"));
    aboutBtn = new QPushButton(tr("aboutMsg"));
    aboutQtBtn = new QPushButton(tr("aboutQtMsg"));
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(label, 0, 0, 1, 2);
    mainLayout->addWidget(questionBtn, 1, 0);
    mainLayout->addWidget(informationBtn, 1, 1);
    mainLayout->addWidget(warningBtn, 2, 0);
    mainLayout->addWidget(criticalBtn, 2, 1);
    mainLayout->addWidget(aboutBtn, 3, 0);
    mainLayout->addWidget(aboutQtBtn, 3, 1);
    connect(questionBtn, SIGNAL(clicked()), this , SLOT(showQuestionMsg()));
    connect(informationBtn, SIGNAL(clicked()), this , SLOT(showInformationMsg()));
    connect(warningBtn, SIGNAL(clicked()), this , SLOT(showWarningMsg()));
    connect(criticalBtn, SIGNAL(clicked()), this , SLOT(showCriticalMsg()));
    connect(aboutBtn, SIGNAL(clicked()), this , SLOT(showAboutMsg()));
    connect(aboutQtBtn, SIGNAL(clicked()), this , SLOT(showAboutQtMsg()));
}
void MsgBoxDlg::showQuestionMsg()
{
    //question消息对话框
    label->setText(tr("question message box"));
    switch (QMessageBox::question(this, tr("question消息框"), tr("您现在已经修改完成,是否结束程序"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)) {
    case QMessageBox::Ok:
        label->setText("Question button/OK");
        break;
    case QMessageBox::Cancel:
        label->setText("Question button/Cancel");
        break;
    default:
        break;
    }
    return ;
}
void MsgBoxDlg::showInformationMsg()
{
    //information消息框
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this, tr("information消息框"), tr("这个information消息框测试,欢迎您!"));
    return ;
}
void MsgBoxDlg::showWarningMsg()
{
    //warning消息框
    label->setText(tr("warning message box"));
    switch (QMessageBox::warning(this, tr("warning消息框"), tr("您修改的内容还未保存,是否要保存对文档的修改?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save)) {
    case QMessageBox::Save:
        label->setText("Warning button/Save");
        break;
    case QMessageBox::Discard:
        label->setText("Warning button/Discard");
        break;
    case QMessageBox::Cancel:
        label->setText("Warning button/Cancel");
        break;
    default:
        break;
    }
    return ;
}
void MsgBoxDlg::showCriticalMsg()
{
    //critical消息框
    label->setText(tr("critical message box"));
    QMessageBox::critical(this, tr("critical消息框"), tr("这是一个critical消息框测试!"));
    return ;
}
void MsgBoxDlg::showAboutMsg()
{
    //about消息框
    label->setText(tr("about message box"));
    QMessageBox::about(this, tr("about消息框"), tr("这是一个about消息框测试!"));
    return ;
}
void MsgBoxDlg::showAboutQtMsg()
{
    //about Qt消息框
    label->setText(tr("about message box"));
    QMessageBox::aboutQt(this, tr("about Qt消息框"));
    return ;
}


  • 最终实现:

主界面

20200303211557973.png

标准输入对话框界面

20200303211717486.png

标准消息对话框界面

20200303212020285.png

自定义消息对话框

20200303212115150.png

工具盒类

工具盒类又称为QToolBox,QToolBox提供了一种列状的层叠窗体,并且提供了一种快速访问命令或选择项的按钮,通常在工具条中使用。

抽屉效果是软件界面设计中的一种常用形式,可以以一种动态直观的方式在大小有限的界面上扩展出更多的功能。


示例“qq界面实例”

通过实现类似QQ抽屉效果的实例来介绍QToolBox类的使用。


实现步骤

  • 新建Qt widget application,项目名称为“MyQQExample”,基类选择“QDialog”,取消“创建界面”复选框选择。
  • 在项目上点右键添加新文件,选择“C++Class”选项,基类选择‘QToolBox’,输入类名称为“Drawer”。
  • 在drawer.h中添加以下代码
#ifndef DRAWER_H
#define DRAWER_H
#include <QObject>
#include <QToolBox>
#include <QToolButton>
#include <QGroupBox>
#include <QVBoxLayout>
class Drawer : public QToolBox
{
public:
    Drawer(QWidget *parent = 0, Qt::WindowFlags f = 0);
private:
    QToolButton *toolBtn1_1;
    QToolButton *toolBtn1_2;
    QToolButton *toolBtn1_3;
    QToolButton *toolBtn1_4;
    QToolButton *toolBtn1_5;
    QToolButton *toolBtn2_1;
    QToolButton *toolBtn2_2;
    QToolButton *toolBtn3_1;
    QToolButton *toolBtn3_2;
};
#endif // DRAWER_H


  • 在drawer.cpp中添加以下代码
#include "drawer.h"
Drawer::Drawer(QWidget *parent, Qt::WindowFlags f)
{
    //设置主窗口标题
    setWindowTitle(tr("My QQ"));
    toolBtn1_1 = new QToolButton;
    toolBtn1_1->setText(tr("张三"));
    toolBtn1_1->setIcon(QPixmap("11.png"));
    toolBtn1_1->setIconSize(QPixmap("11.png").size());
    toolBtn1_1->setAutoRaise(true);
    toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn1_2 = new QToolButton;
    toolBtn1_2->setText(tr("李四"));
    toolBtn1_2->setIcon(QPixmap("12.png"));
    toolBtn1_2->setIconSize(QPixmap("12.png").size());
    toolBtn1_2->setAutoRaise(true);
    toolBtn1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn1_3 = new QToolButton;
    toolBtn1_3->setText(tr("王五"));
    toolBtn1_3->setIcon(QPixmap("13.png"));
    toolBtn1_3->setIconSize(QPixmap("13.png").size());
    toolBtn1_3->setAutoRaise(true);
    toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn1_4 = new QToolButton;
    toolBtn1_4->setText(tr("小孙"));
    toolBtn1_4->setIcon(QPixmap("14.png"));
    toolBtn1_4->setIconSize(QPixmap("14.png").size());
    toolBtn1_4->setAutoRaise(true);
    toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn1_5 = new QToolButton;
    toolBtn1_5->setText(tr("小赵"));
    toolBtn1_5->setIcon(QPixmap("15.png"));
    toolBtn1_5->setIconSize(QPixmap("15.png").size());
    toolBtn1_5->setAutoRaise(true);
    toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    QGroupBox *groupBox1 = new QGroupBox;
    QVBoxLayout *layout1 = new QVBoxLayout(groupBox1);
    //布局中各窗体的显示间距
    layout1->setMargin(10);
    //布局中各窗体的显示位置,居中
    layout1->setAlignment(Qt::AlignCenter);
    //插入各个按钮
    layout1->addWidget(toolBtn1_1);
    layout1->addWidget(toolBtn1_2);
    layout1->addWidget(toolBtn1_3);
    layout1->addWidget(toolBtn1_4);
    layout1->addWidget(toolBtn1_5);
    //插入一个占位符
    layout1->addStretch();
    toolBtn2_1 = new QToolButton;
    toolBtn2_1->setText(tr("小王"));
    toolBtn2_1->setIcon(QPixmap("21.png"));
    toolBtn2_1->setIconSize(QPixmap("21.png").size());
    toolBtn2_1->setAutoRaise(true);
    toolBtn2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn2_2 = new QToolButton;
    toolBtn2_2->setText(tr("小张"));
    toolBtn2_2->setIcon(QPixmap("22.png"));
    toolBtn2_2->setIconSize(QPixmap("22.png").size());
    toolBtn2_2->setAutoRaise(true);
    toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    QGroupBox *groupBox2 = new QGroupBox;
    QVBoxLayout *layout2 = new QVBoxLayout(groupBox2);
    layout2->setMargin(10);
    layout2->setAlignment(Qt::AlignCenter);
    layout2->addWidget(toolBtn2_1);
    layout2->addWidget(toolBtn2_2);
    toolBtn3_1 = new QToolButton;
    toolBtn3_1->setText(tr("小陈"));
    toolBtn3_1->setIcon(QPixmap("31.png"));
    toolBtn3_1->setIconSize(QPixmap("31.png").size());
    toolBtn3_1->setAutoRaise(true);
    toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolBtn3_1 = new QToolButton;
    toolBtn3_1->setText(tr("小李"));
    toolBtn3_1->setIcon(QPixmap("32.png"));
    toolBtn3_1->setIconSize(QPixmap("32.png").size());
    toolBtn3_1->setAutoRaise(true);
    toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    QGroupBox *groupBox3 = new QGroupBox;
    QVBoxLayout *layout3 = new QVBoxLayout(groupBox3);
    layout3->setMargin(10);
    layout3->setAlignment(Qt::AlignCenter);
    layout3->addWidget(toolBtn3_1);
    layout3->addWidget(toolBtn3_2);
    //将准备好的抽屉插入Toolbox中
    this->addItem((QWidget *)groupBox1, tr("我的好友"));
    this->addItem((QWidget *)groupBox2, tr("陌生人"));
    this->addItem((QWidget *)groupBox3, tr("黑名单"));
}


  • 修改dialog.cpp中代码
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    drawer = new Drawer(this);
    drawer->show();
}
Dialog::~Dialog()
{
}


  • dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "drawer.h"
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
    Drawer *drawer;
};
#endif // DIALOG_H


界面效果


20200303222535171.png

20200303222546632.png

20200303222555209.png

相关文章
|
5月前
|
Linux iOS开发 MacOS
19 QT - 标准文件对话框
19 QT - 标准文件对话框
30 0
|
3月前
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
41 0
|
5月前
15 QT - 对话框QDialog概述
15 QT - 对话框QDialog概述
20 0
|
2月前
|
存储
QT基础入门——QMainWindow与对话框QDialog(三)
QT基础入门——QMainWindow与对话框QDialog(三)
52 0
QT基础入门——QMainWindow与对话框QDialog(三)
|
8月前
QT5基本对话框
QFileDialog类的几个静态函数见上表,用户通过这些函数可以很方便地定制 自己的文件对话框。其中,getOpenFileName()函数返回用户选择的文件名。但是当 用户在选择文件时,如果选择“取消”(Cancel),则返回一个空串。在此仅详细说 明getOpenFileName()静态函数中各个参数的作用,其他文件对话框类中相关的静态函数 的参数有与其类似之处。
29 0
QT5基本对话框
|
4月前
|
数据安全/隐私保护
QT基础教程(对话框2)
QT基础教程(对话框2)
32 0
|
4月前
QT基础教程(对话框1)
QT基础教程(对话框1)
33 0
|
4月前
|
C++
C++ Qt开发:自定义Dialog对话框组件
在之前的文章中笔者已经为大家展示了默认`Dialog`组件的使用方法,虽然内置组件支持对数据的输入,但有时候我们需要一次性输入多个数据,此时如果之使用默认模态对话框似乎不太够用,此时我们需要自己创建一个自定义对话框,需要说明的是此类对话框也是一种窗体,所以可以在其上面放置任何通用组件,以实现更多复杂的开发需求。自定义对话框需要解决的问题是,如何让父窗体与子窗体进行数据交换,要实现数据的交换有两种方式,第一种方式是通过动态加载模态对话框,当用户点击确定后通过`GetValue()`来拿到数据,而第二种方式则是通过发送信号的方式将数据投递给父窗体,这两种方式都可以,读者可根据自身需求来选择不同的通
39 1
C++ Qt开发:自定义Dialog对话框组件
|
4月前
|
数据安全/隐私保护 C++ 开发者
C++ Qt开发:标准Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍标准对话框`QInputDialog`、`QFileDialog `这两种对话框组件的常用方法及灵活运用。在 Qt 中,标准对话框提供了一些常见的用户交互界面,用于执行特定任务,例如获取用户输入、选择文件路径、显示消息等。这些对话框通常具有标准化的外观和行为,使得在不同的平台上能够保持一致性。在一般的开发过程中,标准对话框是开发者常用的工具之一。
50 1
C++ Qt开发:标准Dialog对话框组件
|
5月前
|
API
18 QT - 消息对话框
18 QT - 消息对话框
24 0

热门文章

最新文章

推荐镜像

更多