Switching between two ui forms / QMainWindow screens
-
Hi,
I am building a QT application (QT4.8), where it has two ui forms, two threads one is main/GUI thread and other is worker thread with timer running.
It has one Base class derived from QObject, and two other classes for ui screens derived from QMainWindow. Base class contains the pointers for ui classes.
What is required is when application loads it should display the first screen , QMainWindow form , for that I am using show() function with CTOR of base class (shown below).
Now when timer in the thread goes off it calls the SIGNAL function in the base class and at that time it should switch to second ui or replace with second screen.
Issue:
First screen is shown properly but when it tries to switch to second screen within SIGNAL function, display becomes blank i.e. it goes off.Any kind of help/suggestion is appreciated,
Thank you,
Code:
//CTOR
Base::Base()
{
qDebug() << "Base CTOR called\n";//Show the first screen, loading raptor status
this->lrsGUI = new RaptorLrsGUI;
this->lrsGUI->setlabel(QString("Loading Status..."));
this->lrsGUI->showMaximized();
this->lrsGUI->show();this->coreGUI = new RaptorCoreGUI;
}/SIGNAL function/
void Base::rcvInitData()
{
qDebug()<< "Main Thread (CALLBACK): Swtch screen\n";this->lrsGUI->hide();
this->coreGUI->setlabel(QString("Second Screen..."));
this->coreGUI->showMaximized();
this->coreGUI->show();
bootInitData = 1;qDebug()<< "Main Thread (CALLBACK): EXiting Display initial screen\n";
} -
Hi and welcome to devnet,
Can you also show how you are setting the threads ?
From your description it seems that you are accessing GUI elements from another thread which is not possible.
-
Hi SGaist ,
Thankyou,
I know that we cannot access GUI elements from worker thread and I think I am not doing that, but still below is the complete code for you.
The SLOT function which switches the ui is rcvInitData().Files:
clientthread.h (worker thread)
coregui.h (second ui form/screen)
lsrgui.h (first ui form/screen)
base.hclientthread.cpp
coregui.cpp
lsrgui.cpp
main.cpp/clientthread.h/
class ClientThread : public QThread
{
Q_OBJECTsignals:
void sendInitData();
private:
void run();
QString m_lastTime;
private slots:
void timerHit();};
/coregui.h/
namespace Ui {
class RaptorCoreGUI;
}class RaptorCoreGUI : public QMainWindow
{
Q_OBJECTpublic:
explicit RaptorCoreGUI(QWidget *parent = 0);
~RaptorCoreGUI();
void setlabel(QString);private:
Ui::RaptorCoreGUI *ui;
};/lsrgui.h/
namespace Ui {
class RaptorLrsGUI;
}class RaptorLrsGUI : public QMainWindow
{
Q_OBJECTpublic:
explicit RaptorLrsGUI(QWidget *parent = 0);
~RaptorLrsGUI();
void setlabel(QString);private:
Ui::RaptorLrsGUI *ui;
};/base.h/
class Base : public QObject
{
Q_OBJECT
public:
RaptorLrsGUI *lrsGUI;
RaptorCoreGUI *coreGUI;
QStackedWidget *stackedWidget;
Base();private slots:
void rcvInitData();
void rcvLPNotif();
void rcvErrCounterInfo();
void rcvGenNotif();
};**/clientthread.c /
void ClientThread::run()
{
QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(timerHit()), Qt::DirectConnection);
timer.setInterval(5000);
timer.start(); // puts one event in the threads event queue
exec();
timer.stop();
}void ClientThread::timerHit()
{
qDebug() << "Client Thread: Emitting sendMsg signal\n";
emit sendInitData();
}/coregui.cpp/
RaptorCoreGUI::RaptorCoreGUI(QWidget *parent) :
QMainWindow(parent, Qt::FramelessWindowHint),
ui(new Ui::RaptorCoreGUI)
{
ui->setupUi(this);
this->setAutoFillBackground(true);
this->setStyleSheet("background-color:white;");
}RaptorCoreGUI::~RaptorCoreGUI()
{
delete ui;
}void RaptorCoreGUI::setlabel(QString label)
{
ui->label->setText(label);
}/lsrgui.cpp/
RaptorLrsGUI::RaptorLrsGUI(QWidget *parent) :
QMainWindow(parent, Qt::FramelessWindowHint),
ui(new Ui::RaptorLrsGUI)
{ui->setupUi(this); this->setAutoFillBackground(true); this->setStyleSheet("background-color:white;");
}
RaptorLrsGUI::~RaptorLrsGUI()
{
delete ui;
}void RaptorLrsGUI::setlabel(QString label)
{
ui->label->setText(label);
}/base.cpp/
Base::Base()
{
qDebug() << "Base CTOR called\n";//Show the first screen, loading raptor status this->lrsGUI = new RaptorLrsGUI; this->lrsGUI->setlabel(QString("Loading Raptor Status...")); this->lrsGUI->showMaximized(); this->lrsGUI->show(); //this->coreGUI = coreGUI; this->coreGUI = new RaptorCoreGUI;
}
void Base::rcvInitData()
{
this->lrsGUI->hide();
this->coreGUI->setlabel(QString("Raptor Status..."));
this->coreGUI->setGeometry(480,272,-0,+128);
this->coreGUI->showMaximized();
this->coreGUI->show(); // ISSUE: DOES NOT SHOW THE SECOND GUI, BUT IF WE COMMENT OUT THE SHOW OF FIRST GUI (this->lrsGUI->show()),
THEN SECOND ONE IS SHOWN PROPERLYqDebug()<< "Main Thread (CALLBACK): EXiting Display initial screen\n";
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
RaptorLrsGUI LrsGui;
RaptorCoreGUI CoreGui;Base baseObj; //instantiate Client thread object ClientThread clientThread; QObject::connect(&clientThread, SIGNAL(sendInitData()), &baseObj, SLOT(rcvInitData()), Qt::QueuedConnection); qDebug() << "Main Thread: starting clockThread\n"; clientThread.start(); app.exec(); qDebug() << "Mian Thread: Quiting clockThread\n"; clientThread.quit(); qDebug() << "Main Thread: Waiting on clockThread \n"; clientThread.wait(); return 0;
}
-
Not to sound rude but that code base is a bit messy:
- You have two **Gui objects in you main.cpp that are not used at all.
- You are setting a geometry on your coreGui object but then call showMaximized followed by show.
Also using a custom thread only for a 5 second timer is way overkill.
What exactly do you want to do with your application ?
-
@SGaist said in Switching between two ui forms / QMainWindow screens:
ou have two **Gui objects in you main.cpp that are not used at all.
You are setting a geometry on your coreGui object but then call showMaximized followed by show.Also using a custom thread only for a 5 second timer is way overkill.
What exactly do you want to do with your application ?Yes I know , this is kind of a PoC I am doing and this is what I want to achieve:
- Keep two separate GUIs (ui forms).
- When application starts, it should spawn a thread which will run timer and at the same time display first GUI on screen (happens in C'TOR).
- When the timer of the thread goes off, it will emit its SIGNAL and SLOT function of main will get called which will stop the first GUI and replace with second one.
Thanks
-
Which platform r u trying this ? Is it embedded box or desktop ? I have sample for ur use case. I can share you. It works perfectly.
-
Hi, Its for embedded Linux.
-
Does you logic works on desktop platform ?
-
And why not just use a single shot QTimer ?
I really fail to see the use for that secondary thread except needless complexity.
-
Hi,
This is just for PoC, BelowI created application with one QMainWindow class and two ui classes derived from QWidget. Then I used QStackedWidget to switch the ui forms but
I cannot see anything on screen.
Thank you for help,/Second UI */ namespace Ui { class RaptorCoreGUI; } class RaptorCoreGUI : public QWidget { Q_OBJECT public: explicit RaptorCoreGUI(QWidget *parent = 0); ~RaptorCoreGUI(); void configInitData(bool); void setlabel(QString); private: Ui::RaptorCoreGUI *ui; }; /*First UI*/ amespace Ui { class RaptorLrsGUI; } class RaptorLrsGUI : public QWidget { Q_OBJECT public: explicit RaptorLrsGUI(QWidget *parent = 0); ~RaptorLrsGUI(); void setlabel(QString); private: Ui::RaptorLrsGUI *ui; }; /*Base class derived from QMainWindow*/ class Base : public QMainWindow { Q_OBJECT public: RaptorLrsGUI *lrsGUI; RaptorCoreGUI *coreGUI; QStackedWidget *stackedWidget; QVBoxLayout *layout; ~Base(); explicit Base (QWidget *parent = 0); private slots: void rcvInitData(); }; /*Second UI CPP*/ RaptorCoreGUI::RaptorCoreGUI(QWidget *parent) : QWidget(parent), ui(new Ui::RaptorCoreGUI) { ui->setupUi(this); this->setAutoFillBackground(true); this->setStyleSheet("background-color:white;"); } RaptorCoreGUI::~RaptorCoreGUI() { delete ui; } void RaptorCoreGUI::setlabel(QString label) { qDebug() << "Main Thread(COREGUI): Setting Label\n"; ui->label->setText(label); } /*First UI cpp*/ RaptorLrsGUI::RaptorLrsGUI(QWidget *parent) : QWidget(parent), ui(new Ui::RaptorLrsGUI) { ui->setupUi(this); this->setAutoFillBackground(true); this->setStyleSheet("background-color:white;"); } RaptorLrsGUI::~RaptorLrsGUI() { qDebug() << "Main Thread: destroying the ui object \n"; delete ui; } void RaptorLrsGUI::setlabel(QString label) { qDebug() << "Main Thread(LSRGUI): Setting Label\n"; ui->label->setText(label); } /*main.cpp*/ Base::Base(QWidget *parent) : QMainWindow(parent, Qt::FramelessWindowHint) { qDebug() << "Base CTOR called\n"; QPalette pal = palette(); // retrieves the default palette for the widget, as defined by the app's style pal.setColor( QPalette::Window, Qt::white ); setPalette( pal ); setAutoFillBackground( true ); //Show the first screen, loading raptor status lrsGUI = new RaptorLrsGUI; lrsGUI->setlabel(QString("Loading Raptor Status...")); coreGUI = new RaptorCoreGUI; stackedWidget = new QStackedWidget(this); stackedWidget->addWidget(lrsGUI); stackedWidget->addWidget(coreGUI); stackedWidget->setCurrentIndex(0); setCentralWidget(stackedWidget); } Base::~Base() { qDebug() << "Main Thread: destroying base objects \n"; delete lrsGUI; delete coreGUI; delete stackedWidget; } void Base::rcvInitData() { qDebug()<< "Main Thread (CALLBACK): Display second screen\n"; stackedWidget->setCurrentIndex(1); } //! [1] int main(int argc, char *argv[]) { QApplication app(argc, argv); //Instantiate base class Base baseObj; //instantiate Client thread object ClientThread clientThread; qDebug() << "Connecting sendmsg and handle_callback1()\n"; QObject::connect(&clientThread, SIGNAL(sendInitData()), &baseObj, SLOT(rcvInitData()), Qt::QueuedConnection); qDebug() << "Main Thread: starting clockThread\n"; clientThread.start(); app.exec(); qDebug() << "Mian Thread: Quiting clockThread\n"; clientThread.quit(); qDebug() << "Main Thread: Waiting on clockThread \n"; clientThread.wait(); return 0; }
-
Is it me or are you missing a call to
baseObj.show();
in yourmain.cpp
? -
You're welcome !
Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)