Problems with using QTimer

2019-07-29 07:29发布

问题:

I am having a small problem with QTimer. Whenever I use QTimer it shows me this error

QTimer *timer = new QTimer();

error: invalid use of incomplete type 'struct QTimer'

So I tried this

QTimer timer();

Now I got rid of that error but when I use members inside the QTimer it shows me these errors. For example

timer.start(1000); 

or

timer->start(1000);

error: request for member 'start' in 'timer', which is of non-class type 'QTimer*()'

I tried to include QTimer but it shows me that there is no such file or directory error.

I am using the Code::Blocks IDE.

回答1:

Just add

#include <QTimer>

to the start of your source file. And go back to your first version:

QTimer *timer = new QTimer();


回答2:

There should be a QTimer header. If it's not found then you might have your include paths set up wrong. If it's not there, reinstall your Qt SDK.

Your code QTimer timer(); is wrong. It does not create a QTimer, but you declare a function 'timer', with return type 'QTimer'. It should be QTimer timer; although that would most likely give you similar issues if it can't find the header.



标签: qt timer