How can Qdialog lock a Qmainwindow?

Asked

Viewed 145 times

3

I am implementing an experimental registration program, and I made a case where, if a person types nothing and tries to register this empty data, it opens a QDialog, receives the warning and does not complete the registration. Basically the code is:

Mainwindow class:

#include "dado.h"
#include "dialog.h"

class MainWindow : public QMainWindow
{

    [...]

    private slots:
        void on_botaoInserir_clicked();  

private:
    Ui::MainWindow *ui;

    Dado *dado;

    Dialog dialog;
};

The class MainWindow (program main window) has an object of type dialog that is implemented by the class Dialog:

Dialog class:

#include <QDialog>

namespace Ui {
    class Dialog;
}

class Dialog : public QDialog
{
     Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);

    ~Dialog();

     void setAviso(QString);

     QString getAviso();

private slots:
    void on_dialogButton_clicked();

private:
    Ui::Dialog *ui;
};

How to get the MainWindow only become clickable after the dialog window is closed? Because even with the dialog window open I can still move and perform another registration in the main window.

Excerpt that opens the dialogue if the user does not inform the data and try to register (located in MainWindow):

    [...]

    ui->statusBar->showMessage("Pessoa adicionada.", SB_MESS_TIME);
}
else{

    dialog.setAviso("Digite um nome.");
    dialog.setFocus();
    dialog.show();
}

    [...]

I hope someone says a way to "lock" the main window while the object dialog above is running the function show().

  • I believe this occurs in the instantiation of dialog. Please post the code so we can help you :)

  • The instantiation is in the third code. It uses a dialog object created based on the first code.

1 answer

3


If I understand correctly, the behavior you want can be obtained by changing the property WindowModality of his QDialog:

dialog.setAviso("Digite um nome.");
dialog.setWindowModality(Qt::ApplicationModal);
dialog.show();

The above code will display the dialog on the screen and prevent any interaction with all other program windows until the dialog is closed.

Another possible behavior is to use Qt::WindowModal, thus only the window responsible for the dialogue (in case the MainWindow) will become inaccessible.

dialog.setAviso("Digite um nome.");
dialog.setWindowModality(Qt::WindowModal);
dialog.show();

However it is necessary that you define who is the window responsible for the dialog, changing the "constructor" of your MainWindow for something like:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    dialog(this) // atenção nessa linha
{
    // [...]
}
  • Wouldn’t it be easier to put the QWidget* parent as the MainWindow?

  • It was exactly this solution that I was looking for, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.