Help with QButtonBox

  • Hi,

    I'm new to Qt.

    I have a QDialog with a QButtonBox that has a Save, Cancel and Open buttons. I've read the documentation for the QButtonBox, but I just can't figure out how to manage the buttons events. I know I can manage the accepted() and cancelled() events, but since Open and Save buttons are both 'accepting' I don't know where to put the code for each one of them. Any pointers?

    I've been searching but I just can't find any answers to this problem. I'm stuck and I would very much appreciate your help.


  • You can use the signal :
    void clicked ( QAbstractButton * button )
    to know what button you activate by clicking

  • Thanks. I tried that, but I had problems figuring out how to know what button sent the signal. I finally found button->text() to be the closest hit, but I'm not sure if that's the best way to do it. Is there a better (correct) way to find out about the sender?

  • Use sender () function which return which object send the signal.
    Example :
    void xxxx:mySlot ()
    QPushButton * pb = dynamic_cast <QPushButton *>( sender ());

    if (pb == buttonOk)

  • Lifetime Qt Champion


    You can use the "clicked signal": signal from QDialogButtonBox:
    void xxxx::onClickedButtonSignal(QAbstractButton * button)
    QDialogButtonBox::StandardButton stdButton = _dialogButtonBox.standardButton(button);
    switch (stdButton) {
    case QDialogButtonBox::Ok:
    // etc...

  • _dialogButtonBox was giving me an error because it's not the name of my instance and took me a while to find out that the right code for me:
    @QDialogButtonBox::StandardButton stdButton = ui->buttonBox->standardButton(button);@
    As I told you, I'm new to Qt. But now I got it running. Thank you very much SGaist!

    @Nikoko: Thanks for your suggestion. I think I get what you mean, but SGaist's solution is exactly what I was looking for.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.