Qt中的对话框

QDialog是对话框的基类,且它继承于QWidget,并有自己的一些特性。

1.常用API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 构造函数
//构造函数第二个形参为窗口标志,默认为 `Qt::Dialog = 0x0000 0002 | Qt::Window`。
//只要该标志位包含`Qt::Window`,则该对象是一个窗口,窗口就是一种顶层的部件。
QDialog::QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());

// 模态显示窗口
[virtual slot] int QDialog::exec();
// 隐藏模态窗口, 并且解除模态窗口的阻塞, 将 exec() 的返回值设置为 QDialog::Accepted
[virtual slot] void QDialog::accept();
// 隐藏模态窗口, 并且解除模态窗口的阻塞, 将 exec() 的返回值设置为 QDialog::Rejected
[virtual slot] void QDialog::reject();
// 关闭对话框并将其结果代码设置为r。finished()信号将发出r;
// 如果r是QDialog::Accepted 或 QDialog::Rejected,则还将分别发出accept()或Rejected()信号。
[virtual slot] void QDialog::done(int r);

[signal] void QDialog::accepted();
[signal] void QDialog::rejected();
[signal] void QDialog::finished(int result);

1.1窗口的特点:

  • 独立的窗口(没有父对象)默认不可见,需要使用.show().exec()等成员函数才能显示

  • 窗口通常会被添加到任务栏中,但若该窗口具有父对象,则不会单独添加到任务栏中,而是与其父窗口共用。

1.2模态与非模态对话框

2.1 模态对话框的定义

在同一个程序中,模态对话框会阻止用户与其他可见窗口的交互, 模态对话框通常用于需要返回值的情形,比如获取用户按下的是 Ok 还是 Cancel 键等。

2.2 模态对话框的分类

  • 应用程序模式对话框: 该模式会阻止用户与应用程序中的所有窗口的交互,直到完成与对话框的交互,并关闭该对话框,然后才能访问程序中的其他窗口。

  • 窗口模式对话框:只阻止与对话框相关联的窗口的访问,允许用户在程序中继续使用其他窗口。 该模式会阻止与对话框的父窗口、所有祖先窗口及父窗口和祖先窗口的所有兄弟姐妹窗口的交互,除这些窗口外,该模式仍可与其他窗口交互。

2.3 设置模态对话框的方法

QDialog::exec():把对话框以模态对话框(默认为应用程序模式)的形式显示,该函数不会立即返回,需结束对话框才会返回该函数。

QDialog::open():把对话框显示为窗口模式对话框,并立即返回。

直接使用QDialog::show()就是非模态的对话框,注意通常QWidget的控件一般都用.show()来显示,其实就是非模态显示,但是QWidget没有exec()方法,如果要模态显示,得设置它的属性

1.3窗口的关闭/隐藏

以下概念需要区分:

  • 删除:如果窗口是new出来的,删除就是把及它的子对象delete了,即该窗口不能继续使用

  • 隐藏:把窗口设置成不可见的,但仍可以通过show()再次可见

  • 关闭:把窗口删除或者隐藏,这依据传入参数决定

关闭一个窗口需要使用close()槽函数,具体是删除还是隐藏与该部件的一个子类QWidgetData的一个成员变量widget_attributes有关,当使用setAttribute(Qt::WA_QuitOnClose, true)接口将这个变量的Qt::WA_QuitOnClose设为true时,关闭的时候即删除该窗口,反之仅隐藏。

1.4Qt内置的常用对话框

在实际写Qt时,我们一般不直接用QDialog类,而是用其子类,比如消息对话框、文件对话框、颜色对话框、输入对话框等等….

并且,一般用的对话框是不需要我们像设置MainWindow那样用UI来设置的,因为内置的对话框里面包含了常用按键,和文本的显示,所以直接定义对话框对象就行了。

注意事项:

这些QDialog的内置子类,由于其内部有个特定的布局管理器来保证个部件的大小正确,因此无法使用resize()函数改变其大小。如果用户需要改变对话框的大小,则需要自己写一个QDialog的子类。

