0

I want to convert exec usages to show method in my projects. Because, when I use the exec for windows (dialogs) I can not open another window. This is the basic difference between exec() and show() method.

The exec and show work in different ways and I am wondering how can I change the below code with using show() instead of exec().

For example:

int result = exampleWindow->exec();

if ( result == QDialogButtonBox::Ok )
{
    exampleWindow->UpdateCalibrationData(&data);
    exampleWindow->UpdateFilterData(&filterData);
    exampleWindow();
}
kefir500
  • 4,184
  • 6
  • 42
  • 48
EmreS
  • 159
  • 1
  • 3
  • 15

3 Answers3

5

show() shows a non-modal window;

exec() shows a modal window.

If you want to get the result of show(), then go with Qt signals/slots:

ExampleWindow::ExampleWindow(QWidget *parent) : QDialog(parent)
{
    // Assuming the QDialogButtonBox name is "buttonBox":
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

MainWindow.h:

class MainWindow : public QMainWindow {
    Q_OBJECT
private slots:
    void updateData();
}

MainWindow.cpp:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    connect(exampleWindow, SIGNAL(accepted()), this, SLOT(updateData()));
}

void MainWindow::updateData()
{
    // Your code:
    exampleWindow->UpdateCalibrationData(&data);
    exampleWindow->UpdateFilterData(&filterData);
    exampleWindow();
}
kefir500
  • 4,184
  • 6
  • 42
  • 48
  • Thanks for answer. I have a question for you, is it safe to use same time exec and show method. For example, first use show and than for the loop use exec? what do you think ? – EmreS Jul 09 '15 at 07:49
  • What's the point of using `exec()` and `show()` *simultaneously*? If you mean *alternation* of these two methods for different cases, then yes, technically it is safe (though be careful, because signals for `exec()` will also be fired). But I'm not sure why would you need the loop for `exec()`. – kefir500 Jul 09 '15 at 08:25
1

show() simply makes the dialog window visible. It is a QWidget method.

exec(), when provided in a class, always spins an event loop. In case of dialogs specifically, it'll make the dialog visible before spinning the event loop.

You could implement a functionally equivalent exec() yourself, as follows:

void myExec(QDialog * dialog) {
  QStateMachine sm;
  QState s1(&sm), s2(&sm);
  sm.setInitialState(&s1);
  QEventTransition transition(dialog, QEvent::Close);
  s2.addTransition(&transition);
  QEventLoop loop;
  QObject::connect(&s2, &QState::entered, &loop, &QEventLoop::quit);
  sm.start();
  dialog->show();
  loop.exec();
}

Generally speaking, you should never use exec() to create a nested event loop, since you're exposing a lot of your own code to possible reentrancy requirements. In case of dialogs, it is always possible to show a modal dialog box without using exec(), so there's really no point to it.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Hi, thanks for answer. This is a good solution for me but i have a question for you the loop is terminates when application is closed. But i want to terminate the loop when the QDialog is closed. How can i do that ? – EmreS Jul 13 '15 at 14:17
  • @EmreS That's what the `exec()` in the answer is doing, and that's what `QDialog::exec()` is doing as well. If you have another question, please post it separately. – Kuba hasn't forgotten Monica Jul 13 '15 at 15:15
  • [here another post](http://stackoverflow.com/questions/31399044/how-can-i-terminate-qstatemachine-when-qdialog-is-closed) – EmreS Jul 14 '15 at 10:16
0

exec() method shows modal dialog as described here

You can use for example closeEvent() of your dialog. In it you can write something like:

void ExmpleWin::closeEvent(QCloseEvent *event)
{
    if (/*Your condition*/) {
        emit signalUpdateMyData();
        event->accept();
    }
}

Also you have to connect signal signalUpdateMyData() with apropriate slot where you can make:

exampleWindow->UpdateCalibrationData(&data);
exampleWindow->UpdateFilterData(&filterData);
exampleWindow();
Max Pashkov
  • 404
  • 1
  • 3
  • 12