Qt5基本对话框

项目一DialogExample
首先介绍标准文件对话框( QFileDialog )、标准颜色对话框( QColorDialog )、标准字体对话框( QFontDialog )、标准输入对话框( QInputDialog )及消息对话框( QMessageBox ),运行效果如图所示


各种基本对话框通过调用各自不同的静态函数来完成其功能,具体说明见表



其函数形式如下:
QString QFileDialog::getOpenFileName
  QWidget* parent=0,               //标准文件对话框的父窗口
  const QString & caption=QString(),  //标准文件对话框的标题名
  const QString & dir=QString(),    //注(1)
  const QString & filter=QString(),  //注(2)
  QString * selectedFilter=0,   //用户选择的过滤器通过此参数返回
  Options options=0         //选择显示文件名的格式,默认是同时显示目录与文件名
)

打开一个文件保存路径
void Dialog::showFile()
{
    QString s = QFileDialog::getOpenFileName(this,"open file dialog","/",
                      "C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
    fileLineEdit->setText(s);
}

getColor()函数是标准颜色对话框QColorDialog类的一个静态函数,该函数返回用户选择的颜色值,下面是getColor()函数形式:
QColor getColor
(
  const QColor& initial=Qt::white,     //注
  QWidget* parent=0               //标准颜色对话框的父窗口

getFont () 函数是标准字体对话框 QFontDialog 类的一个静态函数,该函数返回用户所选择的字体,下面是 getFont () 函数形式:
QFont  getFont
(
   bool * ok,              //
   QWidget * parent=0      // 标准字体对话框的父窗口
);     

标准字符串输入对话框是通过 QInputDialog 类的静态函数 getText () 来完成的, getText () 函数形式如下:
QStringgetText
(
  QWidget* parent,            //标准输入对话框的父窗口
  const QString& title,          //标准输入对话框的标题名
  const QString& label,          //标准输入对话框的标签提示
  QLineEdit::EchoMode mode=QLineEdit::Normal,
  //指定标准输入对话框中QLineEdit控件的输入
    模式
  const QString& text=QString(),  //标准字符串输入对话框弹出时QLineEdit控件
    中默认出现的文字
  bool* ok=0,                   //注
  Qt::WindowFlagsflags=0       //指明标准输入对话框的窗体标识
);   

标准条目选择对话框是通过QInputDialog类的静态函数getItem()来完成的,getItem()函数形式如下:
QString getItem
(
  QWidget* parent,                 //标准输入对话框的父窗口
  const QString& title,            //标准输入对话框的标题名
  const QString& label,            //标准输入对话框的标签提示
  const QStringList& items,       //注(1)
  intcurrent=0,                    //注(2)
  booleditable=true,                //指定QComboBox控件中显示的文字是否可编辑
  bool* ok=0,                     //注(3)
  Qt::WindowFlagsflags=0        //指明标准输入对话框的窗口标识
);

标准int类型输入对话框是通过QInputDialog类的静态函数getInt()来完成的,getInt()函数形式如下:
int getInt
(
  QWidget* parent,              //标准输入对话框的父窗口
  const QString& title,         //标准输入对话框的标题名
  const QString& label,         //标准输入对话框的标签提示
  int value=0,                    //指定标准输入对话框中QSpinBox控件的默认显示值
  int min=-2147483647,          //指定QSpinBox控件的数值范围
  int max=2147483647,
  int step=1,                     //指定QSpinBox控件的步进值
  bool* ok=0,                     //注
  Qt::WindowFlagsflags=0      //指明标准输入对话框的窗口标识
);


标准double类型输入对话框是通过QInputDialog类的静态函数getDouble()来完成的,getDouble()函数形式如下:
double getDouble
(
  QWidget* parent,               //标准输入对话框的父窗口
  const QString& title,         //标准输入对话框的标题名
  const QString& label,     //标准输入对话框的标签提示
  double value=0,                //指定标准输入对话框中QSpinBox控件默认的显示值
  double min=-2147483647,       //指定QSpinBox控件的数值范围
  double max=2147483647,
  intdecimals=1,                //指定QSpinBox控件的步进值
  bool* ok=0,                    //注
  Qt::WindowFlagsflags=0       //指明标准输入对话框的窗口标识
);

Question消息框使用QMessageBox::question()函数完成,此函数形式如下:
StandardButton QMessageBox::question
(
  QWidget* parent,                                   //消息框的父窗口指针
  const QString& title,                               //消息框的标题栏
  const QString& text,                                //消息框的文字提示信息
  StandardButtons buttons=Ok,                         //注(1)
  StandardButton defaultButton=NoButton             //注(2)
);

Information消息框使用QMessageBox::information()函数完成,函数形式如下:
StandardButton QMessageBox::information
(
  QWidget*parent,                     //消息框的父窗口指针
  const QString& title,             //消息框的标题栏
  const QString& text,               //消息框的文字提示信息
  StandardButtonsbuttons=Ok,        //同Question消息框的注释内容
  StandardButton defaultButton=NoButton  //同Question消息框的注释内容
);

Warning消息框使用QMessageBox::warning()函数完成,函数形式如下:
StandardButton QMessageBox::warning
(
  QWidget* parent,                           //消息框的父窗口指针
  const QString& title,                      //消息框的标题栏
  const QString& text,                       //消息框的文字提示信息
  StandardButtonsbuttons=Ok,              //同Question消息框的注释内容
  StandardButton defaultButton=NoButton    //同Question消息框的注释内容
);

Critical消息框使用QMessageBox::critical()函数完成,函数形式如下:
StandardButton QMessageBox::critical
(
  QWidget* parent,                        //消息框的父窗口指针
  const QString& title,                   //消息框的标题栏
  const QString& text,                   //消息框的文字提示信息
  StandardButtonsbuttons=Ok,           //同Question消息框的注释内容
  StandardButton defaultButton=NoButton  //同Question消息框的注释内容
);

About消息框使用QMessageBox::about()函数完成,函数形式如下:
void QMessageBox::about
(
  QWidget* parent,             //消息框的父窗口指针
  const QString& title,           //消息框的标题栏
  const QString& text          //消息框的文字提示信息
);

About Qt消息框使用QMessageBox:: aboutQt()函数完成,函数形式如下:
void QMessageBox::aboutQt
(
  QWidget* parent,                   //消息框的父窗口指针
  const QString& title=QString()        //消息框的标题栏
);

main.cpp

#include "dialog.h"
#include <QApplication>
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
 
 
    return a.exec();
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H
 
 
#include <QDialog>
#include <QLineEdit>
#include <QGridLayout>
#include <QLabel>
#include "inputdlg.h"
#include "msgboxdlg.h"
 
 
 
 
class Dialog : public QDialog
{
    Q_OBJECT
 
 
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton *fileBtn;
    QLineEdit *fileLineEdit;
    QPushButton *colorBtn;
    QFrame *colorFrame;
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;
    QPushButton *inputBtn;
    InputDlg *inputDlg;
    QPushButton *MsgBtn;
    MsgBoxDlg *msgDlg;
    QPushButton *CustomBtn;
    QLabel *label;
    QGridLayout *mainLayout;
 
 
private slots:
    void showFile();
    void showColor();
    void showFont();
    void showInputDlg();
    void showMsgDlg();
    void showCustomDlg();
};
 
#endif   //   DIALOG_H
dialog.cpp
#include "dialog.h"
#include <QFileDialog>
#include <QColorDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QMessageBox>
#include <QPushButton>
 
 
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle (tr("各种标准对话框实例"));
 
 
    fileBtn = new QPushButton(tr("文件标准对话框实例"));
    fileLineEdit = new QLineEdit;
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget (fileBtn, 0, 0);
    mainLayout->addWidget (fileLineEdit, 0, 1);
    connect (fileBtn, SIGNAL(clicked(bool)), this, SLOT(showFile()));
 
 
    colorBtn = new QPushButton(tr("颜色标准对话框实例"));
    colorFrame = new QFrame;
    //colorFrame用于根据用户选择的不同颜色更新不同的背景色
    colorFrame->setFrameShape (QFrame::Box);
    colorFrame->setAutoFillBackground (true);
    mainLayout->addWidget (colorBtn, 1, 0);
    mainLayout->addWidget (colorFrame, 1, 1);
    connect (colorBtn, SIGNAL(clicked(bool)), this, SLOT(showColor()));
 
 
    fontBtn = new QPushButton(tr("字体标准对话框实例"));
    fontLineEdit = new QLineEdit(tr("Welcome"));
    mainLayout->addWidget (fontBtn, 2, 0);
    mainLayout->addWidget (fontLineEdit, 2, 1);
    connect (fontBtn, SIGNAL(clicked(bool)), this, SLOT(showFont()));
 
 
    inputBtn = new QPushButton(tr("标准输入对话框实例"));
    mainLayout->addWidget (inputBtn, 3, 0);
    connect (inputBtn, SIGNAL(clicked(bool)), this, SLOT(showInputDlg()));
 
 
    MsgBtn = new QPushButton(tr("标准消息对话框实例"));
    mainLayout->addWidget (MsgBtn, 3, 1);
    connect (MsgBtn, SIGNAL(clicked(bool)), SLOT(showMsgDlg()));
 
 
    CustomBtn = new QPushButton(tr("用户定义消息对话框实例"));
    label = new QLabel;
    label->setFrameStyle (QFrame::Panel | QFrame::Sunken);
    mainLayout->addWidget (CustomBtn, 4, 0);
    mainLayout->addWidget (label, 4, 1);
    connect (CustomBtn, SIGNAL(clicked(bool)), 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 ())
    {
        //setPalette()设置调色板QPalette()获取选择的颜色
        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();
    msgDlg->show ();
}
 
 
void Dialog::showCustomDlg ()
{
    label->setText (tr("Custom Message Box"));
 
 
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle (tr("用户自定义消息框"));
    /*addButton()第一个参数为按钮显示的文字,第二个参数为按钮类型的描述。该函数默认
     *先后顺序在消息框中由左至右依次插入按钮。Cancel是一个标准按钮,不用写按钮的名字*/
    QPushButton *yesBtn = customMsgBox.addButton (tr("yes"), QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton (tr("No"), QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton (QMessageBox::Cancel);
 
 
    customMsgBox.setText (tr("这是一个用户自定义消息框"));
    customMsgBox.setIconPixmap (QPixmap("Qt.png"));
    customMsgBox.exec ();       //显示此自定义消息框
 
 
    if(customMsgBox.clickedButton () == yesBtn)
    {
        label->setText ((tr("Custom Message Box/Yes")));
    }
    else if(customMsgBox.clickedButton () == noBtn)
    {
        label->setText (tr("Custom Message Box/No"));
    }
    else if(customMsgBox.clickedButton () == cancelBtn)
    {
        label->setText (tr("Custom Message Box/Cancel"));
    }
}

inputdlg.h

#ifndef INPUTDLG_H
#define INPUTDLG_H
 
 
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
 
 
class InputDlg : public QDialog
{
    Q_OBJECT
public:
    explicit InputDlg(QDialog *parent = 0);
 
 
private:
    QLabel *nameLabel1;
    QLabel *nameLabel2;
    QLabel *sexLabel1;
    QLabel *sexLabel2;
    QLabel *ageLabel1;
    QLabel *ageLabel2;
    QLabel *scoreLabel1;
    QLabel *scoreLabel2;
    QPushButton *nameBtn;
    QPushButton *sexBtn;
    QPushButton *ageBtn;
    QPushButton *scoreBtn;
    QGridLayout *mainLayout;
private slots:
    void changeName();
    void changeSex();
    void changeAge();
    void changeScore();
};
 
#endif   //   INPUTDLG_H
inputdlg.cpp
#include "inputdlg.h"
#include <QInputDialog>
 
 
InputDlg::InputDlg(QDialog *parent) : QDialog(parent)
{
    setWindowTitle (tr("标准输入对话框实例"));
 
 
    nameLabel1 = new QLabel(tr("姓名"));
    nameLabel2 = new QLabel(tr("孔杰"));
    nameLabel2->setFrameStyle (QFrame::Sunken | QFrame::Panel);
    nameBtn = new QPushButton(tr("修改姓名"));
 
 
    sexLabel1 = new QLabel(tr("性别"));
    sexLabel2 = new QLabel(tr("男"));
    sexLabel2->setFrameStyle (QFrame::Sunken | QFrame::Panel);
    sexBtn = new QPushButton(tr("修改性别"));
 
 
    ageLabel1 = new QLabel(tr("年龄"));
    ageLabel2 = new QLabel(tr("20"));
    ageLabel2->setFrameStyle (QFrame::Sunken | QFrame::Panel);
    ageBtn = new QPushButton(tr("修改年龄"));
 
 
    scoreLabel1 = new QLabel(tr("成绩"));
    scoreLabel2 = new QLabel(tr("80"));
    scoreLabel2->setFrameStyle (QFrame::Sunken | QFrame::Panel);
    scoreBtn = new QPushButton(tr("修改成绩"));
 
 
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget (nameLabel1, 0, 0);
    mainLayout->addWidget (nameLabel2, 0, 1);
    mainLayout->addWidget (nameBtn, 0, 2);
 
 
    mainLayout->addWidget (sexLabel1, 1, 0);
    mainLayout->addWidget (sexLabel2, 1, 1);
    mainLayout->addWidget (sexBtn, 1, 2);
 
 
    mainLayout->addWidget (ageLabel1, 2, 0);
    mainLayout->addWidget (ageLabel2, 2, 1);
    mainLayout->addWidget (ageBtn, 2, 2);
 
 
    mainLayout->addWidget (scoreLabel1, 3, 0);
    mainLayout->addWidget (scoreLabel2, 3, 1);
    mainLayout->addWidget (scoreBtn, 3, 2);
 
 
    connect (nameBtn, SIGNAL(clicked(bool)), this, SLOT(changeName()));
    connect (sexBtn, SIGNAL(clicked(bool)), this, SLOT(changeSex()));
    connect (ageBtn, SIGNAL(clicked(bool)), this, SLOT(changeAge()));
    connect (scoreBtn, SIGNAL(clicked(bool)), this, SLOT(changeScore()));
 
 
}
 
 
void InputDlg::changeName ()
{
    bool ok;
    QString text = QInputDialog::getText (this, tr("标准字符串输入对话框"),
        tr("请输入姓名:"), QLineEdit::Normal, nameLabel2->text (), &ok);
    if(ok && !text.isEmpty ())
    {
        nameLabel2->setText (text);
    }
}
 
 
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);
    }
}
 
 
void InputDlg::changeAge ()
{
    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));
    }
}
 
 
void InputDlg::changeScore ()
{
    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));
}


