Solved 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. -
@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()
-
@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-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
MainWindow1 w1;
-
w1.show();
-
MainWindow2 w2;
-
w2.show();
-
return a.exec();
-
}
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.
-
-
@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.