2.对话框的常见使用方法

根据用户针对对话框窗口的按钮操作, 进行相应的逻辑处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 创建对话框对象
MyDialog dlg;
int ret = dlg.exec();
if(ret == QDialog::Accepted)
{
qDebug() << "accept button clicked...";
}
else if(ret == QDialog::Rejected)
{
qDebug() << "reject button clicked...";
}
else
{
qDebug() << "done button clicked...";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 对话框窗口中三个普通按钮按下之后对应的槽函数
void MyDialog::on_acceptBtn_clicked()
{
this->accept(); // exec()函数返回值为QDialog::Accepted
}

void MyDialog::on_rejectBtn_clicked()
{
this->reject(); // exec()函数返回值为QDialog::Rejected
}

void MyDialog::on_donBtn_clicked()
{
// exec()函数返回值为 done() 的参数, 并根据参数发射出对应的信号
this->done(666);
}

3.对话框的子类

3.1QMessageBox(消息对话框)

QMessageBox 对话框类是 QDialog 类的子类, 通过这个类可以显示一些简单的提示框, 用于展示警告、错误、问题等信息。关于这个类我们只需要掌握一些静态方法的使用就可以了。

3.1.1 API - 静态函数

用以下API可以直接创建模态对话框

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
// 显示一个模态对话框, 将参数 text 的信息展示到窗口中
[static] void QMessageBox::about(QWidget *parent, const QString &title, const QString &text);

/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口的标题
- text: 对话框窗口中显示的提示信息
- buttons: 对话框窗口中显示的按钮(一个或多个)
- defaultButton
1. defaultButton指定按下Enter键时使用的按钮。
2. defaultButton必须引用在参数 buttons 中给定的按钮。
3. 如果defaultButton是QMessageBox::NoButton, QMessageBox会自动选择一个合适的默认值。
*/
// 显示一个信息模态对话框
[static] QMessageBox::StandardButton QMessageBox::information(
QWidget *parent, const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton);

// 显示一个错误模态对话框
[static] QMessageBox::StandardButton QMessageBox::critical(
QWidget *parent, const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton);

// 显示一个问题模态对话框
[static] QMessageBox::StandardButton QMessageBox::question(
QWidget *parent, const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = StandardButtons(Yes | No),
QMessageBox::StandardButton defaultButton = NoButton);

// 显示一个警告模态对话框
[static] QMessageBox::StandardButton QMessageBox::warning(
QWidget *parent, const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = Ok,
QMessageBox::StandardButton defaultButton = NoButton);

3.1.2使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QMessageBox::about(this, "about",  "这是一个简单的消息提示框!!!");
QMessageBox::critical(this, "critical", "这是一个错误对话框-critical...");
int ret = QMessageBox::question(this, "question",
"你要保存修改的文件内容吗???",
QMessageBox::Save | QMessageBox::Cancel,
QMessageBox::Cancel);
if(ret == QMessageBox::Save)
{
QMessageBox::information(this, "information", "恭喜你保存成功了, o(* ̄︶ ̄*)o!!!");
}
else if(ret == QMessageBox::Cancel)
{
QMessageBox::warning(this, "warning", "你放弃了保存, ┭┮﹏┭┮ !!!");
}

image-20231010093826925image-20231010093838476

3.2QFileDialog(文件对话框)

QFileDialog 对话框类是 QDialog 类的子类, 通过这个类可以选择要打开/保存的文件或者目录。关于这个类我们只需要掌握一些静态方法的使用就可以了。

3.2.1 API - 静态函数

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
/*
通用参数:
- parent: 当前对话框窗口的父对象也就是父窗口
- caption: 当前对话框窗口的标题
- dir: 当前对话框窗口打开的默认目录
- options: 当前对话框窗口的一些可选项,枚举类型, 一般不需要进行设置, 使用默认值即可
- filter: 过滤器, 在对话框中只显示满足条件的文件, 可以指定多个过滤器, 使用 ;; 分隔
- 样式举例:
- Images (*.png *.jpg)
- Images (*.png *.jpg);;Text files (*.txt)
- selectedFilter: 如果指定了多个过滤器, 通过该参数指定默认使用哪一个, 不指定默认使用第一个过滤器
*/
// 打开一个目录, 得到这个目录的绝对路径
[static] QString QFileDialog::getExistingDirectory(
QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
QFileDialog::Options options = ShowDirsOnly);

// 打开一个文件, 得到这个文件的绝对路径
[static] QString QFileDialog::getOpenFileName(
QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = Options());

// 打开多个文件, 得到这多个文件的绝对路径
[static] QStringList QFileDialog::getOpenFileNames(
QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = Options());

// 打开一个目录, 使用这个目录来保存指定的文件
[static] QString QFileDialog::getSaveFileName(
QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = Options());

3.2.2 测试代码

打开一个本地目录:

1
2
3
4
5
void MainWindow::on_filedlg_clicked()
{
QString dirName = QFileDialog::getExistingDirectory(this, "打开目录", "e:\\temp");
QMessageBox::information(this, "打开目录", "您选择的目录是: " + dirName);
}

打开一个本地文件:

1
2
3
4
5
6
7
8
void MainWindow::on_filedlg_clicked()
{
QString arg("Text files (*.txt)");
QString fileName = QFileDialog::getOpenFileName(
this, "Open File", "e:\\temp",
"Images (*.png *.jpg);;Text files (*.txt)", &arg);
QMessageBox::information(this, "打开文件", "您选择的文件是: " + fileName);
}

打开多个本地文件:

1
2
3
4
5
6
7
8
9
10
11
12
void MainWindow::on_filedlg_clicked()
{
QStringList fileNames = QFileDialog::getOpenFileNames(
this, "Open File", "e:\\temp",
"Images (*.png *.jpg);;Text files (*.txt)");
QString names;
for(int i=0; i<fileNames.size(); ++i)
{
names += fileNames.at(i) + " ";
}
QMessageBox::information(this, "打开文件(s)", "您选择的文件是: " + names);
}

打开保存文件对话框:

1
2
3
4
5
void MainWindow::on_filedlg_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, "保存文件", "e:\\temp");
QMessageBox::information(this, "保存文件", "您指定的保存数据的文件是: " + fileName);
}

