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>
{
Q_OBJECT
public:
{
connect(this, SIGNAL(clicked()), this, SLOT(display_in()));
connect(this, SIGNAL(pressed()), this, SLOT(display_out()));
}
~Custom1() {}
protected:
public slots:
void display_in();
void display_out();
void display();
};
#endif // 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
To copy to clipboard, switch view to plain text mode
Custom1.cpp
#include "custom1.h"
#include<QTimer>
#include<QMessageBox>
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++;
msg.setText("hi"); // <--- THIS MESSAGE BOX APPEARS SOME RANDOM NO OF TIMES... WHY??
msg.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks