Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • #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.


  • Moderators

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


  • 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()


  • Moderators

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


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


  • Moderators

    @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!



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


Log in to reply