3.3 QFontDialog

QFontDialog类是QDialog的子类, 通过这个类我们可以得到一个进行字体属性设置的对话框窗口, 和前边介绍的对话框类一样, 我们只需要调用这个类的静态成员函数就可以得到想要的窗口了。

3.3.1 QFont 字体类

关于字体的属性信息, 在QT框架中被封装到了一个叫QFont的类中, 下边为大家介绍一下这个类的API, 了解一下关于这个类的使用。

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
// 构造函数
QFont::QFont();
/*
参数:
- family: 本地字库中的字体名, 通过 office 等文件软件可以查看
- pointSize: 字体的字号
- weight: 字体的粗细, 有效范围为 0 ~ 99
- italic: 字体是否倾斜显示, 默认不倾斜
*/
QFont::QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);

// 设置字体
void QFont::setFamily(const QString &family);
// 根据字号设置字体大小
void QFont::setPointSize(int pointSize);
// 根据像素设置字体大小
void QFont::setPixelSize(int pixelSize);
// 设置字体的粗细程度, 有效范围: 0 ~ 99
void QFont::setWeight(int weight);
// 设置字体是否加粗显示
void QFont::setBold(bool enable);
// 设置字体是否要倾斜显示
void QFont::setItalic(bool enable);

// 获取字体相关属性(一般规律: 去掉设置函数的 set 就是获取相关属性对应的函数名)
QString QFont::family() const;
bool QFont::italic() const;
int QFont::pixelSize() const;
int QFont::pointSize() const;
bool QFont::bold() const;
int QFont::weight() const;

如果一个QFont对象被创建, 并且进行了初始化, 我们可以将这个属性设置给某个窗口, 或者设置给当前应用程序对象。

