Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. [Connecting two mainwindows in Qt]
QtWS25 Last Chance

[Connecting two mainwindows in Qt]

Scheduled Pinned Locked Moved Solved Qt 6
7 Posts 3 Posters 1.6k Views
  • 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.
  • A Offline
    A Offline
    appdev
    wrote on last edited by
    #1

    Hello guys,

    I hope you're all doing well.

    So I've tried some examples on how to open another window (dialog) from a mainwindow but now I want to do the reverse thing.

    I've developed an application Where I have a mainwindow.ui that appears first once I run my program. The user can rotate an object either manually by inserting quaternion values or through importing a csv file that contains quaternion values.
    But I wanna add another mainwindow that appears before this one, add a push button that takes the user to the other mainwindow where he can see the object orientation.

    So When the dialog appears once I run my program, I click on the push button " take me to mainwindow", the mainwindow appears and then disappears immediately.

    Can someone please tell me what's wrong with my program:

    Here is the code:

    My main.cpp:

    #include "mainwindow.h"
    #include "secdialog.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
    //    QApplication a(argc, argv);
    //    MainWindow w;
    //    w.show();
    
        QApplication ab (argc, argv);
        SecDialog secdialog;
        secdialog.show();
    
        return ab.exec();
    }
    
    

    My SecDialog.cpp:

    #include "mainwindow.h"
    
    
    SecDialog::SecDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::SecDialog)
    {
        ui->setupUi(this);
    }
    
    SecDialog::~SecDialog()
    {
        delete ui;
    }
    
    void SecDialog::on_pushButton_clicked()
    {
        MainWindow w;
        w.show();
        //window.setModal(true);
    
    }
    
    

    Thank you all.

    JonBJ 1 Reply Last reply
    0
    • A appdev

      Hello guys,

      I hope you're all doing well.

      So I've tried some examples on how to open another window (dialog) from a mainwindow but now I want to do the reverse thing.

      I've developed an application Where I have a mainwindow.ui that appears first once I run my program. The user can rotate an object either manually by inserting quaternion values or through importing a csv file that contains quaternion values.
      But I wanna add another mainwindow that appears before this one, add a push button that takes the user to the other mainwindow where he can see the object orientation.

      So When the dialog appears once I run my program, I click on the push button " take me to mainwindow", the mainwindow appears and then disappears immediately.

      Can someone please tell me what's wrong with my program:

      Here is the code:

      My main.cpp:

      #include "mainwindow.h"
      #include "secdialog.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
      //    QApplication a(argc, argv);
      //    MainWindow w;
      //    w.show();
      
          QApplication ab (argc, argv);
          SecDialog secdialog;
          secdialog.show();
      
          return ab.exec();
      }
      
      

      My SecDialog.cpp:

      #include "mainwindow.h"
      
      
      SecDialog::SecDialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::SecDialog)
      {
          ui->setupUi(this);
      }
      
      SecDialog::~SecDialog()
      {
          delete ui;
      }
      
      void SecDialog::on_pushButton_clicked()
      {
          MainWindow w;
          w.show();
          //window.setModal(true);
      
      }
      
      

      Thank you all.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @appdev
      You must not declare MainWindow w; as a local variable inside a function like on_pushButton_clicked() --- it goes out of scope and is destroyed as soon as that function exits.

      Your SecDialog secdialog; might easiest be placed as a member variable of MainWindow (there are other ways). Then you can write connect()s to slots there which show() whichever of the two windows you want shown and hide() the other one. Or you might give SecDialog secdialog; the same scope/lifetime as your MainWindow w; back in main(), and connect whatever signals from them to code which show()s/hide()s the two windows appropriately.

      As a separate point, quite why you would want multiple QMainWindows is beyond me: I would only ever have one instance of a main window in an application. I don't understand your explanation/reasoning/intention, and I don't think you further explaining it would mean much to me! :)

      1 Reply Last reply
      3
      • A Offline
        A Offline
        appdev
        wrote on last edited by
        #3

        Okay but in the tutorial where he opens a second dialog from a mainwindow he does this, he used a new SecDialog inside the pushbutton

        Mainwindow.cpp:

        void MainWindow::on_pushButton_clicked()
        {
           secDialog = new SecDialog(this);
           secDialog->show();
        }
        

        and it works for him, without getting errors or using signals and slots.
        He's opening a second dialog from a mainwindow. I want to do the reverse thing.

        I do have a mainwindow that has push buttons and a widget in which there is an OpenGL scene. But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow. that's my intention.

        jsulmJ JonBJ 2 Replies Last reply
        0
        • A appdev

          Okay but in the tutorial where he opens a second dialog from a mainwindow he does this, he used a new SecDialog inside the pushbutton

          Mainwindow.cpp:

          void MainWindow::on_pushButton_clicked()
          {
             secDialog = new SecDialog(this);
             secDialog->show();
          }
          

          and it works for him, without getting errors or using signals and slots.
          He's opening a second dialog from a mainwindow. I want to do the reverse thing.

          I do have a mainwindow that has push buttons and a widget in which there is an OpenGL scene. But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow. that's my intention.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @appdev said in [Connecting two mainwindows in Qt]:

          and it works for him

          Because in this code SecDialog is allocated on the heap, not stack. You need to understand the difference.
          If you allocate an object on the stack in a function/method then this object does only exist as long as the function/method is beeing executed! When the function/method finishes all variables allocated on the stack are destroyed!

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • A appdev

            Okay but in the tutorial where he opens a second dialog from a mainwindow he does this, he used a new SecDialog inside the pushbutton

            Mainwindow.cpp:

            void MainWindow::on_pushButton_clicked()
            {
               secDialog = new SecDialog(this);
               secDialog->show();
            }
            

            and it works for him, without getting errors or using signals and slots.
            He's opening a second dialog from a mainwindow. I want to do the reverse thing.

            I do have a mainwindow that has push buttons and a widget in which there is an OpenGL scene. But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow. that's my intention.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @appdev said in [Connecting two mainwindows in Qt]:

            But I want to add another window that appears before the mainwindow and has a pushbutton that leads to the mainwindow.

            Sounds like a QDialog. And that will be easier to code (won't need signals/slots) to show, accept input, disappear and then move onto main window than what you had with a secondary non-dialog window.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              appdev
              wrote on last edited by
              #6

              @jsulm Okay that's clear, But I still need to read about heap and stack.
              @JonB that's exactly what I wanted to do , I used a QDialog to open the mainwindow like shown in the code above.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                appdev
                wrote on last edited by
                #7

                It worked like this.
                Thank you all for your precious help.

                void SecDialog::on_pushButton_clicked()
                {
                    hide();
                    MainWindow *mainWindow = new MainWindow();
                    mainWindow->show();
                 
                
                }
                
                
                
                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