Qt长记事件

时间:2011-08-12 10:30:49

标签: qt events

我想知道 Qt 中是否有长按事件 我正在进行的模块目前需要长按。我已经看到在C7中长时间按下卸载

按钮点击事件和按钮按下事件会给出相同的结果吗?

alfah

3 个答案:

答案 0 :(得分:3)

你可以查看Qt的gesture framework
你想要的手势是QTapAndHoldGesture然后我想。

答案 1 :(得分:2)

您可以使用

keyPressEvent ( QKeyEvent * event )

keyReleaseEvent ( QKeyEvent * event )

for handle long_press_event

答案 2 :(得分:1)

快速简单老式方式,如果您不想使用该手势,那么就会发生类似这样的事情:

标题中声明一个毫秒时间戳以保留上次按下的时间。

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 *);

来源中定义鼠标按下和释放处理程序,如下所示:

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);
    }
}

注意:我没有编译这段代码,除了我脑子里的内置编译器,它有很多非传统的扩展。