PDA

View Full Version : Problem with QObject::timerEvent()



ericwny
4th June 2009, 19:09
Using Qt 4.5.0 Open Source under Windwos Vista 64 - I am having a problem with timerEvent().

I start timers in my QObject derived class:

timer_ID_t item_timer_id = startTimer( FadeTimerInterval );
if ( item_timer_id != 0 )
{
acivity_timer_map_[ item_timer_id ] = item;
}

timer_ID_t is a typedef of int
FadeTimerInterval is a static const int whose value is 50
acivity_timer_map_ is a std::map<>

This is done twice and I get back timer IDs of 5 and 6.
But in the timerEvent( QTimerEvent* ev ) that
gets called ev->timerId() returns 4 every time.

Is this a deprecated timer event method? Does Qt coalesce timers if the same object starts multiple timers?

ericwny
4th June 2009, 19:54
Just in case it turns out I can't use timer events in this way let me describe what I am trying to do and ask some related questions.

I am making a networking app. One of the features is a list of entities out on the network that are transmitting to this machine. I am using a QTreeView to implement a multi-column list view. In one column I wanted to have pixmaps of LEDs that will light up when network activity happens.

I have implemented the custom drawing of items in the LED column using the StarDelegate example as a guide. That works fine. I am now trying to implement a way to have a fade timer.

Network activity causes things to happen that end up in a pixmap representing a lit LED to be displayed but I want to be able to fade it to off over a time interval.

I want to be as efficient with this as possible. As a side issue I wondered if there was some info on how the paint system works from Qt's point of view. For example: in windows I would probably be invalidating the rectangles of the LED pixmaps that were affected but I can't find any info analogous to this in Qt. So I am just going to rely on dataChanged signals from the affected items.

Because I want the fade to happen gradually over a time period I went with a timer event for each entity in the list. I would have used a single timer event and update everyone but I can't find anything analogous to a tick count - So I could store a tick count in each item when the light goes on and on each timer event see how long the current tick count is from the count when the on state occurred in order to scale the brightness. Querying the Date/Time seems a little inefficient for what I want to do. I may be wrong.

Well, if it turns out that I need a separate QTimer for each LED fade - I notice that in the posts about QTimer I always seem code fragments like the following:

MyObject::MyObject()
{
QTimer* my_timer = new QTimer( this );

// connect signals/slots, set intervals, start...
}

This is a memory leak right? Is it just this way for brevity? Or is there some memory management Qt does to reclaim the memory when the timer is killed? I see this form again and again in code people are posting for questions on their own projects.

Thanks in advance.

ericwny
4th June 2009, 20:15
OK found my problem. The timer start was done in the thread that monitors the network, not the GUI thread. QObject::startTimer should have returned 0 so I knew the was a problem. I only found out by tracing into the code.

wysota
4th June 2009, 21:40
Did you have the console support enabled? Qt should have warned you there that you were doing something you were not supposed to.

ericwny
4th June 2009, 22:08
Good question. I guess that would be how I would get to see all the debug messages I see in the source code but didn't know how to have Qt display.:rolleyes:

wysota
4th June 2009, 22:11
Just add "CONFIG+=console" to your qmake project and rerun qmake.

ericwny
4th June 2009, 22:20
Ah that's it - I'm using MSVC 2005.

wysota
4th June 2009, 22:22
You can still add it to the project and regenerate the VS project using qmake. Or you can enable the console subsystem directly in VS.

ericwny
4th June 2009, 22:30
I just remembered about enabling the console subsystem. Had stopped running my app that way for aesthetic reasons.

wysota
5th June 2009, 00:31
This is useful only for debugging. When you release it to your users, disable the console.