QT标准对话框

image-20220123114821788

示例:

  • 消息框

image-20220123223658903

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
#include "msgboxdlg.h"
#include<QMessageBox>
MsgBoxDlg::MsgBoxDlg(QWidget* parent ):QDialog(parent)
{
setWindowTitle("标准信息对话框集合");
//构造部件
m_tipLabel = new QLabel("请选择一种消息框");
m_questionBtn = new QPushButton("问题消息框");
m_infomationBtn= new QPushButton("信息消息框");
m_warningBtn = new QPushButton("警告消息框");
m_criticalBtn = new QPushButton("错误消息框");
m_aboutBtn = new QPushButton("关于消息框");
m_aboutQtBtn = new QPushButton("关于Qt消息框");
//布局界面
m_mainLayout = new QGridLayout(this);
m_mainLayout->addWidget(m_tipLabel,0,0,1,2);
m_mainLayout->addWidget(m_questionBtn,1,0);
m_mainLayout->addWidget(m_infomationBtn,1,1);
m_mainLayout->addWidget(m_warningBtn,2,0);
m_mainLayout->addWidget(m_criticalBtn,2,1);
m_mainLayout->addWidget(m_aboutBtn,3,0);
m_mainLayout->addWidget(m_aboutQtBtn,3,1);

connect(m_questionBtn,&QPushButton::clicked,this,&MsgBoxDlg::showQuestionMsgDlg);
connect(m_infomationBtn,&QPushButton::clicked,this,&MsgBoxDlg::showInfomationMsgDlg);
connect(m_warningBtn,&QPushButton::clicked,this,&MsgBoxDlg::showWarningMsgDlg);
connect(m_criticalBtn,&QPushButton::clicked,this,&MsgBoxDlg::showCriticalMsgDlg);
connect(m_aboutBtn,&QPushButton::clicked,this,&MsgBoxDlg::showAboutMsgDlg);
connect(m_aboutQtBtn,&QPushButton::clicked,this,&MsgBoxDlg::showAboutQtMsgDlg);
}

void MsgBoxDlg::showQuestionMsgDlg()
{
m_tipLabel->setText("问题消息框");
int res = QMessageBox::question(this,"问题消息框","您已打开问题消息框,是否关闭?",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Ok);
switch (res) {
case QMessageBox::Ok:
m_tipLabel->setText("问题消息框-确定");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("问题消息框-取消");
default:
break;
}
}

void MsgBoxDlg::showInfomationMsgDlg()
{
m_tipLabel->setText("信息消息框");
QMessageBox::information(this,"信息提示框","这是个信息提示框");
}

void MsgBoxDlg::showWarningMsgDlg()
{
m_tipLabel->setText("警告消息");
int res = QMessageBox::warning(this,"警告消息框","警告!",QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard,QMessageBox::Save);
switch (res) {
case QMessageBox::Save:
m_tipLabel->setText("用户点击了保存");
break;
case QMessageBox::Discard:
m_tipLabel->setText("用户点击了忽视");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("用户点击了取消");
default:
break;
}
}

void MsgBoxDlg::showCriticalMsgDlg()
{
m_tipLabel->setText("错误消息框");
QMessageBox::critical(this,"错误消息框","发生错误!");
}

void MsgBoxDlg::showAboutMsgDlg()
{
m_tipLabel->setText("关于消息框");
QMessageBox::about(this,"关于","关于消息");
}

void MsgBoxDlg::showAboutQtMsgDlg()
{
m_tipLabel->setText("关于Qt消息框");
QMessageBox::aboutQt(this,"关于QT");
}

  • 输入对话框

image-20220123223756778

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
#include "inputdlag.h"
#include<QLineEdit>
#include<QInputDialog>
InputDlag::InputDlag(QWidget*parent):QDialog(parent)
{
setWindowTitle("输入对话框示例");
m_nameTitle = new QLabel("姓名:");
m_sexTitle = new QLabel("性别");
m_ageTitle = new QLabel("年龄:");
m_scoreTitle = new QLabel("打分:");
m_nameLabel = new QLabel("快乐的威猛先生");
m_sexLabel = new QLabel("男");
m_ageLabel = new QLabel("19");
m_scoreLabel = new QLabel("100");
m_nameBtn = new QPushButton("编辑姓名");
m_ageBtn = new QPushButton("编辑年龄");
m_sexBtn = new QPushButton("编辑性别");
m_scoreBtn = new QPushButton("编辑分数");

m_mainLayout = new QGridLayout(this);
m_mainLayout->addWidget(m_nameTitle,0,0);
m_mainLayout->addWidget(m_nameLabel,0,1);
m_mainLayout->addWidget(m_nameBtn,0,2);
m_mainLayout->addWidget(m_sexTitle,1,0);
m_mainLayout->addWidget(m_sexLabel,1,1);
m_mainLayout->addWidget(m_sexBtn,1,2);
m_mainLayout->addWidget(m_ageTitle,2,0);
m_mainLayout->addWidget(m_ageLabel,2,1);
m_mainLayout->addWidget(m_ageBtn,2,2);
m_mainLayout->addWidget(m_scoreTitle,3,0);
m_mainLayout->addWidget(m_scoreLabel,3,1);
m_mainLayout->addWidget(m_scoreBtn,3,2);
//设置控件间的间隙
m_mainLayout->setSpacing(20);
//设置控件与窗体的间隙
m_mainLayout->setMargin(10);

connect(m_nameBtn,&QPushButton::clicked,this,&InputDlag::editName);
connect(m_ageBtn,&QPushButton::clicked,this,&InputDlag::editAge);
connect(m_sexBtn,&QPushButton::clicked,this,&InputDlag::editSex);
connect(m_scoreBtn,&QPushButton::clicked,this,&InputDlag::editScore);
}

void InputDlag::editName()
{
bool ok;
QString sName = QInputDialog::getText(this,"标准字符串输入对话框","请编辑姓名",QLineEdit::Normal,m_nameLabel->text(),&ok);
if(ok && !sName.isEmpty())
{
m_nameLabel->setText(sName);
}
}

void InputDlag::editAge()
{
bool ok;
int age = QInputDialog::getInt(this,"标准int数据类型对话框","请编辑年龄",m_ageLabel->text().toInt(),0,120,1,&ok);
if(ok)
{
m_ageLabel->setText(QString("%1").arg(age));
}
}

void InputDlag::editSex()
{
bool ok;
QStringList sexList;
sexList<<"男"<<"女"<<"保密";
QString sex = QInputDialog::getItem(this,"标准条目输入对话框","请选择性别",sexList,0,false,&ok);
if(ok && !sex.isEmpty())
{
m_sexLabel->setText(sex);
}
}

void InputDlag::editScore()
{
bool ok;//保存函数调用是否成功
double score = QInputDialog::getDouble(this,"标准double类型输入对话框","请打分",m_scoreLabel->text().toDouble(),0,100,1,&ok);
if(ok)
{
m_scoreLabel->setText(QString::number(score));
}
}

  • 文件对话框

image-20220123224016456

1
2
3
4
5
void Dialog::ShowFileDlg()
{
QString sPath = QFileDialog::getOpenFileName(this,"标准文件对话框",".","C++ files(*.cpp);;C files(*.c);;Header files(*.h)" );
m_fileLineBtn->setText(sPath);
}