msgboxdlg.h

#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
 
 
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
 
 
class MsgBoxDlg : public QDialog
{
    Q_OBJECT
public:
    explicit MsgBoxDlg(QDialog *parent = 0);
 
 
private:
    QLabel *label;
    QPushButton *questionBtn;
    QPushButton *informationBtn;
    QPushButton *warningBtn;
    QPushButton *criticalBtn;
    QPushButton *aboutBtn;
    QPushButton *aboutQtBtn;
    QGridLayout *mainLayout;
 
 
public slots:
    void showQuestionMsg();
    void showInformationMsg();
    void showWarningMsg();
    void showCriticalMsg();
    void showAboutMsg();
    void showAboutQtMsg();
};
 
#endif   //   MSGBOXDLG_H
msgboxdlg.cpp
#include "msgboxdlg.h"
#include <QMessageBox>
 
 
MsgBoxDlg::MsgBoxDlg(QDialog *parent) : QDialog(parent)
{
    setWindowTitle (tr("标准消息对话框的实例"));
    label = new QLabel(tr("请选择一种消息框"));
    questionBtn = new QPushButton(tr("QuestionMsg"));
    informationBtn = new QPushButton(tr("InformationMsg"));
    warningBtn = new QPushButton(tr("WarningMsg"));
    criticalBtn = new QPushButton(tr("CriticalMsg"));
    aboutBtn = new QPushButton(tr("AboutMsg"));
    aboutQtBtn = new QPushButton(tr("AboutQtMse"));
 
 
    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(bool)), this, SLOT(showQuestionMsg()));
    connect (informationBtn, SIGNAL(clicked(bool)), this, SLOT(showInformationMsg()));
    connect (warningBtn, SIGNAL(clicked(bool)), this, SLOT(showWarningMsg()));
    connect (criticalBtn, SIGNAL(clicked(bool)), this, SLOT(showCriticalMsg()));
    connect (aboutBtn, SIGNAL(clicked(bool)), this, SLOT(showAboutMsg()));
    connect (aboutQtBtn, SIGNAL(clicked(bool)), this, SLOT(showAboutQtMsg()));
 
 
}
 
 
void MsgBoxDlg::showQuestionMsg ()
{
    label->setText (tr("Question Message Box"));
 
 
    switch(QMessageBox::question (this, tr("Question消息框"),
        tr("您现在已经修改完成,是否要结束程序?"),
        QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok))
    {
    case QMessageBox::Ok:
        label->setText (tr("Question button/ok"));
        break;
    case QMessageBox::Cancel:
        label->setText (tr("Question button/Cancel"));
        break;
    default:
        break;
    }
    return ;
}
 
 
void MsgBoxDlg::showInformationMsg ()
{
    label->setText (tr("Information Message Box"));
    QMessageBox::information (this, tr("Information消息框"),
                              tr("这是Information消息框,欢迎您!"));
    return;
}
 
 
void MsgBoxDlg::showWarningMsg ()
{
    label->setText (tr("Warning Message Box"));
 
 
    switch(QMessageBox::warning (this, tr("Warning消息框"),
        tr("修改的内容还未保存,是否要保存对文档的修改?"),
        QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel))
    {
    case QMessageBox::Save:
        label->setText (tr("Waring button/save"));
        break;
    case QMessageBox::Discard:
        label->setText (tr("Warning button/discard"));
        break;
    case QMessageBox::Cancel:
        label->setText (tr("Warning button/cancel"));
        break;
    default:
        break;
    }
    return;
}
 
 
void MsgBoxDlg::showCriticalMsg ()
{
    label->setText (tr("Critical Message Box"));
    QMessageBox::critical (this, tr("Critical消息框"),
        tr("这是一个Critical消息框测试"));
    return;
}
 
 
void MsgBoxDlg::showAboutMsg ()
{
    label->setText (tr("About Message Box"));
    QMessageBox::about (this, tr("About消息框"), tr("这是一个About消息框测试"));
    return;
}
 
 
void MsgBoxDlg::showAboutQtMsg ()
{
    label->setText (tr("About Qt Message Box"));
    QMessageBox::aboutQt (this, tr("About Qt消息框"));
    return;
}











 

 


 


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值