How can QDialog lock a QMainWindow?

3

I am implementing an experimental registration program, and I made a case where, if a person does not type and tries to register this empty data, it opens 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 (main program window) has an object of type dialog that is implemented by 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 make MainWindow only clickable after the dialog window is closed? Because even with the open dialog window I can still move and perform another registration in the main window.

Excerpt that opens the dialog if the user does not enter the data and tries to register (located at 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 dialog object above is running the show() function.

    
asked by anonymous 26.04.2015 / 19:41

1 answer

3

If understood, the behavior you want can be achieved by changing the WindowModality of your 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 windows of the program until the dialog is closed.

Another possible behavior is to use Qt::WindowModal , so only the window responsible for the dialog (in this case MainWindow ) will be inaccessible.

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

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    dialog(this) // atenção nessa linha
{
    // [...]
}
    
26.04.2015 / 22:08