View Full Version : Problem of QTimer with repeat statments

13th October 2009, 18:46
Dear all QT Expert

I make a program of using the QTimer class and I create a QTimer in the Constructor as follows and every time the timer start it show up 2 repeat statment as follows :

system test 1
system test 1

system test 2
system test 2

It seems run 2 times every trigger with same slot. Is it some problems of my code or any statements missing to make this happen. Please help !!

My code

QTimer *sendtimer = new QTimer(this);
connect(sendtimer, SIGNAL(timeout()), this, SLOT(testprog()));

void ConfigurationPage::testprog()

qDebug() << "system test " << loopcount++;



testdata = QString("system test %1").arg(loopcount);

qDebug() << testdata <<"\n";

13th October 2009, 20:50
your code is not quite clear (it is a collection of fragments);

but it looks like you have 2 places that print such a line: one in the slot connected to timeout, another when printing the string testdata. So I would not be that surprised about getting 2 such lines...
show us the real code, if that doesn't solve it.

14th October 2009, 04:50
thanks for your reply

SInce I found that the problem due to I refer to this class in othe class by declaration

in other class query

Configuration cpage;


which will call the Configuration class constructor again which make the repetition

Please advise how can I use other class variable without trigger the constructor

sorry for I am newbie of the C++ program to ask those questions


14th October 2009, 08:57
you can try storing pointers instead.

QList<X*> aList;
aList << new X(...);
where X would be the class type you want to store.

If you do that, you have to make sure that the heap-allocated objects will be deletetd (with delete) eventually.
Alternatively, you can use smart pointers, like QSharedPointer