[SOLVED]How to show QDialog after QMainWindow



  • Hello,

    I want to show QDialog object directly after showing QMainWindow using exec() how can I do it?

    I use the following approach but it doesn't work correctly in QMainWindow constructor.

    [code]MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    //TODO: Login page
    Login *dlg = new Login(this);
    ui->stackedWidget->addWidget(dlg);
    ui->stackedWidget->setCurrentWidget(dlg);
    //FIXME: unable to load the app
    int result = dlg->exec();
    qDebug(&#41; << result;
    if (result == QDialog::Accepted) {
        ui->stackedWidget->setCurrentIndex(0);
        ui->stackedWidget->removeWidget(dlg);
        delete dlg;
    } if (result == QDialog::Rejected) {
        qApp->quit();
    }
    

    }[/code]



  • exec is blocking call. So your constructor is not executed completely. You can move the dialog code out of MainWindow constructor and call it separately. Inside the constructor just do initialization.

    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    dlg = new QDialog(this);
    dlg->setWindowTitle("MyDialog");
    }

    void MainWindow::showlogin(){
    int result = dlg->exec();
    qDebug() << result;

    if (result == QDialog::Accepted) {
        delete dlg;
    } if (result == QDialog::Rejected) {
        qApp->quit();
    }
    

    }@



  • Calling QDialog separately will missing the point I want. I need to call it directly after showing the mainwindow.

    Is there any trick to get a similar behavior without calling it from the constructor?


  • Lifetime Qt Champion

    Hi,

    Use QTimer::singleShot with 0 as timeout value in your constructor to call showlogin



  • [quote author="SGaist" date="1404075900"]Hi,

    Use QTimer::singleShot with 0 as timeout value in your constructor to call showlogin[/quote]

    Thank you... That's it.


Log in to reply
 

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