Results 1 to 15 of 15

Thread: State Machine Framework haging the UI when using it on a Qthread

  1. #1
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:

    Qt Code:
    1. #include "frogs.h"
    2.  
    3.  
    4. frogs::frogs(QWidget *parent) :
    5. {
    6. imagenrana = new QLabel(parent);
    7.  
    8. imagenrana->setPixmap(QPixmap(":frog1.png"));
    9. imagenrana->move(50, 50);
    10. imagenrana->show();
    11. imagenrana->setAttribute(Qt::WA_DeleteOnClose);
    12.  
    13. // imagenrana->close();
    14.  
    15. automataRana =new QStateMachine();
    16.  
    17. QState * sReposo = new QState();
    18. QState * sMorir = new QState();
    19. QState * sDescansar = new QState();
    20.  
    21. automataRana->addState(sReposo);
    22. automataRana->addState(sMorir);
    23. automataRana->addState(sDescansar);
    24. sReposo->addTransition(this, SIGNAL(tocada()), sMorir);
    25. sReposo->addTransition(this, SIGNAL(cansada()), sDescansar);
    26.  
    27. connect(sReposo, SIGNAL(entered()), this, SLOT (esperar()));
    28. connect(sDescansar, SIGNAL(entered()), this, SLOT (esconder()));
    29. connect(sMorir, SIGNAL(entered()), this, SLOT (morir()));
    30.  
    31.  
    32.  
    33.  
    34. automataRana->setInitialState(sReposo);
    35.  
    36.  
    37.  
    38. }
    39.  
    40.  
    41. void frogs::run() {
    42. automataRana->start();
    43. // QThread::sleep(5);
    44. // imagenrana->close();
    45. }
    46.  
    47. void frogs::esperar() {
    48. QThread::sleep(5);
    49. // for (int i=0;i<5;i++) {
    50. // sleep(1);
    51. // this->exec();
    52. // }
    53. emit cansada() ;
    54. }
    55.  
    56.  
    57.  
    58.  
    59. void frogs::esconder() {
    60. imagenrana->close();
    61. this->~QThread();
    62. }
    63.  
    64. void frogs::morir() {
    65. this->~QThread();
    66. }
    To copy to clipboard, switch view to plain text mode 


    Here is how I call the thread:



    Qt Code:
    1. frogs * rana = new frogs(background);
    2.  
    3. rana->start();
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 7th October 2011 at 20:46. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    Thanks for your useful help wysota.

    Quote Originally Posted by wysota View Post
    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

    Qt Code:
    1. #include "frogs.h"
    2.  
    3.  
    4. frogs::frogs(QWidget *parent) :
    5. {
    6.  
    7.  
    8. }
    9.  
    10.  
    11. void frogs::run() {
    12.  
    13. automataRana =new QStateMachine();
    14.  
    15. QState * sReposo = new QState();
    16. QState * sMorir = new QState();
    17. QState * sDescansar = new QState();
    18.  
    19. automataRana->addState(sReposo);
    20. automataRana->addState(sMorir);
    21. automataRana->addState(sDescansar);
    22. sReposo->addTransition(this, SIGNAL(tocada()), sMorir);
    23. sReposo->addTransition(this, SIGNAL(cansada()), sDescansar);
    24.  
    25.  
    26.  
    27. automataRana->setInitialState(sReposo);
    28.  
    29.  
    30. connect(sReposo, SIGNAL(entered()), this, SLOT (esperar()));
    31. connect(sDescansar, SIGNAL(entered()), this, SLOT (esconder()));
    32. connect(sMorir, SIGNAL(entered()), this, SLOT (morir()));
    33. connect(this, SIGNAL(cansada()), this, SLOT (esperar()));
    34.  
    35. automataRana->start();
    36.  
    37. // imagenrana->close();
    38. }
    39.  
    40. void frogs::esperar() {
    41. qDebug("He entrado en Esperar");
    42. QThread::sleep(5);
    43. emit cansada() ;
    44. }
    45.  
    46.  
    47.  
    48.  
    49. void frogs::esconder() {
    50. // imagenrana->close();
    51. }
    52.  
    53. void frogs::morir() {
    54. // imagenrana->close();
    55. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    Quote Originally Posted by wysota View Post
    Why should the slot be executed? Apparently you never enter the sReposo state.
    Actually yes, it is the initial state:

    Qt Code:
    1. automataRana->setInitialState(sReposo);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    Quote Originally Posted by wysota View Post
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    Quote Originally Posted by wysota View Post
    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?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    Quote Originally Posted by wysota View Post
    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?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Sep 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: State Machine Framework haging the UI when using it on a Qthread

    the problem is finally soved.

    The code is like:
    Qt Code:
    1. frogs * rana = new frogs(background);
    2. rana->moveToThread(rana);
    3. rana->start();
    To copy to clipboard, switch view to plain text mode 

    Thank you very much for your help wysota.

    Best Regards,

    A. Cano

Similar Threads

  1. state machine in its own thread
    By shaolin in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2011, 13:45
  2. State Machine returning to previous state
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2011, 00:39
  3. Replies: 0
    Last Post: 19th December 2010, 16:03
  4. QTouchEvent and State Machine Framework
    By acano in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2010, 07:58
  5. How to get this Qt state machine to work?
    By blukske in forum Qt Programming
    Replies: 0
    Last Post: 1st April 2010, 10:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.