【问题标题】:qt detect mouse click duration while button pressedqt在按下按钮时检测鼠标点击持续时间
【发布时间】:2019-09-28 14:37:41
【问题描述】:

如果按钮按下超过 3 秒,我想关闭系统。 按下按钮时如何检测点击持续时间? 我可以使用pressed() 和released() 来做到这一点,但不是在发稿时。

【问题讨论】:

    标签: qt qt5


    【解决方案1】:

    你必须使用 QTimer:

    #include <QtWidgets>
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QPushButton button("Press me");
    
        QTimer timer;
        timer.setSingleShot(true);
        timer.setInterval(3 * 1000);
    
        QObject::connect(&button, &QPushButton::pressed, &timer, [&timer](){
            timer.start();
        });
        QObject::connect(&button, &QPushButton::released, &timer, [&timer](){
            timer.stop();
        });
        QObject::connect(&timer, &QTimer::timeout, [](){
            qDebug() << "shutdown";
        });
    
        button.show();
    
        return a.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多