Because I have too much time, or better to much work to get around, here is an example:
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QtGui>
{
Q_OBJECT
public:
void setMinimumHoldTime(int msec)
{
m_minimumHoldTime = msec;
}
int minimumHoldTime() const
{
return m_minimumHoldTime;
}
protected:
signals:
void holdForMSec(int);
private:
int m_minimumHoldTime;
};
#endif // MYBUTTON_H
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QtGui>
class myButton : public QPushButton
{
Q_OBJECT
public:
myButton(QWidget *parent = 0);
void setMinimumHoldTime(int msec)
{
m_minimumHoldTime = msec;
}
int minimumHoldTime() const
{
return m_minimumHoldTime;
}
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
signals:
void holdForMSec(int);
private:
QTime m_time;
int m_minimumHoldTime;
};
#endif // MYBUTTON_H
To copy to clipboard, switch view to plain text mode
#include "mybutton.h"
myButton
::myButton(QWidget *parent
) : QPushButton(parent
), m_time
(), m_minimumHoldTime
(4000) {}
{
m_time
= QTime::currentTime();
}
{
int msec = m_time.elapsed();
// Do time check in the button class
if (m_minimumHoldTime < msec && rect().contains(event->pos()))
emit holdForMSec(msec);
// or let the reciver decide what to do
// if (rect().contains(event->pos()))
// emit holdForMSec(msec);
}
#include "mybutton.h"
myButton::myButton(QWidget *parent)
: QPushButton(parent), m_time(), m_minimumHoldTime(4000)
{}
void myButton::mousePressEvent(QMouseEvent *event)
{
m_time = QTime::currentTime();
QPushButton::mousePressEvent(event);
}
void myButton::mouseReleaseEvent(QMouseEvent *event)
{
int msec = m_time.elapsed();
// Do time check in the button class
if (m_minimumHoldTime < msec && rect().contains(event->pos()))
emit holdForMSec(msec);
// or let the reciver decide what to do
// if (rect().contains(event->pos()))
// emit holdForMSec(msec);
QPushButton::mouseReleaseEvent(event);
}
To copy to clipboard, switch view to plain text mode
@talk2amulya: If you call QTimer::start(int) you start a new "event loop" (I don't know how QTimer is exactly implemented), to measure when the time is up and at which end a signal is emitted. And this continuous! Whereas QTime simple returns a time Object. Nothing more...
Bookmarks