[Qt-interest] Problem with fast QTimer and Qt::DirectConnection

Till Priemer till.priemer at kerdos.de
Mon Mar 8 08:36:53 CET 2010


Hello,

I wrote a small class TimerTest with two QTimer-Objects with very small
intervals (1 mSec and a slower one with 100 mSec) connected to the same slot
(see source code).
If I use Qt::DirectConnection I get a kind of a strange behavior: Sometimes
the fast timer 'stops' sending events for some time (some seconds or even
longer) and the slot gets signals from the slow timer only.
I checked this on two different machines with Windows XP (visual studio 05)
and WindowsVista (Qt Creator), both using Qt4.6.2 .

In the Qt documentation I found that some timer events are discarded by Qt
if the precision can't be handled by the operation system. But is this the
explanation for this strange behavior?

In a more complex program I saw the same behavior with bigger interval for
the fast timer (5ms or 10ms).


Thank you in advance!

Regards
Till Priemer


class TimerTest : public QObject
{
    Q_OBJECT
public:
    TimerTest();
    ~TimerTest(){}

public slots:
    void logTimer();
signals:
    void shot(const QString&);
private:
    QTimer timer1;
    QTimer timer2;

    int count1;
    int count2;
    int watchdog;
};

TimerTest::TimerTest()
 : count1(0), count2(0), watchdog(0)
{
    connect( &timer1, SIGNAL( timeout() ), this, SLOT( logTimer() ),
Qt::DirectConnection);
    connect( &timer2, SIGNAL( timeout() ), this, SLOT( logTimer() ),
Qt::DirectConnection);

    timer1.start(1);
    timer2.start(100);
}


void TimerTest::logTimer()
{
    if (sender() == &timer1 ) {
        count1++;
        watchdog = 0;
    }
    else if (sender() == &timer2) {
        count2++;
        watchdog++;

        if(watchdog > 5){
            QString st = QString("Watchdog>5! %1 %2
%3").arg(count1).arg(count2).arg(watchdog);
            //qFatal(st.toAscii().data());
            qWarning(st.toAscii().data());

        }
    }
}




More information about the Qt-interest-old mailing list