1
2
3
4
5
6
7
8
9
10
11
// QWidget 类
// 得到当前窗口使用的字体
const QWidget::QFont& font() const;
// 给当前窗口设置字体, 只对当前窗口类生效
void QWidget::setFont(const QFont &);

// QApplication 类
// 得到当前应用程序对象使用的字体
[static] QFont QApplication::font();
// 给当前应用程序对象设置字体, 作用于当前应用程序的所有窗口
[static] void QApplication::setFont(const QFont &font, const char *className = nullptr);

3.3.2 QFontDialog类的静态API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
参数:
- ok: 传出参数, 用于判断是否获得了有效字体信息, 指定一个布尔类型变量地址
- initial: 字体对话框中默认选中并显示该字体信息, 用于对话框的初始化
- parent: 字体对话框窗口的父对象
- title: 字体对话框的窗口标题
- options: 字体对话框选项, 使用默认属性即可, 一般不设置
*/
[static] QFont QFontDialog::getFont(
bool *ok, const QFont &initial,
QWidget *parent = nullptr, const QString &title = QString(),
QFontDialog::FontDialogOptions options = FontDialogOptions());

[static] QFont QFontDialog::getFont(bool *ok, QWidget *parent = nullptr);

3.3.3 测试代码

通过字体对话框选择字体, 并将选择的字体设置给当前窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MainWindow::on_fontdlg_clicked()
{
#if 1
// 方式1
bool ok;
QFont ft = QFontDialog::getFont(
&ok, QFont("微软雅黑", 12, QFont::Bold), this, "选择字体");
qDebug() << "ok value is: " << ok;
#else
// 方式2
QFont ft = QFontDialog::getFont(NULL);
#endif
// 将选择的字体设置给当前窗口对象
this->setFont(ft);
}

字体对话框效果展示:

3.4QColorDialog

QColorDialog类是QDialog的子类, 通过这个类我们可以得到一个选择颜色的对话框窗口, 和前边介绍的对话框类一样, 我们只需要调用这个类的静态成员函数就可以得到想要的窗口了。

3.4.1 颜色类 QColor

关于颜色的属性信息, 在QT框架中被封装到了一个叫QColor的类中, 下边为大家介绍一下这个类的API, 了解一下关于这个类的使用。
各种颜色都是基于红, 绿, 蓝这三种颜色调配而成的, 并且颜色还可以进行透明度设置, 默认是不透明的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 构造函数
QColor::QColor(Qt::GlobalColor color);
QColor::QColor(int r, int g, int b, int a = ...);
QColor::QColor();

// 参数设置 red, green, blue, alpha, 取值范围都是 0-255
void QColor::setRed(int red); // 红色
void QColor::setGreen(int green); // 绿色
void QColor::setBlue(int blue); // 蓝色
void QColor::setAlpha(int alpha); // 透明度, 默认不透明(255)
void QColor::setRgb(int r, int g, int b, int a = 255);

int QColor::red() const;
int QColor::green() const;
int QColor::blue() const;
int QColor::alpha() const;
void QColor::getRgb(int *r, int *g, int *b, int *a = nullptr) const;

3.4.2 静态API函数

1
2
3
4
5
6
7
8
9
10
11
12
  // 弹出颜色选择对话框, 并返回选中的颜色信息
/*
参数:
- initial: 对话框中默认选中的颜色, 用于窗口初始化
- parent: 给对话框窗口指定父对象
- title: 对话框窗口的标题
- options: 颜色对话框窗口选项, 使用默认属性即可, 一般不需要设置
*/
[static] QColor QColorDialog::getColor(
const QColor &initial = Qt::white,
QWidget *parent = nullptr, const QString &title = QString(),
QColorDialog::ColorDialogOptions options = ColorDialogOptions());

3.4.3 测试代码

