nhungr
20th July 2012, 11:11
Hello,
I have written two classes, one of which uses QTimer. My first class creates a QVector of my second class and in each instance of the second class, a QTimer is started in order to increment a counter periodically until it reaches a maximum. In other words, I have 5 timers running each in their respective class instance. I have no compile or execution errors but the timer's slot never seems to be called. I guess this means that the timeout signal of each qtimer is not called either... why? Are the QTimers out of scope before the timeout signal can be sent? I don't see why, as all instances of the classes remain active throughout... I have seen the comment on using QTimer in multiple threads here (http://doc.qt.nokia.com/4.7-snapshot/qtimer.html#details), except that I'm not using threads. Does anyone have any idea where I'm going wrong? Thanks very much in advance. My simplified code is as follows:
.h of my first class:
#include <QVector>
class MyFirstClass {
public:
void Initialize();
void Run();
QVector<QSharedPointer<MySecondClass> > mMySecondClass;
}
.cpp of my first class:
void MyFirstClass::Initialize() {
mMySecondClass.resize(5); // Create 5 instances of second class
for (i = 0; i<mMySecondClass.size(); ++i) { // Initialize all 5 instances
mMySecondClass[i] = QSharedPointer<MySecondClass>(new MySecondClass);
mMySecondClass[i]->Initialize();
}
}
void MyFirstClass::Run() {
for (i = 0; i<mMySecondClass.size(); ++i) { // Call Run for all 5 instances of second class
mMySecondClass[i]->Run();
}
}
.h of my second class:
#include <QTimer>
class MySecondClass: public QObject {
Q_OBJECT
public:
void Initialize();
void Run();
protected slots:
void TimerSlot(); // slot connected to timeout of QTimer
protected:
QTimer *mMyTimer;
int mCounter;
}
.cpp of my second class:
void MySecondClass::Initialize() {
mMyTimer= new QTimer();
}
void MySecondClass::Run() {
mCounter = 0; // initialize counter
connect(mMyTimer, SIGNAL(timeout()), this, SLOT(TimerSlot())); // connect timer signal to slot
mMyTimer->start(5); // start timer
}
void MySecondClass::TimerSlot() {
mCounter += 1; // increment counter
if (mCounter > 10) { // stop timer when counter reaches 10
mMyTimer->stop();
}
}
Example implementation:
MyFirstClass foo;
foo.Initialize();
foo.Run();
I have written two classes, one of which uses QTimer. My first class creates a QVector of my second class and in each instance of the second class, a QTimer is started in order to increment a counter periodically until it reaches a maximum. In other words, I have 5 timers running each in their respective class instance. I have no compile or execution errors but the timer's slot never seems to be called. I guess this means that the timeout signal of each qtimer is not called either... why? Are the QTimers out of scope before the timeout signal can be sent? I don't see why, as all instances of the classes remain active throughout... I have seen the comment on using QTimer in multiple threads here (http://doc.qt.nokia.com/4.7-snapshot/qtimer.html#details), except that I'm not using threads. Does anyone have any idea where I'm going wrong? Thanks very much in advance. My simplified code is as follows:
.h of my first class:
#include <QVector>
class MyFirstClass {
public:
void Initialize();
void Run();
QVector<QSharedPointer<MySecondClass> > mMySecondClass;
}
.cpp of my first class:
void MyFirstClass::Initialize() {
mMySecondClass.resize(5); // Create 5 instances of second class
for (i = 0; i<mMySecondClass.size(); ++i) { // Initialize all 5 instances
mMySecondClass[i] = QSharedPointer<MySecondClass>(new MySecondClass);
mMySecondClass[i]->Initialize();
}
}
void MyFirstClass::Run() {
for (i = 0; i<mMySecondClass.size(); ++i) { // Call Run for all 5 instances of second class
mMySecondClass[i]->Run();
}
}
.h of my second class:
#include <QTimer>
class MySecondClass: public QObject {
Q_OBJECT
public:
void Initialize();
void Run();
protected slots:
void TimerSlot(); // slot connected to timeout of QTimer
protected:
QTimer *mMyTimer;
int mCounter;
}
.cpp of my second class:
void MySecondClass::Initialize() {
mMyTimer= new QTimer();
}
void MySecondClass::Run() {
mCounter = 0; // initialize counter
connect(mMyTimer, SIGNAL(timeout()), this, SLOT(TimerSlot())); // connect timer signal to slot
mMyTimer->start(5); // start timer
}
void MySecondClass::TimerSlot() {
mCounter += 1; // increment counter
if (mCounter > 10) { // stop timer when counter reaches 10
mMyTimer->stop();
}
}
Example implementation:
MyFirstClass foo;
foo.Initialize();
foo.Run();