Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to switch between two w.show()'s in main?
Forum Updated to NodeBB v4.3 + New Features

How to switch between two w.show()'s in main?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 2 Posters 1.1k 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.
  • S Offline
    S Offline
    Sriu1
    wrote on last edited by
    #1

    #include "mainwindow1.h"
    #include "mainwindow2.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow1 w1;
    w1.show();
    MainWindow2 w2;
    w2.show();
    return a.exec();
    }

    In w1.show I have Finish button.After clicking "Finish" below
    MainWindow2 w2;
    w2.show();
    should execute and I also have "Back" button in w2.show() UI, on clicking it I should go back to w1.show().
    Is it possible through signals and slots?Because the signal might come from any of w1 or w2 instances? Please suggest.

    J.HilkJ 1 Reply Last reply
    0
    • S Sriu1

      #include "mainwindow1.h"
      #include "mainwindow2.h"
      #include <QApplication>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow1 w1;
      w1.show();
      MainWindow2 w2;
      w2.show();
      return a.exec();
      }

      In w1.show I have Finish button.After clicking "Finish" below
      MainWindow2 w2;
      w2.show();
      should execute and I also have "Back" button in w2.show() UI, on clicking it I should go back to w1.show().
      Is it possible through signals and slots?Because the signal might come from any of w1 or w2 instances? Please suggest.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @Sriu1
      it is, but I would rather suggest making a single parent Widget and handle it in there.

      if you have to do it in main, than MainWindow 1 needs a signal e.g buttonFinished that is emitted when the button is pressed and MainWindo2 needs a signal as well, e.g buttonBack

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow1 w1;
      w1.show();
      MainWindow2 w2;
      
      QObject::connect(&w1, &MainWindow1::buttonFinished, &w2, &MainWindow2::show);
      QObject::connect(&w1, &MainWindow1::buttonFinished, &w1, &MainWindow1::hide);
      
      QObject::connect(&w2, &MainWindow2::buttonBack, &w1, &MainWindow1::show);
      QObject::connect(&w2, &MainWindow2::buttonBack, &w2, &MainWindow2::hide);
      return a.exec();
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Sriu1
        wrote on last edited by
        #3

        But when main is executing it goes in a flow rite?w1.show() and w2.show() both will be executed.This should not happen.Should I use a condition ?when button finished is clicked do w2.show()

        J.HilkJ 1 Reply Last reply
        0
        • S Sriu1

          But when main is executing it goes in a flow rite?w1.show() and w2.show() both will be executed.This should not happen.Should I use a condition ?when button finished is clicked do w2.show()

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Sriu1 what do you mean?

          take this basic example:

          #include <QApplication>
          #include <QPushButton>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              QPushButton btn1("Finish");
              btn1.show();
          
              QPushButton btn2("Back");
          
              QObject::connect(&btn1, &QPushButton::clicked, &btn1, &QPushButton::hide);
              QObject::connect(&btn1, &QPushButton::clicked, &btn2, &QPushButton::show);
          
              QObject::connect(&btn2, &QPushButton::clicked, &btn2, &QPushButton::hide);
              QObject::connect(&btn2, &QPushButton::clicked, &btn1, &QPushButton::show);
          
          return a.exec();
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sriu1
            wrote on last edited by
            #5

            Hi @J-Hilk
            Consider the below code

            1.   int main(int argc, char *argv[])
              
            2.  {
              
            3.  QApplication a(argc, argv);
              
            4.      MainWindow1 w1;
              
            5.        w1.show();
              
            6.       MainWindow2 w2;
              
            7.        w2.show();
              
            8.        return a.exec();
              
            9.       }
              

            When w1.show() executes at line 14, I want line 15 and 16 to execute only when "Finish" is clicked in w1.show().Basically I want to pause the execution of main till finish signal is generated.

            J.HilkJ 1 Reply Last reply
            0
            • S Sriu1

              Hi @J-Hilk
              Consider the below code

              1.   int main(int argc, char *argv[])
                
              2.  {
                
              3.  QApplication a(argc, argv);
                
              4.      MainWindow1 w1;
                
              5.        w1.show();
                
              6.       MainWindow2 w2;
                
              7.        w2.show();
                
              8.        return a.exec();
                
              9.       }
                

              When w1.show() executes at line 14, I want line 15 and 16 to execute only when "Finish" is clicked in w1.show().Basically I want to pause the execution of main till finish signal is generated.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Sriu1
              have you tried the example main I provided?

              Basically I want to pause the execution of main till finish signal is generated.

              that's not how an event loop or an event driven program works!


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • S Offline
                S Offline
                Sriu1
                wrote on last edited by
                #7

                @J-Hilk
                Thanks for the reply.I tried your code.I was looking for something like that.

                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