标准基本对话框应用
通过一个实例来介绍Qt5中对于各类对话框的应用。这个实例中具有以下这几种QT的标准对话框:
标准文件对话框(QFileDialog)
标准颜色对话框(QColorDialog)
标准字体对话框(QFontDialog)
标准输入对话框(QInputDialog)
标准消息对话框(QMessageBox)
效果展示
各种基本对话框都是通过调用各自不同的静态函数来完成其功能。具体如下:
类 | 类说明 | 静态函数 | 函数说明 |
---|---|---|---|
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消息框 |
函数详细介绍看Qt帮助文档
源码如下
文件分组
dialog.h
主界面头文件以及信号槽的定义
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include "inputdialog.h"
#include "msgboxdlg.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
private:
QLineEdit* filePathEdit;
QPushButton* fileBtn;
QPushButton* colorBtn;
QGridLayout* mainLayout;
QFrame* colorFrame;
QPushButton* fontBtn;
QLineEdit* fontLineEdit;
QPushButton* inputBtn;
QPushButton* infoBtn;
InputDlog* indialog;
MsgBoxDlg* msgbox;
QPushButton* customBtn;
QLabel* label;
private slots:
void showFile();
void showcolor();
void showfont();
void showInput();
void showMsgBox();
void showCustom();
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp文件
界面的基本布局
#include "dialog.h"
#include "ui_dialog.h"
#include "inputdialog.h"
#include <QColorDialog>
#include <QFileDialog>
#include <QFontDialog>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
setWindowTitle("各种标准对话框示例");
fileBtn = new QPushButton("文件标准对话框实例");
filePathEdit = new QLineEdit;
mainLayout = new QGridLayout(this);
mainLayout->addWidget(fileBtn,0,0);
mainLayout->addWidget(filePathEdit,0,1);
connect(fileBtn,&QPushButton::clicked,this,&Dialog::showFile);
colorBtn = new QPushButton("颜色标准对话框实例");
colorFrame = new QFrame;
colorFrame->setFrameShape(QFrame::StyledPanel);//设置框架的形状
colorFrame->setAutoFillBackground(true);//启用背景颜色填充
mainLayout->addWidget(colorBtn,1,0);
mainLayout->addWidget(colorFrame,1,1);
connect(colorBtn,&QPushButton::clicked,this,&Dialog::showcolor);
//标准字体对话框
fontBtn = new QPushButton("标准字体对话框");
fontLineEdit = new QLineEdit("Welcome");
mainLayout->addWidget(fontBtn,2,0);
mainLayout->addWidget(fontLineEdit,2,1);
connect(fontBtn,&QPushButton::clicked,this,&Dialog::showfont);
//标准输入对话框/标准消息对话框
inputBtn = new QPushButton("标准输入对话框");
infoBtn = new QPushButton("标准消息对话框");
mainLayout->addWidget(inputBtn,3,0);
mainLayout->addWidget(infoBtn,3,1);
connect(inputBtn,&QPushButton::clicked,this,&Dialog::showInput);
connect(infoBtn,&QPushButton::clicked,this,&Dialog::showMsgBox);
//用户自定义对话框
customBtn = new QPushButton("用户自定义对话框实例");
label = new QLabel;
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
mainLayout->addWidget(customBtn,4,0);
mainLayout->addWidget(label,4,1);
connect(customBtn,&QPushButton::clicked,this,&Dialog::showCustom);
}
void Dialog::showFile()
{
QString s = QFileDialog::getOpenFileName(this,"opne file dialog","/","(*.txt *.cpp *.h)");
filePathEdit->setText(s);
}
void Dialog::showcolor()
{
//获取颜色对话框,初始颜色问red 返回为获取到的颜色Qcolor
QColor c = QColorDialog::getColor(Qt::red);
if(c.isValid())//颜色有效返回ture否则返回false
{
colorFrame->setPalette(c);
}
}
void Dialog::showfont()
{
bool ok;
QFont f = QFontDialog::getFont(&ok);
if(ok){
fontLineEdit->setFont(f);
}
}
void Dialog::showInput()
{
indialog = new InputDlog();
indialog->show();
}
void Dialog::showMsgBox()
{
msgbox = new MsgBoxDlg();
msgbox->show();
}
void Dialog::showCustom()
{
QMessageBox customBox;
customBox.setWindowTitle("用户自定义消息框");
QPushButton* YesBtn = customBox.addButton("Yes",QMessageBox::ActionRole);
QPushButton* NoBtn = customBox.addButton("No",QMessageBox::ActionRole);
QPushButton* CancelBtn = customBox.addButton(QMessageBox::Cancel);
customBox.setText("这是进击着周星星");
customBox.setIconPixmap(QPixmap("QT.png"));
customBox.exec();//事件循环 显示此消息框
if(customBox.clickedButton() == YesBtn)
{
label->setText("Custom Message Yes");
}
if(customBox.clickedButton() == NoBtn)
{
label->setText("Custom Message NO");
}
if(customBox.clickedButton() == CancelBtn)
{
label->setText("Custom Message cancelBtn");
}
}
Dialog::~Dialog()
{
delete ui;
}
输入对话框
基本布局以及事件绑定
对应的头文件inputdialog.h
#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QDialog>
class InputDlog : public QDialog
{
Q_OBJECT
public:
InputDlog(QWidget* parent = 0);
private slots:
void changeName();
void changeSex();
void changeAge();
void changeScore();
private:
QLabel* nameLabel;
QLabel* sexLabel;
QLabel* ageLabel;
QLabel* scoreLabel;
QLabel* nameLabel1;
QLabel* sexLabel1;
QLabel* ageLabel1;
QLabel* scoreLabel1;
QLabel* nameLabel2;
QPushButton* nameBtn;
QPushButton* sexBtn;
QPushButton* ageBtn;
QPushButton* scoreBtn;
};
#endif // INPUTDIALOG_H
对应的cpp文件inputdialog.cpp
#include "inputdialog.h"
#include <QDebug>
#include <QInputDialog>
#include <QString>
InputDlog::InputDlog(QWidget* parent):QDialog(parent)
{
setWindowTitle("标准输入框实例");
nameLabel = new QLabel("姓名");
nameLabel1 = new QLabel("李四");
// nameLabel1->setText(tr("李四"));
nameLabel1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
nameBtn = new QPushButton("修改姓名");
sexLabel = new QLabel("性别");
sexLabel1 = new QLabel("男");
sexLabel1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
sexBtn = new QPushButton("修改性别");
ageLabel = new QLabel("年龄");
ageLabel1 = new QLabel("25");
ageLabel1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
ageBtn = new QPushButton("修改年龄");
scoreLabel = new QLabel("分数");
scoreLabel1 = new QLabel("59");
scoreLabel1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
scoreBtn = new QPushButton("修改分数");
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(nameLabel,0,0);
mainLayout->addWidget(nameLabel1,0,1);
mainLayout->addWidget(nameBtn,0,2);
mainLayout->addWidget(sexLabel,1,0);
mainLayout->addWidget(sexLabel1,1,1);
mainLayout->addWidget(sexBtn,1,2);
mainLayout->addWidget(ageLabel,2,0);
mainLayout->addWidget(ageLabel1,2,1);
mainLayout->addWidget(ageBtn,2,2);
mainLayout->addWidget(scoreLabel,3,0);
mainLayout->addWidget(scoreLabel1,3,1);
mainLayout->addWidget(scoreBtn,3,2);
mainLayout->setMargin(15);//设置控件之间的间距
mainLayout->setSpacing(10);//水平垂直间距
connect(nameBtn,&QPushButton::clicked,this,&InputDlog::changeName);
connect(sexBtn,&QPushButton::clicked,this,&InputDlog::changeSex);
connect(ageBtn,&QPushButton::clicked,this,&InputDlog::changeAge);
connect(scoreBtn,&QPushButton::clicked,this,&InputDlog::changeScore);
}
void InputDlog::changeName()
{
bool ok;
QString text = QInputDialog::getText(this,tr("标准字符串输入对话框"),
tr("请输入姓名:"),
QLineEdit::Normal,
nameLabel1->text(),
&ok);
if(ok && !text.isEmpty())
{
nameLabel1->setText(text);
}
}
void InputDlog::changeSex()
{
QStringList SexItems;
SexItems<<tr("男")<<tr("女");
bool ok;
QString SexItem = QInputDialog::getItem(this,tr("标准条目对话框"),
"请选择性别:",
SexItems,0,false,&ok);
if(ok && !SexItem.isEmpty())
{
sexLabel1->setText(SexItem);
}
}
void InputDlog::changeAge()
{
bool ok;
int age = QInputDialog::getInt(this,"年龄修改对话框",
"请输入年龄:",
ageLabel1->text().toInt(&ok),
0,100,1,&ok);
if(ok){
ageLabel1->setText(QString(tr("%1").arg(age)));
}
}
void InputDlog::changeScore()
{
bool ok;
double score = QInputDialog::getDouble(this,tr("标准double类型输入对话框"),
"请输入成绩:",
scoreLabel1->text().toDouble(&ok),
0,100,1,&ok);
if(ok){
scoreLabel1->setText(QString(tr("%1")).arg(score));
}
}
标准消息对话框
标准消息对话框事件绑定以及基本布局
MsgBoxDlg.h
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
class MsgBoxDlg : public QDialog
{
Q_OBJECT
public:
MsgBoxDlg();
private:
QLabel* infoText;
QPushButton* questionBtn;
QPushButton* infomationBtn;
QPushButton* warningBtn;
QPushButton* GriticalBtn;
QPushButton* aboutBtn;
QPushButton* aboutQtBtn;
QGridLayout* infomationLayout;
QPushButton* CustomBtn;
QLabel* label;
private slots:
void showQuestionMgs();
void showInfomationMgs();
void showWarningMgs();
void showgriticalMgs();
void showaboutMgs();
void showaboutQtMgs();
};
#endif // MSGBOXDLG_H
MsgBoxDlg.cpp
#include "msgboxdlg.h"
#include <qmessagebox.h>
MsgBoxDlg::MsgBoxDlg()
{
setWindowTitle(tr("标准消息对话框实例"));
infoText = new QLabel("请选择一种消息框");
questionBtn = new QPushButton("questionMsg");
infomationBtn = new QPushButton("infomationMsg");
warningBtn = new QPushButton("warningMgs");
GriticalBtn = new QPushButton("GriticalMsg");
aboutBtn = new QPushButton("aboutMsg");
aboutQtBtn = new QPushButton("aboutRtMsg");
infomationLayout = new QGridLayout(this);
infomationLayout->addWidget(infoText,0,0);
infomationLayout->addWidget(questionBtn,1,0);
infomationLayout->addWidget(infomationBtn,1,1);
infomationLayout->addWidget(warningBtn,2,0);
infomationLayout->addWidget(GriticalBtn,2,1);
infomationLayout->addWidget(aboutBtn,3,0);
infomationLayout->addWidget(aboutQtBtn,3,1);
//绑定事件
connect(questionBtn,&QPushButton::clicked,this,&MsgBoxDlg::showQuestionMgs);
connect(infomationBtn,&QPushButton::clicked,this,&MsgBoxDlg::showInfomationMgs);
connect(warningBtn,&QPushButton::clicked,this,&MsgBoxDlg::showWarningMgs);
connect(GriticalBtn,&QPushButton::clicked,this,&MsgBoxDlg::showgriticalMgs);
connect(aboutBtn,&QPushButton::clicked,this,&MsgBoxDlg::showaboutMgs);
connect(aboutQtBtn,&QPushButton::clicked,this,&MsgBoxDlg::showaboutQtMgs);
}
void MsgBoxDlg::showQuestionMgs()
{
switch (QMessageBox::question(this,"questio消息框","修改完成是否结束?",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok)) {
case QMessageBox::Ok:infoText->setText(tr("question btn/ok"));break;
case QMessageBox::Cancel:infoText->setText("queston btn/cancel");break;
default:break;
}
return;
}
void MsgBoxDlg::showInfomationMgs()
{
QMessageBox:: StandardButton result = QMessageBox::information(this,"infonmation消息框","come on come on!",
QMessageBox::Ok,
QMessageBox::Ok);
if(QMessageBox::Ok == result)
{
infoText->setText("infomation /OK");
}
return;
}
void MsgBoxDlg::showWarningMgs()
{
switch (QMessageBox::warning(this,"questio消息框",
"是否保存?",
QMessageBox::Discard | QMessageBox::Save,
QMessageBox::Cancel,QMessageBox::Save)) {
case QMessageBox::Discard:infoText->setText(tr("question btn/discard"));break;
case QMessageBox::Cancel:infoText->setText("queston btn/cancel");break;
case QMessageBox::Save:infoText->setText("queston btn/Save");break;
default:break;
}
return;
}
void MsgBoxDlg::showgriticalMgs()
{
QMessageBox::critical(this,tr("critical消息框"),"这是一个测试!");
return;
}
void MsgBoxDlg::showaboutMgs()
{
QMessageBox::about(this,tr("about消息框"),"这是一个测试about!");
return;
}
void MsgBoxDlg::showaboutQtMgs()
{
QMessageBox::aboutQt(this,"这是一个测试about QT!");
return;
}
总结
基本对话框属于QT最基本是比较容易的功能,也是比较常用的,建议熟练掌握,各个静态函数的参数大同小异,可以参考QT帮助文档。