QDialogButtonBox使用方法总结

第一此使用QDialogButtonBox按钮盒子,这个按钮盒子可以用系统自带的默认按钮,也可以自己定制按钮使用。

#include <QDialogButtonBox> //包含头文件
button_Box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);//设置两个按钮
Constant                Value       Description
QDialogButtonBox::Ok        0x00000400  An "OK" button defined with the AcceptRole.
QDialogButtonBox::Open      0x00002000  A "Open" button defined with the AcceptRole.
QDialogButtonBox::Save      0x00000800  A "Save" button defined with the AcceptRole.
QDialogButtonBox::Cancel    0x00400000  A "Cancel" button defined with the RejectRole.
QDialogButtonBox::Close     0x00200000  A "Close" button defined with the RejectRole.
QDialogButtonBox::Discard   0x00800000  A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QDialogButtonBox::Apply     0x02000000  An "Apply" button defined with the ApplyRole.
QDialogButtonBox::Reset     0x04000000  A "Reset" button defined with the ResetRole.
QDialogButtonBox::RestoreDefaults   0x08000000  A "Restore Defaults" button defined with the ResetRole.
QDialogButtonBox::Help      0x01000000  A "Help" button defined with the HelpRole.
QDialogButtonBox::SaveAll   0x00001000  A "Save All" button defined with the AcceptRole.
QDialogButtonBox::Yes       0x00004000  A "Yes" button defined with the YesRole.
QDialogButtonBox::YesToAll  0x00008000  A "Yes to All" button defined with the YesRole.
QDialogButtonBox::No        0x00010000  A "No" button defined with the NoRole.
QDialogButtonBox::NoToAll   0x00020000  A "No to All" button defined with the NoRole.
QDialogButtonBox::Abort     0x00040000  An "Abort" button defined with the RejectRole.
QDialogButtonBox::Retry     0x00080000  A "Retry" button defined with the AcceptRole.
QDialogButtonBox::Ignore    0x00100000  An "Ignore" button defined with the AcceptRole.
QDialogButtonBox::NoButton  0x00000000  An invalid button.
button_Box = new QDialogButtonBox(QDialogButtonBox::Ok
                                      | QDialogButtonBox::Cancel
                                      | QDialogButtonBox::Open
                                      | QDialogButtonBox::Save
                                      | QDialogButtonBox::Close
                                      | QDialogButtonBox::Discard
                                      | QDialogButtonBox::Apply
                                      | QDialogButtonBox::Reset
                                      | QDialogButtonBox::RestoreDefaults
                                      | QDialogButtonBox::Help
                                      | QDialogButtonBox::SaveAll);

对于上面按钮盒子里面的按钮,可以供选择和使用的哦这些
规则有下面的规则:
accepted Role(同意规则),rejected Role,ApplayRole,HelpRole,ResetRole,YesRole
按钮盒里面部分按钮

第二种使用方法,在按钮盒子中添加自己想要的按钮

    QDialogButtonBox* button_Box2 = new  QDialogButtonBox(Qt::Vertical);  //Qt::Horizontal
    QPushButton* button_add = new QPushButton(tr("&Add"));
    QPushButton* button_divid = new QPushButton(tr("&Divid"));
    QPushButton* button_multiply = new QPushButton(tr("&Multiply"));
    QPushButton* button_subtract = new QPushButton(tr("&Subtract"));

    button_Box2->addButton(button_add,QDialogButtonBox::ActionRole);
    button_Box2->addButton(button_divid,QDialogButtonBox::ActionRole);
    button_Box2->addButton(button_multiply,QDialogButtonBox::ActionRole);
    button_Box2->addButton(button_subtract,QDialogButtonBox::ActionRole);
    mainlayout->addWidget(button_Box2);

    this->setLayout(mainlayout);

这里写图片描述

这里写图片描述

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QDialogButtonBox是Qt中的一个小部件,用于显示对话框中的标准按钮。它提供了一种简单的方式来添加和管理这些按钮。下面是一个QDialogButtonBox使用示例: ```cpp #include <QApplication> #include <QDialog> #include <QDialogButtonBox> #include <QVBoxLayout> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个对话框 QDialog dialog; dialog.setWindowTitle("QDialogButtonBox Example"); // 创建一个垂直布局 QVBoxLayout layout(&dialog); // 创建一个QDialogButtonBox QDialogButtonBox buttonBox(Qt::Horizontal); // 添加标准按钮到QDialogButtonBox buttonBox.addButton("OK", QDialogButtonBox::AcceptRole); buttonBox.addButton("Cancel", QDialogButtonBox::RejectRole); buttonBox.addButton("Apply", QDialogButtonBox::ApplyRole); buttonBox.addButton("Reset", QDialogButtonBox::ResetRole); // 将QDialogButtonBox添加到布局中 layout.addWidget(&buttonBox); // 连接按钮的信号和槽函数 QObject::connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); QObject::connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); // 显示对话框 dialog.show(); return app.exec(); } ``` 在这个示例中,我们创建了一个对话框,并在对话框中添加了一个QDialogButtonBox。然后,我们使用addButton()函数向QDialogButtonBox添加了几个标准按钮,分别是"OK"、"Cancel"、"Apply"和"Reset"。我们还使用了信号和槽机制,将按钮的accepted和rejected信号连接到对话框的accept()和reject()槽函数上。 这样,当用户点击对应的按钮时,对话框会根据按钮的角色执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值