1
2
3
4
场景描述:
1. 在窗口上放一个标签控件
2. 通过颜色对话框选择一个颜色, 将选中的颜色显示到标签控件上
3. 将选中的颜色的 RGBA 值分别显示出来
1
2
3
4
5
6
7
8
9
10
11
12
13
void MainWindow::on_colordlg_clicked()
{
QColor color = QColorDialog::getColor();
QBrush brush(color);
QRect rect(0, 0, ui->color->width(), ui->color->height());
QPixmap pix(rect.width(), rect.height());
QPainter p(&pix);
p.fillRect(rect, brush);
ui->color->setPixmap(pix);
QString text = QString("red: %1, green: %2, blue: %3, 透明度: %4")
.arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
ui->colorlabel->setText(text);
}

3.5QInputDialog

QInputDialog类是QDialog的子类, 通过这个类我们可以得到一个输入对话框窗口, 根据实际需求我们可以在这个输入窗口中输入整形, 浮点型, 字符串类型的数据, 并且还可以显示下拉菜单供使用者选择。
和前边介绍的对话框类一样, 我们只需要调用这个类的静态成员函数就可以得到想要的窗口了。

3.5.1 API - 静态函数

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
// 得到一个可以输入浮点数的对话框窗口, 返回对话框窗口中输入的浮点数
/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口显示的标题信息
- label: 对话框窗口中显示的文本信息(用于描述对话框的功能)
- value: 对话框窗口中显示的浮点值, 默认为 0
- min: 对话框窗口支持显示的最小数值
- max: 对话框窗口支持显示的最大数值
- decimals: 浮点数的精度, 默认保留小数点以后1位
- ok: 传出参数, 用于判断是否得到了有效数据, 一般不会使用该参数
- flags: 对话框窗口的窗口属性, 使用默认值即可
*/
[static] double QInputDialog::getDouble(
QWidget *parent, const QString &title,
const QString &label, double value = 0,
double min = -2147483647, double max = 2147483647,
int decimals = 1, bool *ok = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags());

// 得到一个可以输入整形数的对话框窗口, 返回对话框窗口中输入的整形数
/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口显示的标题信息
- label: 对话框窗口中显示的文本信息(用于描述对话框的功能)
- value: 对话框窗口中显示的整形值, 默认为 0
- min: 对话框窗口支持显示的最小数值
- max: 对话框窗口支持显示的最大数值
- step: 步长, 通过对话框提供的按钮调节数值每次增长/递减的量
- ok: 传出参数, 用于判断是否得到了有效数据, 一般不会使用该参数
- flags: 对话框窗口的窗口属性, 使用默认值即可
*/
[static] int QInputDialog::getInt(
QWidget *parent, const QString &title,
const QString &label, int value = 0,
int min = -2147483647, int max = 2147483647,
int step = 1, bool *ok = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags());

// 得到一个带下来菜单的对话框窗口, 返回选择的菜单项上边的文本信息
/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口显示的标题信息
- label: 对话框窗口中显示的文本信息(用于描述对话框的功能)
- items: 字符串列表, 用于初始化窗口中的下拉菜单, 每个字符串对应一个菜单项
- current: 通过菜单项的索引指定显示下拉菜单中的哪个菜单项, 默认显示第一个(编号为0)
- editable: 设置菜单项上的文本信息是否可以进行编辑, 默认为true, 即可以编辑
- ok: 传出参数, 用于判断是否得到了有效数据, 一般不会使用该参数
- flags: 对话框窗口的窗口属性, 使用默认值即可
- inputMethodHints: 设置显示模式, 默认没有指定任何特殊显示格式, 显示普通文本字符串
- 如果有特殊需求, 可以参数帮助文档进行相关设置
*/
[static] QString QInputDialog::getItem(
QWidget *parent, const QString &title,
const QString &label, const QStringList &items,
int current = 0, bool editable = true, bool *ok = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);

