-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputdlg.cpp
132 lines (94 loc) · 3.64 KB
/
inputdlg.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "inputdlg.h"
#include "QInputDialog"
#include "QStringList"
InputDlg::InputDlg(QWidget *parent) :
QDialog(parent)
{
setWindowTitle(QStringLiteral("标准输入对话框的实例"));
nameLabel1 = new QLabel;
nameLabel1->setText(QStringLiteral("姓名"));
nameLabel2 = new QLabel;
nameLabel2->setText(QStringLiteral("张三"));
nameLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
nameBtn = new QPushButton;
nameBtn->setText(QStringLiteral("修改姓名"));
sexLabel1 = new QLabel;
sexLabel1->setText(QStringLiteral("性别"));
sexLabel2 = new QLabel;
sexLabel2->setText(QStringLiteral("男"));
sexLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
sexBtn = new QPushButton;
sexBtn->setText(QStringLiteral("修改性别"));
ageLabel1 = new QLabel;
ageLabel1->setText(QStringLiteral("年龄"));
ageLabel2 = new QLabel;
ageLabel2->setText(tr("21"));
ageLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
ageBtn = new QPushButton;
ageBtn->setText(QStringLiteral("修改年龄"));
scoreLabel1 = new QLabel;
scoreLabel1->setText(QStringLiteral("成绩"));
scoreLabel2 = new QLabel;
scoreLabel2->setText(tr("80"));
scoreLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
scoreBtn = new QPushButton;
scoreBtn->setText(QStringLiteral("修改成绩"));
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);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
connect(nameBtn, SIGNAL(clicked()), this, SLOT(ChangeName()));
connect(sexBtn, SIGNAL(clicked()), this, SLOT(ChangeSex()));
connect(ageBtn, SIGNAL(clicked()), this, SLOT(ChangeAge()));
connect(scoreBtn, SIGNAL(clicked()), this, SLOT(ChangeScore()));
}
void InputDlg::ChangeName()
{
bool ok;
QString text = QInputDialog::getText(this, QStringLiteral("标准字符串输入对话框"), QStringLiteral("请输入姓名: "), QLineEdit::Normal,
nameLabel2->text(), &ok);
if(ok && !text.isEmpty())
{
nameLabel2->setText(text);
}
}
void InputDlg::ChangeSex()
{
QStringList SexItems;
SexItems << QStringLiteral("男") << QStringLiteral("女");
bool ok;
QString sexItem = QInputDialog::getItem(this, QStringLiteral("标准条目选择对话框"), QStringLiteral("请选择性别"), SexItems, 0, false, &ok);
if(ok && !sexItem.isEmpty())
{
sexLabel2->setText(sexItem);
}
}
void InputDlg::ChangeAge()
{
bool ok;
int age = QInputDialog::getInt(this, QStringLiteral("标准int类型输入对话框"), QStringLiteral("请输入年龄: "), 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, QStringLiteral("标准double类型输入对话框"), QStringLiteral("请输入成绩:"), scoreLabel2->text().toDouble(&ok),
0, 100, 1, &ok);
if(ok)
scoreLabel2->setText(QString(tr("%1")).arg(score));
}