Reputation: 137
I wrote a header xx.h, there are two classes, one is a Qt class "QClass" and another is a generic class "Normal". And declare an external Normal variable. Normal has a QClass member.
Here is the content of xx.h :
#ifndef XX_H
#define XX_H
#include <QObject>
#include <QTimer>
class QClass : public QObject
{
Q_OBJECT
public:
QClass();
~QClass();
private:
QTimer *t;
private slots:
void func();
};
class Normal
{
int i;
QClass q;
};
extern Normal globalN;
#endif
I also wrote a xx.cpp to implement xx.h
#include "xx.h"
QClass::QClass() : t(new QTimer)
{
connect(t, SIGNAL(timeout()), this, SLOT(func()));
t->start(1000);
}
QClass::~QClass()
{
delete t;
}
void QClass::func()
{
static int n = 0;
++n;
}
And here is my main.cpp, here I define globalN. The class Why is my Qt window class, it is declared in why.h. This header is not important, so I didn't post its content.
#include "why.h"
#include "xx.h"
#include <QtWidgets/QApplication>
Normal globalN;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Why w;
w.show();
return a.exec();
}
My idea is that I put globalN in global scope, so when program starts, it constructs globalN, then the timer start to tick.
In order to check these stuff work right or not, I set a breakpoint in QClass::func()
, to check if QTimer calls func()
, but it doesn't.
I examine the code a long time, but I can't find where I mistake, please tell me. Thank you!!
Upvotes: 2
Views: 1048
Reputation: 997
For your timer to start, you need an event loop. To have an event loop you need to have an active QApplication
.
Here, since globalN
is global, it is created before your QApplication
, so your timer is not started.
Upvotes: 9