// 得到一个可以输入多行数据的对话框窗口, 返回用户在窗口中输入的文本信息
/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口显示的标题信息
- label: 对话框窗口中显示的文本信息(用于描述对话框的功能)
- text: 指定显示到多行输入框中的文本信息, 默认是空字符串
- ok: 传出参数, 用于判断是否得到了有效数据, 一般不会使用该参数
- flags: 对话框窗口的窗口属性, 使用默认值即可
- inputMethodHints: 设置显示模式, 默认没有指定任何特殊显示格式, 显示普通文本字符串
- 如果有特殊需求, 可以参数帮助文档进行相关设置
*/
[static] QString QInputDialog::getMultiLineText(
QWidget *parent, const QString &title, const QString &label,
const QString &text = QString(), bool *ok = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);

// 得到一个可以输入单行信息的对话框窗口, 返回用户在窗口中输入的文本信息
/*
参数:
- parent: 对话框窗口的父窗口
- title: 对话框窗口显示的标题信息
- label: 对话框窗口中显示的文本信息(用于描述对话框的功能)
- mode: 指定单行编辑框中数据的反馈模式, 是一个 QLineEdit::EchoMode 类型的枚举值
- QLineEdit::Normal: 显示输入的字符。这是默认值
- QLineEdit::NoEcho: 不要展示任何东西。这可能适用于连密码长度都应该保密的密码。
- QLineEdit::Password: 显示与平台相关的密码掩码字符,而不是实际输入的字符。
- QLineEdit::PasswordEchoOnEdit: 在编辑时按输入显示字符,否则按密码显示字符。
- text: 指定显示到单行输入框中的文本信息, 默认是空字符串
- ok: 传出参数, 用于判断是否得到了有效数据, 一般不会使用该参数
- flags: 对话框窗口的窗口属性, 使用默认值即可
- inputMethodHints: 设置显示模式, 默认没有指定任何特殊显示格式, 显示普通文本字符串
- 如果有特殊需求, 可以参数帮助文档进行相关设置
*/
[static] QString QInputDialog::getText(
QWidget *parent, const QString &title, const QString &label,
QLineEdit::EchoMode mode = QLineEdit::Normal,
const QString &text = QString(), bool *ok = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);

3.5.2 测试代码

  • 整形输入
1
2
3
4
5
void MainWindow::on_inputdlg_clicked()
{
int ret = QInputDialog::getInt(this, "年龄", "您的当前年龄: ", 10, 1, 100, 2);
QMessageBox::information(this, "年龄", "您的当前年龄: " + QString::number(ret));
}

  • 浮点型输入框
1
2
3
4
5
void MainWindow::on_inputdlg_clicked()
{
double ret = QInputDialog::getDouble(this, "工资", "您的工资: ", 2000, 1000, 6000, 2);
QMessageBox::information(this, "工资", "您的当前工资: " + QString::number(ret));
}

  • 带下拉菜单的输入框
1
2
3
4
5
6
7
void MainWindow::on_inputdlg_clicked()
{
QStringList items;
items << "苹果" << "橙子" << "橘子" << "葡萄" << "香蕉" << "哈密瓜";
QString item = QInputDialog::getItem(this, "请选择你喜欢的水果", "你最喜欢的水果:", items, 1, false);
QMessageBox::information(this, "水果", "您最喜欢的水果是: " + item);
}

3103867f36bb81d7640e98268a709299_image-6e4f5ea8159d45789291f9b14ba18d7b

  • 多行字符串输入框
1
2
3
4
5
void MainWindow::on_inputdlg_clicked()
{
QString info = QInputDialog::getMultiLineText(this, "表白", "您最想对漂亮小姐姐说什么呢?", "呦吼吼...");
QMessageBox::information(this, "知心姐姐", "您最想对小姐姐说: " + info);
}

  • 单行字符串输入框
1
2
3
4
5
void MainWindow::on_inputdlg_clicked()
{
QString text = QInputDialog::getText(this, "密码", "请输入新的密码", QLineEdit::Password, "helloworld");
QMessageBox::information(this, "密码", "您设置的密码是: " + text);
}

3.6QProgressDialog

QProgressDialog类是QDialog的子类, 通过这个类我们可以得到一个带进度条的对话框窗口, 这种类型的对话框窗口一般常用于文件拷贝数据传输等实时交互的场景中。

