John Smith
John Smith

Reputation: 861

How to show only one window in qt?

I have two MainWindows with WindowModality equals to Window and I would like that the the second window is shown after the first is closed (and valid).

The problem : when I use this code, I have two windows a the same time

int main(int argc, char *argv[])
{
    LoginWindow loginWin;
    loginWin.show();

    MainWindow w;
    w.show();

    return a.exec();
}

Upvotes: 0

Views: 183

Answers (1)

Evgeny
Evgeny

Reputation: 4010

You can add some signal to LoginWindow and emit it when user close it and its valid:

class LoginWindow: public QMainWindow
{
.......
signals:
    void loginReady();
.....
};

Then in your main function you can do somethisn like this:

int main(int argc, char *argv[])
{
    ....
    LoginWindow loginWin;
    connect(&loginWin, &LoginWindow::loginReady, [](){
             MainWindow* w = new MainWindow();
             w->show();
    });
    loginWin.show();

    return a.exec();
}

The lambda connected to loginReady signal will be executed when you call emit loginReady().

Of course you should add CONFIG += c++11 into your .pro file.

Upvotes: 3

Related Questions