PDA

View Full Version : QTimer problem ... it runs but never triggs



yellowmat
29th June 2006, 10:00
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 :


#include <qobject.h>
#include "HmiStateA.h"
#include "HmiState.h"
#include "Hmi.h"
#include <qtimer.h>

class CHmiStateB : public QObject, public CAvreHmiStateA
{
Q_OBJECT

public:
static CHmiState* GetInstance();
virtual ~CHmiStateB();

protected:
CHmiStateB();
static CHmiStateB* m_Instance;

public:
virtual void ShowHmi(CHmi*);
virtual void HideHmi(CHmi*);
virtual void ResetHmi(CHmi*);
virtual void ValidateHmiItem(CHmi*);
virtual void CancelHmiItem(CHmi*);

protected slots:
void ProcessTimerTimeout();
protected:
QTimer* pTimer;
};

#endif


My implementation file class is :


#include "HmiStateB.h"
#include "HmiStateA.h"
#include <qapplication.h>

CHmiStateB* CHmiStateB::m_Instance = 0;

CHmiStateB::CHmiStateB():
QObject(),
CHmiStateA(),
pTimer(0)
{
if( !pTimer )
{
pTimer = new QTimer;
Q_ASSERT( pTimer );
connect(pTimer, SIGNAL(timeout()), this, SLOT(ProcessTimerTimeout()));
}
}

CHmiStateB::~CHmiStateB()
{
if( pTimer )
{
if( pTimer->isActive() )
pTimer->stop();
delete pTimer;
pTimer = 0;
}

}

CHmiState* CHmiStateB::GetInstance()
{
if( !m_Instance )
m_Instance = new CHmiStateB;

return m_Instance;
}

void CHmiStateB::ShowHmi(CHmi* hmi)
{
if( !pTimer->start(4000, TRUE) )
qDebug("Failed to start timer");
}

void CHmiStateB::HideHmi(CHmi* hmi)
{
if( pTimer->isActive() )
pTimer->stop();
}

void CHmiStateB::ResetHmi(CHmi* hmi)
{
}


void CHmiStateB::ValidateHmiItem(CHmi* hmi)
{
}

void CHmiStateB::CancelHmiItem(CHmi* hmi)
{
}

void CHmiStateB::ProcessTimerTimeout()
{
qDebug("Timer Timeout");
}


So if someone could tell what is wrong ?

Thanks in advance

ball
30th June 2006, 05:22
it depends when do you call the 'ShowHmi' public function from outside, which is not attached

yellowmat
30th June 2006, 07:55
Hmm, could you give some more informations please, I don't know what you mean :o

yellowmat
30th June 2006, 14:06
Another thing I can say is that I use QCanvas, QCanvasViews and QCanvasItem ... does it change something ?

yellowmat
4th July 2006, 12:54
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.

++