3.6.1 常用API

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
// 构造函数
/*
参数:
- labelText: 对话框中显示的提示信息
- cancelButtonText: 取消按钮上显示的文本信息
- minimum: 进度条最小值
- maximum: 进度条最大值
- parent: 当前窗口的父对象
- f: 当前进度窗口的flag属性, 使用默认属性即可, 无需设置
*/
QProgressDialog::QProgressDialog(
QWidget *parent = nullptr,
Qt::WindowFlags f = Qt::WindowFlags());

QProgressDialog::QProgressDialog(
const QString &labelText, const QString &cancelButtonText,
int minimum, int maximum, QWidget *parent = nullptr,
Qt::WindowFlags f = Qt::WindowFlags());


// 设置取消按钮显示的文本信息
[slot] void QProgressDialog::setCancelButtonText(const QString &cancelButtonText);

// 公共成员函数和槽函数
QString QProgressDialog::labelText() const;
void QProgressDialog::setLabelText(const QString &text);

// 得到进度条最小值
int QProgressDialog::minimum() const;
// 设置进度条最小值
void QProgressDialog::setMinimum(int minimum);

// 得到进度条最大值
int QProgressDialog::maximum() const;
// 设置进度条最大值
void QProgressDialog::setMaximum(int maximum);

// 设置进度条范围(最大和最小值)
[slot] void QProgressDialog::setRange(int minimum, int maximum);

// 得到进度条当前的值
int QProgressDialog::value() const;
// 设置进度条当前的值
void QProgressDialog::setValue(int progress);


bool QProgressDialog::autoReset() const;
// 当value() = maximum()时,进程对话框是否调用reset(),此属性默认为true。
void QProgressDialog::setAutoReset(bool reset);


bool QProgressDialog::autoClose() const;
// 当value() = maximum()时,进程对话框是否调用reset()并且隐藏,此属性默认为true。
void QProgressDialog::setAutoClose(bool close);

// 判断用户是否按下了取消键, 按下了返回true, 否则返回false
bool wasCanceled() const;


// 重置进度条
// 重置进度对话框。wascancelled()变为true,直到进程对话框被重置。进度对话框被隐藏。
[slot] void QProgressDialog::cancel();
// 重置进度对话框。如果autoClose()为真,进程对话框将隐藏。
[slot] void QProgressDialog::reset();

// 信号
// 当单击cancel按钮时,将发出此信号。默认情况下,它连接到cancel()槽。
[signal] void QProgressDialog::canceled();

// 设置窗口的显示状态(模态, 非模态)
/*
参数:
Qt::NonModal -> 非模态
Qt::WindowModal -> 模态, 阻塞父窗口
Qt::ApplicationModal -> 模态, 阻塞应用程序中的所有窗口
*/
void QWidget::setWindowModality(Qt::WindowModality windowModality);

3.6.2 测试代码

1
2
3
4
5
场景描述:
1. 基于定时器模拟文件拷贝的场景
2. 点击窗口按钮, 进度条窗口显示, 同时启动定时器
3. 通过定时器信号, 按照固定频率更新对话框窗口进度条
4. 当进度条当前值 == 最大值, 关闭定时器, 关闭并析构进度对话框
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
void MainWindow::on_progressdlg_clicked()
{
// 1. 创建进度条对话框窗口对象
QProgressDialog *progress = new QProgressDialog(
"正在拷贝数据...", "取消拷贝", 0, 100, this);
// 2. 初始化并显示进度条窗口
progress->setWindowTitle("请稍后");
progress->setWindowModality(Qt::WindowModal);
progress->show();

// 3. 更新进度条
static int value = 0;
QTimer *timer = new QTimer;
connect(timer, &QTimer::timeout, this, [=]()
{
progress->setValue(value);
value++;
// 当value > 最大值的时候
if(value > progress->maximum())
{
timer->stop();
value = 0;
delete progress;
delete timer;
}
});

connect(progress, &QProgressDialog::canceled, this, [=]()
{
timer->stop();
value = 0;
delete progress;
delete timer;
});

timer->start(50);
}