View Full Version : When is timeout signal fired (Qtimer)
aamir_azad
9th February 2013, 06:25
Is timeout() signal fired when I explicitly stop the timer? If yes, what should I do to prevent it from being called?
Thanks in advance.
ChrisW67
9th February 2013, 06:35
No, the timeout() signal is issued when the timer has expired and the program has returned to the Qt event loop. Calling stop() immediately kills the timer.
aamir_azad
9th February 2013, 06:52
Here is my code. I want that the msg box appears ONCE if the cursor stays inside the widget for two sec continuously.But when i move the cursor in (for 1sec) and then out, and again in for two sec, the message box appears for Random no of time..
what am i doing wrong??
Custom1.h
#ifndef CUSTOM1_H
#define CUSTOM1_H
#include<QPushButton>
#include<QTimer>
class Custom1 : public QPushButton
{
Q_OBJECT
public:
explicit Custom1(QWidget* parent = 0) : QPushButton(parent)
{
connect(this, SIGNAL(clicked()), this, SLOT(display_in()));
connect(this, SIGNAL(pressed()), this, SLOT(display_out()));
}
~Custom1() {}
protected:
void enterEvent(QEvent*);
void leaveEvent(QEvent*);
public slots:
void display_in();
void display_out();
void display();
};
#endif // CUSTOM1_H
Custom1.cpp
#include "custom1.h"
#include<QTimer>
#include<QMessageBox>
QTimer *my_timer = new QTimer();
void Custom1::enterEvent(QEvent*)
{
Q_EMIT clicked();
connect(my_timer, SIGNAL(timeout()), this, SLOT(display()));
my_timer->setSingleShot(true);
my_timer->start(2000);
}
void Custom1::leaveEvent(QEvent*)
{
if (my_timer->isActive())
{
my_timer->stop();
}
Q_EMIT pressed();
}
void Custom1::display_in()
{
setText("CURSOR IN");
}
void Custom1::display_out()
{
setText("CURSOR OUT");
}
void Custom1::display()
{
a++;
setText(QString::number(a));
QMessageBox msg;
msg.setText("hi"); // <--- THIS MESSAGE BOX APPEARS SOME RANDOM NO OF TIMES... WHY??
msg.exec();
}
wysota
9th February 2013, 09:01
Please provide a minimal compilable example reproducing the problem.
Lesiok
9th February 2013, 09:25
First of all timer signal is emmited AFTER 2 sec. If in this 2 sec Custom1::enterEvent(QEvent*) is called a few times timer signal is connected to display many times. Secondly, you never disconnect timer signal from display slot.
aamir_azad
9th February 2013, 09:49
Thanks Lesiok!!!
got the mistake!!!
Now its working :)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.