文章目录
- 1. 简介
- 2. 对话框中的标准按钮
- 3. 对话框中按钮的布局
- 4. 在clicked信号的槽函数中获取被点击的按钮
本文持续更新,在以后的工作中如果遇到了新的问题时,会更新本文
1. 简介
??QDialogButtongBox类是一个包含很多按钮的控件,在对话框中有多个按钮需要分组排列的按钮时,可以使用QDialogButtongBox类。
??对话框或者消息框中的按钮布局,不同平台风格不同。开发人员可以向QDialogButtonBox添加按钮,在添加后QDialogButtonBox会为用户自动使用合适的布局。
| Header: | #include |
|---|---|
| qmake: | QT += widgets |
| Since: | Qt 4.2 |
| Inherits: | QWidget. |
2. 对话框中的标准按钮
??对于对话框中的标准按钮(如,OK,Cancel,Save)可以在QDialogButtonBox创建时,使用flag标志的或运算指定。
3. 对话框中按钮的布局
??另外,QDialogButtongBox中的按钮,可以水平排列也可以竖直排列。在创建 QDialogButtongBox时,通过指定参数Qt::Orientation orientation 来指定按钮的布局方式。
1 2 3 4 5 6 7 8 9 10 11 12 | buttonBox = new QDialogButtonBox(Qt::Horizontal); QPushButton *mkdirButton = buttonBox->addButton( tr("&Create Directory..."), QDialogButtonBox::ActionRole); QPushButton *removeButton = buttonBox->addButton(tr("&Remove"), QDialogButtonBox::ActionRole); buttonBox->addButton(tr("&Quit"), QDialogButtonBox::AcceptRole); connect(mkdirButton, SIGNAL(clicked()), this, SLOT(createDirectory())); connect(removeButton, SIGNAL(clicked()), this, SLOT(remove())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); |
4. 在clicked信号的槽函数中获取被点击的按钮
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 | #include "dialog.h" #include "ui_dialog.h" #include <QPushButton> #include <QDebug> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_buttonBox_clicked(QAbstractButton *button) { if(button == static_cast<QAbstractButton *>(ui->buttonBox->button(QDialogButtonBox::Ok))) { qDebug() << R"(Button "OK" has been clicked.)"; } else if(button == ui->buttonBox->button(QDialogButtonBox::Cancel)) { qDebug() << R"(Button "Cancle" has been clicked.)"; } } |
??重点是要包含头文件 QPushButton ,否则代码中 QAbstractButton 和 QPushButton 两种类型的指针无法进行转换和比较。
未完待续