Chaining multiple UI with QStatemachine
Hi,
I'd like to use a QStatemachine to handle the state of an application. According to the current state of the SM, a different UI should appears like here :
<QSM states> [Start] -> [State1] -> [State2] ->[State3]->[State4]->etc
<UI> [Splash screen for 10s] -> [ UI1] -> [UI2] -> [ UI3 ] -> [UI4]
A button click create the transition between the states.
Whereas I could use the QStatemachine from Qt docs from a single ui, my problem is I don't see how to implement the relationship when SM changed in the main or MainWidow class to call
other classes/UI.
Re: Chaining multiple UI with QStatemachine
Each QState can set properties on objects.
For example the visible property of a window, or the currentIndex property of a widget stack.
Cheers,
_
Re: Chaining multiple UI with QStatemachine
OK but how to access different ui from the same place then ?
from my example the main (or MainWindow) starts the splash creen ui and then must hide it to call the UI1 [SM transition] ,
then do the same if the UI1/Btn click event arise[SM transition] : hide UI1 and show UI2, and so on.
edit : another approach could be to access the SM from the different ui to update the SM itself ?
Re: Chaining multiple UI with QStatemachine
If the main window creates all these UIs then it should be able to pass these pointers to the states' assignProperty() method.
You can also use a mediator object that has the properties which get changed by the state machine and other parts of the program access that mediator.
Doesn't really have anything to do with either Qt or QSM, basic C++ at work.
Cheers,
_
Re: Chaining multiple UI with QStatemachine
anda_skoa I see what you mean, but the UI are quite specific and are created from the designer, insteead by code.
Thus, they are not created by the Mainwindow.
For the same reasons, while being a really interesting idea, the mediator object can't have the different UI assigned to it.
Cheers
Re: Chaining multiple UI with QStatemachine
Quote:
Originally Posted by
dlewin
anda_skoa I see what you mean, but the UI are quite specific and are created from the designer, insteead by code.
Doesn't change anything.
Quote:
Originally Posted by
dlewin
Thus, they are not created by the Mainwindow.
They need to be created somewhere.
Quote:
Originally Posted by
dlewin
For the same reasons, while being a really interesting idea, the mediator object can't have the different UI assigned to it.
The mediator approach is the one you use when you don't have access to the UI pointers from where you create the state machine.
Hence the mediator being accessible from everywhere.
Cheers,
_
Re: Chaining multiple UI with QStatemachine
ok, I think I got it :
* I create a "MediatorWhatever" Class
* in "MediatorWhatever" I create a QStatemachine + instance of splash screen class
* from "Splashscreen" Class on time out (10s in my example) I update the SM state
* in "MediatorWhatever" I create an instance of UI1 class
* from "UI1" Class on Btn click event I update the SM state
* and so on ...
is that what you mean ?
Re: Chaining multiple UI with QStatemachine
No, but that would also work.
If you have a class that has access to all UI, either because it creates all of them or it receives all pointers, then it is a good place to create the state machine.
If that class is the main window class or a separate class really doesn't matter.
The mediator approach is something entirely different, just fortget about it :)
Cheers,
_
Re: Chaining multiple UI with QStatemachine
thanks?
Well ... ok I forget i.
what I don't see the way is : "it receives all pointers,"
Re: Chaining multiple UI with QStatemachine
What does your code look right now?
How and where do you create the instances of these UIs?
Cheers,
_
Re: Chaining multiple UI with QStatemachine
To simplify the explanations I'd say I work by validating function parts, that are :
* using a QStatemachine :
Code:
QState *s1 = new QState();
QState *s2 = new QState();
QState *s3 = new QState();
s1->assignProperty(&button, "text", "Click me"); //Property is assign only when state-machine is started.
machine.addState(s1);
machine.addState(s2);
machine.addState(s3);
machine.setInitialState(s1);
// Definition de l'enchainement des états
s1->addTransition(&button, SIGNAL(clicked()), s2);
s2->addTransition(&button, SIGNAL(clicked()), s3);
s3->addTransition(&button, SIGNAL(clicked()), s1);
// Definition de l'action des états
s1->assignProperty(label1, "text", "Splash screen");
s2->assignProperty(label1, "text", "1 Menu");
s3->assignProperty(label1, "text", "3 Maintenance");
machine.start();
layout->addWidget(&button);
layout->addWidget(label1);
------------------------------------------------------------
* I aim to use this code into whatever allows me to do what I want to, like any Qtcreator application wizard will generate :
I use the main (but not sure this is the best idea) :
Code:
SplashScreen *SScreen = new SplashScreen ();
SScreen ->show();
then in MainWindow I use :
Code:
void MainWindow::Splash_Timerout()
{
//this->show();
Frm_Warning_Sourceinvalide *Warning_window = new Frm_Warning_Sourceinvalide();
Warning_window->show();
}
Re: Chaining multiple UI with QStatemachine
Some options:
Code:
s1->assignProperty(splashScreen, "visible", true);
s2->assignProperty(warningWindow, "visible", true);
// and a transition that triggers s1 -> s2 when the slash closes/times out.
Code:
connect(s1, SIGNAL(entered()), someReceiver, SLOT(showSplash()));
connect(s2, SIGNAL(entered()), someReceiver, SLOT(showWarning()));
Cheers,
_
Re: Chaining multiple UI with QStatemachine
great indeed this is better.
Then : I still need to update the S1 transition from MainWindow when it's finished after 10s
Re: Chaining multiple UI with QStatemachine
That's what I wrote, no?
Code:
// and a transition that triggers s1 -> s2 when the slash closes/times out.
Cheers,
_
Re: Chaining multiple UI with QStatemachine
I meant : by code.
Thanks
Re: Chaining multiple UI with QStatemachine
I don't understand what you mean.
Of course adding a transition is done by code, how else would you do it?
Cheers,
_
Re: Chaining multiple UI with QStatemachine
It's ok.
I would paraphrase Magrite elsewhere :
// and a transition that triggers s1 -> s2 when the slash closes/times out.
"this is not a code" ;)