How can I enable the QDialogButtonBox



  • Hello!
    I am creating a GUI and I inserted in it a QDialogButtonBox in Designer.
    I seek a function that enables the OK button (which should close the screen and save changes)
    and a Cancel button (which should close the program and not save changes).



  • someone help me?


  • Lifetime Qt Champion

    Hi,

    Select the button box, go to the property editor and scroll down until you find standardButtons, there you can select which one you want



  • But in this property only selects a button name and not a function to cancel and not save, for example.



  • From the "QDialogButtonBox help":http://qt-project.org/doc/qt-5/qdialogbuttonbox.html#details

    "When a button is clicked in the button box, the clicked() signal is emitted for the actual button is that is pressed. For convenience, if the button has an AcceptRole, RejectRole, or HelpRole, the accepted(), rejected(), or helpRequested() signals are emitted respectively."

    You need to connect the signals, at least "clicked()":http://qt-project.org/doc/qt-5/qdialogbuttonbox.html#clicked to the slots in your class and process the buttons.



  • I created the signal:
    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi(this);

    connect(ui->buttonBox,SIGNAL(clicked()),this,on_buttonBox_accepted());
    connect(ui->buttonBox,SIGNAL(clicked()),this,on_buttonBox_rejected());
    }@

    and the functions:
    @void MainWindow::on_buttonBox_accepted()
    {

    }

    void MainWindow::on_buttonBox_rejected()
    {

    }
    @

    But has an error in connect:
    error: invalid use of void expression.

    How can I solve that?



  • Two things.

    If you create a slot from designer "Goto slot"/accepted, rejected then you don't need to connect them manually. So just delete two connect() lines and it should work.

    If you prefer to connect manually then you connect call is not correct.

    It should look like this
    @
    connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)),
    this, SLOT(buttonClicked(QAbstractButton*)));
    @



  • One way I found to solve my problem was to put in my function @ void MainWindow::on_buttonBox_accepted()
    @ the execution to write in my text file and then used the @qApp->quit()@
    And @ void MainWindow::on_buttonBox_rejected()
    @ I put only the @qApp->quit()@ to close and not execute any writing in my file.


Log in to reply
 

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