Long Press Event in Qt
I just want to know if there is a long press event available in Qt
The module im working on currently requires long press. I have seen long press in C7 for uninstalling.The but开发者_开发知识库ton click event and the button press event gives the same result right?
alfah
You can have a look at Qt's gesture framework,
The gesture you are after would be QTapAndHoldGesture then I think.
You can use
keyPressEvent ( QKeyEvent * event )
and
keyReleaseEvent ( QKeyEvent * event )
for handle long_press_event
The quick, easy and old fashioned way, if you don't want to use that gesture stuff goes a little something like this:
In header declare a millisecond time stamp to hold the time of last press.
private:
// Remembers the point in time when mouse button went down
quint64 mLastPressTime=0;
// Pressing and holding for one full second constitutes a "longpress", set whatever value in milliseconds you want here.
static const quint64 MY_LONG_PRESS_THRESHOLD=1000;
protected:
// Declare that we are overriding QWidget's events for mouse press and release
void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE;
signals:
// Our custom signal to emit once a longpress is detected.
void longPressEvent(QMouseEvent *);
In source define the mouse press and release handlers like so:
void MyClass::mousePressEvent(QMouseEvent *event)
{
// Remeber last time mousr was pressed
mLastPressTime=QDateTime::currentMSecsSinceEpoch();
}
void MyClass::mouseReleaseEvent(QMouseEvent *event)
{
// Calculate for how long the button has been pressed upon release
const quint64 pressTime = QDateTime::currentMSecsSinceEpoch() - mLastPressTime;
// The press time exceeds our "threshold" and this constitutes a longpress
if( pressTime > MY_LONG_PRESS_THRESHOLD){
// We pass the original mouse event in case it is useful (it contains all sorts of goodies like mouse posittion, which button was pressed etc).
emit longPressEvent(event);
}
}
NOTE: I did not compile this code, except in the built in compiler in my head which has a lot of non-traditional extensions.
精彩评论