State Machine Framework haging the UI when using it on a Qthread
Hi everyone,
I am using the State Machine framework to simulate the behavious of some animations.
The problem is that when I do so the UI hangs.
Could someone help?, please.
Here is my code:
Code:
#include "frogs.h"
{
imagenrana
= new QLabel(parent
);
imagenrana
->setPixmap
(QPixmap(":frog1.png"));
imagenrana->move(50, 50);
imagenrana->show();
imagenrana->setAttribute(Qt::WA_DeleteOnClose);
// imagenrana->close();
automataRana =new QStateMachine();
QState * sReposo = new QState();
QState * sMorir = new QState();
QState * sDescansar = new QState();
automataRana->addState(sReposo);
automataRana->addState(sMorir);
automataRana->addState(sDescansar);
sReposo->addTransition(this, SIGNAL(tocada()), sMorir);
sReposo->addTransition(this, SIGNAL(cansada()), sDescansar);
connect(sReposo, SIGNAL(entered()), this, SLOT (esperar()));
connect(sDescansar, SIGNAL(entered()), this, SLOT (esconder()));
connect(sMorir, SIGNAL(entered()), this, SLOT (morir()));
automataRana->setInitialState(sReposo);
}
void frogs::run() {
automataRana->start();
// QThread::sleep(5);
// imagenrana->close();
}
void frogs::esperar() {
// for (int i=0;i<5;i++) {
// sleep(1);
// this->exec();
// }
emit cansada() ;
}
void frogs::esconder() {
imagenrana->close();
}
void frogs::morir() {
}
Here is how I call the thread:
Code:
frogs * rana = new frogs(background);
rana->start();
Re: State Machine Framework haging the UI when using it on a Qthread
Do the transitions involve some animations or property changes of some QObjects? It seems "imagenrana" is a widget, you can't access those from within threads. Furthermore your state machine lives in the main thread and not in the thread you create thus you can't access it from the thread either.
Re: State Machine Framework haging the UI when using it on a Qthread
Thanks for your useful help wysota.
Quote:
Originally Posted by
wysota
Furthermore your state machine lives in the main thread and not in the thread you create thus you can't access it from the thread either.
Can I make my state machine to live in the thread I create?
Re: State Machine Framework haging the UI when using it on a Qthread
Yes, create it in the run() method of your thread. or use QObject::moveToThread().
Re: State Machine Framework haging the UI when using it on a Qthread
I tried to do it by creating the State Machine in the run() method. But it is still not working: the slot SLOT (esperar()) is never executed.
Do you have some idea about why is it happening.
Thaks again!!
Code:
#include "frogs.h"
{
}
void frogs::run() {
automataRana =new QStateMachine();
QState * sReposo = new QState();
QState * sMorir = new QState();
QState * sDescansar = new QState();
automataRana->addState(sReposo);
automataRana->addState(sMorir);
automataRana->addState(sDescansar);
sReposo->addTransition(this, SIGNAL(tocada()), sMorir);
sReposo->addTransition(this, SIGNAL(cansada()), sDescansar);
automataRana->setInitialState(sReposo);
connect(sReposo, SIGNAL(entered()), this, SLOT (esperar()));
connect(sDescansar, SIGNAL(entered()), this, SLOT (esconder()));
connect(sMorir, SIGNAL(entered()), this, SLOT (morir()));
connect(this, SIGNAL(cansada()), this, SLOT (esperar()));
automataRana->start();
// imagenrana->close();
}
void frogs::esperar() {
qDebug("He entrado en Esperar");
emit cansada() ;
}
void frogs::esconder() {
// imagenrana->close();
}
void frogs::morir() {
// imagenrana->close();
}
Re: State Machine Framework haging the UI when using it on a Qthread
Why should the slot be executed? Apparently you never enter the sReposo state.
Re: State Machine Framework haging the UI when using it on a Qthread
Quote:
Originally Posted by
wysota
Why should the slot be executed? Apparently you never enter the sReposo state.
Actually yes, it is the initial state:
Code:
automataRana->setInitialState(sReposo);
Re: State Machine Framework haging the UI when using it on a Qthread
If it is the initial state then you never enter it. You are already in it when the machine starts running.
Re: State Machine Framework haging the UI when using it on a Qthread
Quote:
Originally Posted by
wysota
If it is the initial state then you never enter it. You are already in it when the machine starts running.
Believe me. It does not work.
I emited SIGNAL(tocada()) and SLOT (morir()) is not executed.
There is still something wrong in my code.
Anyway, thank you very much for your help.
Re: State Machine Framework haging the UI when using it on a Qthread
I think the problem is that your run() returns right after the machine is started and the thread stops running. Try calling exec() at the end of your run() method.
Re: State Machine Framework haging the UI when using it on a Qthread
Quote:
Originally Posted by
wysota
I think the problem is that your run() returns right after the machine is started and the thread stops running. Try calling exec() at the end of your run() method.
May be that is the reason.
I tried calling exec() at then end, but then the main Thread is stalled again.
Do you think that trying with QThread::wait() is a good idea?
Re: State Machine Framework haging the UI when using it on a Qthread
Then maybe your thread is not running at all? You are starting it with start() and not run(), right?
Re: State Machine Framework haging the UI when using it on a Qthread
Quote:
Originally Posted by
wysota
Then maybe your thread is not running at all? You are starting it with start() and not run(), right?
Yes, I am starting with start(). Is there something wrong with it?
Re: State Machine Framework haging the UI when using it on a Qthread
It would be advised if you posted your complete code related to the thread and your main() function.
Re: State Machine Framework haging the UI when using it on a Qthread
the problem is finally soved.
The code is like:
Code:
frogs * rana = new frogs(background);
rana->moveToThread(rana);
rana->start();
Thank you very much for your help wysota.
Best Regards,
A. Cano