2.1.6 button box
The button box can easily and quickly arrange a group of standard buttons. For example, the common confirmation dialog boxes have standard buttons such as "OK" and "Cancel". Qt makes these typical buttons into standard button boxes and encapsulates the corresponding signals for the convenience of programmers. It has horizontal and vertical styles.
Create ButtonBox button box example
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,this);
Enumeration values of button box standard buttons:
constant | numerical value | describe |
QDialogButtonBox::Ok | 0x00000400 | Ok |
QDialogButtonBox::Open | 0x00002000 | Open |
QDialogButtonBox::Save | 0x00000800 | Save |
QDialogButtonBox::Cancel | 0x00400000 | Cancel |
QDialogButtonBox::Close | 0x00200000 | Close |
QDialogButtonBox::Discard | 0x00800000 | Discard |
QDialogButtonBox::Apply | 0x02000000 | Apply |
QDialogButtonBox::Reset | 0x04000000 | Reset |
QDialogButtonBox::RestoreDefaults | 0x08000000 | RestoreDefaults |
QDialogButtonBox::Help | 0x01000000 | Help |
QDialogButtonBox::SaveAll | 0x00001000 | SaveAll |
QDialogButtonBox::Yes | 0x00004000 | Yes |
QDialogButtonBox::YesToAll | 0x00008000 | YesToAll |
QDialogButtonBox::No | 0x00010000 | No |
QDialogButtonBox::NoToAll | 0x00020000 | NoToAll |
QDialogButtonBox::Abort | 0x00040000 | Abort |
QDialogButtonBox::Retry | 0x00080000 | Retry |
QDialogButtonBox::Ignore | 0x00100000 | Ignore |
QDialogButtonBox::NoButton | 0x00000000 | NoButton |
Common member functions
(1) Add a button to the button box. Define the role of the button as role. If the role is invalid, the button will not be added. If the button has been added, it will be removed and added as a new button again.
void QDialogButtonBox::addButton(QAbstractButton * button, ButtonRole role)
(2) Create a button with text to add the specified role to the button box and return the corresponding button. If the role is invalid, the button will not be created and 0 will be returned.
QPushButton * QDialogButtonBox::addButton(const QString & text, ButtonRole role)
(3) Add a standard button to the button box and return to the standard button. If the button is invalid, do not add, and return 0.
QPushButton * QDialogButtonBox::addButton(StandardButton button)
(4) Clear the button box and delete all buttons.
void QDialogButtonBox::clear()
(5) Move the button out of the button box without deleting it. Set its parent window to 0.
void QDialogButtonBox::removeButton(QAbstractButton * button)
(6) Corresponding to the given button, return the enumeration value of the standard button. If the given button is not a standard button, return NoButton.
StandardButton QDialogButtonBox::standardButton(QAbstractButton * button) const
(7) Returns the standard button QPushButton corresponding to the button box. If it is not a standard button, it returns 0, indicating that the button does not exist in the button box. In the slot function of the button box, you can use this function to distinguish which button is currently pressed.
QPushButton * QDialogButtonBox::button(StandardButton which) const
Button related signals
(1) This signal is emitted when the buttons defined as AcceptRole and YesRole roles in the button box are clicked.
void QDialogButtonBox::accepted()
(2) This signal is emitted when the button in the button box is clicked
void QDialogButtonBox::clicked(QAbstractButton * button)
(3) This signal is emitted when the button defined as the HelpRole role in the button box is clicked.
void QDialogButtonBox::helpRequested()
(4) This signal is emitted when the buttons defined as RejectRole and NoRole roles in the button box are clicked.
void QDialogButtonBox::rejected()
Open QtCreator, create a new Qt Widgets Application project, and inherit the Qwidget class. Write an example program to realize the use of command buttons. (supporting procedure No. CH2-5)
Example widget.ui file
Figure 2-12 UI design interface
widget.c file (slot function processing code)
void Widget::on_buttonBox_clicked(QAbstractButton *button) { QString str="Button box test"; QString button_str="Click the button="; if(ui->buttonBox->button(QDialogButtonBox::Ok)==(QPushButton*)button) { //Pop up display button_str+="OK"; QMessageBox::information(this,str,button_str); } else if(ui->buttonBox->button(QDialogButtonBox::Cancel)==(QPushButton*)button) { button_str+="Cancel"; QMessageBox::information(this,str,button_str); } else if(ui->buttonBox->button(QDialogButtonBox::Open)==(QPushButton*)button) { button_str+="open"; QMessageBox::information(this,str,button_str); } }
Operation effect
Figure 2-13 operation effect