I am creating a timer, so I can call a function periodically, I use the function connect to make the connection between the timer and the function. The code follows:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(DevicesScanner::scanUpdate()));
timer->start(1000);
My question is: How do I make this connection unique, so the timer is not created multiple times?
CodePudding user response:
You have a few options.
Make sure that your code is called once or at least uniquely.
Use singleShot.
#include #include
int main(int argc, char *argv[]) { QApplication app(argc, argv); Timer::singleShot(600000, &app, SLOT(quit())); return app.exec(); }
Use Qt::UniqueConnection.
This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set, QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6.
CodePudding user response:
QTimer should be defined in class member variable or you can define it in static variable area.The timer will be only create once.