Results 1 to 5 of 5

Thread: QTimer problem ... it runs but never triggs

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default QTimer problem ... it runs but never triggs

    Hi everybody !

    I have a strange problem this morning. I'm tryng to use a QTimer object in a class but it does not work.

    I have a class which has a QTimer objet as a member. this class has also a slot to process each timer timeout.

    I call a function to start the timer and check the return value of the function ... every thing is ok because the returned value is different from zero ... cool. I also check that there is no problem with the SIGNAL SLOT conection ... here again no problem. In fact there is a problem because the SLOT to process timer timeout is never called, why ?

    My header file class is :
    Qt Code:
    1. #include <qobject.h>
    2. #include "HmiStateA.h"
    3. #include "HmiState.h"
    4. #include "Hmi.h"
    5. #include <qtimer.h>
    6.  
    7. class CHmiStateB : public QObject, public CAvreHmiStateA
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. static CHmiState* GetInstance();
    13. virtual ~CHmiStateB();
    14.  
    15. protected:
    16. CHmiStateB();
    17. static CHmiStateB* m_Instance;
    18.  
    19. public:
    20. virtual void ShowHmi(CHmi*);
    21. virtual void HideHmi(CHmi*);
    22. virtual void ResetHmi(CHmi*);
    23. virtual void ValidateHmiItem(CHmi*);
    24. virtual void CancelHmiItem(CHmi*);
    25.  
    26. protected slots:
    27. void ProcessTimerTimeout();
    28. protected:
    29. QTimer* pTimer;
    30. };
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

    My implementation file class is :
    Qt Code:
    1. #include "HmiStateB.h"
    2. #include "HmiStateA.h"
    3. #include <qapplication.h>
    4.  
    5. CHmiStateB* CHmiStateB::m_Instance = 0;
    6.  
    7. CHmiStateB::CHmiStateB():
    8. CHmiStateA(),
    9. pTimer(0)
    10. {
    11. if( !pTimer )
    12. {
    13. pTimer = new QTimer;
    14. Q_ASSERT( pTimer );
    15. connect(pTimer, SIGNAL(timeout()), this, SLOT(ProcessTimerTimeout()));
    16. }
    17. }
    18.  
    19. CHmiStateB::~CHmiStateB()
    20. {
    21. if( pTimer )
    22. {
    23. if( pTimer->isActive() )
    24. pTimer->stop();
    25. delete pTimer;
    26. pTimer = 0;
    27. }
    28.  
    29. }
    30.  
    31. CHmiState* CHmiStateB::GetInstance()
    32. {
    33. if( !m_Instance )
    34. m_Instance = new CHmiStateB;
    35.  
    36. return m_Instance;
    37. }
    38.  
    39. void CHmiStateB::ShowHmi(CHmi* hmi)
    40. {
    41. if( !pTimer->start(4000, TRUE) )
    42. qDebug("Failed to start timer");
    43. }
    44.  
    45. void CHmiStateB::HideHmi(CHmi* hmi)
    46. {
    47. if( pTimer->isActive() )
    48. pTimer->stop();
    49. }
    50.  
    51. void CHmiStateB::ResetHmi(CHmi* hmi)
    52. {
    53. }
    54.  
    55.  
    56. void CHmiStateB::ValidateHmiItem(CHmi* hmi)
    57. {
    58. }
    59.  
    60. void CHmiStateB::CancelHmiItem(CHmi* hmi)
    61. {
    62. }
    63.  
    64. void CHmiStateB::ProcessTimerTimeout()
    65. {
    66. qDebug("Timer Timeout");
    67. }
    To copy to clipboard, switch view to plain text mode 

    So if someone could tell what is wrong ?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer problem ... it runs but never triggs

    it depends when do you call the 'ShowHmi' public function from outside, which is not attached

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QTimer problem ... it runs but never triggs

    Hmm, could you give some more informations please, I don't know what you mean

  4. #4
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QTimer problem ... it runs but never triggs

    Another thing I can say is that I use QCanvas, QCanvasViews and QCanvasItem ... does it change something ?

  5. #5
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QTimer problem ... it runs but never triggs

    Hi everybody,

    After a few days of investigations I finally found the origin of my QTimer object which never trigs.

    Before giving the solution of my problem, I must detailled a little bit the architecture of my application.

    It has a hmi object (using Desing Patern "STATE" ... it perhaps has another name but it is the only one I know so I hope it is the right one), a mp3 player object (using FMOD) and a QTimer object.

    My player object has just a signal called Finished used to notify the end of a mp3 file. I used to trigger this signal into the CALLBACK provided by the FMOD librairy ... this detail is the heart of the problem, in fact it is the problem.

    So let me tell you what was happening :
    My HMI let the user choose a MP3 file to open, this file is played then when it is finished the CALLBACK (the one provided by FMOD) is called in which I trigger my Finished signal. this signal is intercepted by my application and is connected to a slot. This slot do a few things and start the timer (which must triggs about 5 seconds later) then return to the line where the signal was triggered ... in the callback. Until this return my application seems to work but in my opinion is "lost" and it is the reason why the trigger signal was never intercepted.

    Here is what I did to fix my problem :
    * implement a boolean variable used to tell the player whether or not notify the end of play (Finished signal), this variable is set in the CALLBACK instead of directly triggering the signal
    * implement a timer in my player, in order to watch the boolean variable and triggering the signal depending on the variable state

    This solution works now but I still wonder what was happening in my case. Is the application lost in an undefined state ? If somebody could give me some more details about this undefined state ... I'm just curious.

    So the problem is over, I thank you alll for your help.

    ++

Similar Threads

  1. Qthread n QTimer Problem
    By quickNitin in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2006, 14:12
  2. Replies: 16
    Last Post: 7th March 2006, 15:57

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.