Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to show QDialog after QMainWindow

[SOLVED]How to show QDialog after QMainWindow

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbnoimi
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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();
      }
      

      }@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbnoimi
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved