Python 按住鼠标按钮10秒后连接信号
我想在点击并按住按钮一定时间后发送“连接”信号。在该时间过后(如10秒),即使按钮未松开,也会触发动作或事件。它可以是单击或按住一段时间或任何不同的组合,然后单击并释放按钮。试试:Python 按住鼠标按钮10秒后连接信号,python,pyqt,Python,Pyqt,我想在点击并按住按钮一定时间后发送“连接”信号。在该时间过后(如10秒),即使按钮未松开,也会触发动作或事件。它可以是单击或按住一段时间或任何不同的组合,然后单击并释放按钮。试试: import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * class MainWindow(QWidget): def __init__(self): super().__init__()
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.textLabel = QLabel()
button = QPushButton("Click me")
button.clicked.connect(self.clickedButton)
grid = QGridLayout(self)
grid.addWidget(self.textLabel)
grid.addWidget(button)
def clickedButton(self):
QTimer.singleShot(3000, self.passed3seconds) # <---
def passed3seconds(self):
self.textLabel.setText("3 seconds passed \n do something")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
导入系统
从PyQt5.QtCore导入*
从PyQt5.QtWidgets导入*
类主窗口(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.textLabel=QLabel()
button=QPushButton(“单击我”)
按钮。单击。连接(self.clicked按钮)
grid=QGridLayout(自)
grid.addWidget(self.textLabel)
grid.addWidget(按钮)
def Clicked按钮(自身):
QTimer.singleShot(3000,self.passed3秒)#试试:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.textLabel = QLabel()
button = QPushButton("Click me")
button.clicked.connect(self.clickedButton)
grid = QGridLayout(self)
grid.addWidget(self.textLabel)
grid.addWidget(button)
def clickedButton(self):
QTimer.singleShot(3000, self.passed3seconds) # <---
def passed3seconds(self):
self.textLabel.setText("3 seconds passed \n do something")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
导入系统
从PyQt5.QtCore导入*
从PyQt5.QtWidgets导入*
类主窗口(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.textLabel=QLabel()
button=QPushButton(“单击我”)
按钮。单击。连接(self.clicked按钮)
grid=QGridLayout(自)
grid.addWidget(self.textLabel)
grid.addWidget(按钮)
def Clicked按钮(自身):
QTimer.singleShot(3000,self.passed3seconds)#我认为您可以依赖的用法,即当用户单击按钮时,您可以以指定的间隔启动一些QTimer实例,当QTimer超时时,然后触发一个信号。若用户中止该操作,QTimer可以被重置,然后信号将不会被触发。下面可以看到示例代码。但是,它是C++的,但对你来说可能不是问题。 下面是主窗口的定义:
#pragma once
#include <memory>
#include <QMainWindow>
namespace Ui
{
class MainWindow;
}
class MainWindow
: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
signals:
void SomeTimerSignal(); // Signal we are eager to fire on the specific event.
public slots:
void ButtonPressed(); // Button is clicked down.
void ButtonReleased(); // Button is released.
void OnTimeout(); // Timeout handling slot.
private:
std::unique_ptr<Ui::MainWindow> m_ui; // UI mockup that is provided by Qt for us under the hood.
QTimer* m_buttonTimer; // The timer we are going to control.
};
#pragma一次
#包括
#包括
名称空间用户界面
{
类主窗口;
}
类主窗口
:public qmain窗口
{
Q_对象
公众:
主窗口(QWidget*parent=nullptr);
虚拟主窗口();
信号:
void SomeTimerSignal();//表示我们急于触发特定事件。
公众时段:
void ButtonPressed();//按钮被向下单击。
void Button释放();//按钮被释放。
void OnTimeout();//超时处理插槽。
私人:
std::unique_ptr m_ui;//Qt在引擎盖下为我们提供的ui模型。
QTimer*m_buttonimer;//我们要控制的计时器。
};
现在代码本身实现了这个定义:
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_ui(new Ui::MainWindow())
{
m_ui->setupUi(this);
// We could have chosen to instantiate timers on each signal, but it is much more efficient
// to create an instance of it once, and then use it appropriately.
m_buttonTimer = new QTimer(this);
// OnTimeout will get triggered after 10 seconds.
m_buttonTimer->setInterval(10000);
connect(m_buttonTimer, &QTimer::timeout, this, &MainWindow::OnTimeout);
auto layout = new QVBoxLayout();
auto button = new QPushButton();
button->setText("Click and hold me!");
button->setFixedSize(150, 50);
layout->addWidget(button);
m_ui->centralWidget->setLayout(layout);
// Listen to button being pressed down: https://doc.qt.io/qt-5/qabstractbutton.html#pressed
connect(button, &QPushButton::pressed, this, &MainWindow::ButtonPressed);
// Listen to button being released: https://doc.qt.io/qt-5/qabstractbutton.html#released
connect(button, &QPushButton::released, this, &MainWindow::ButtonReleased);
}
MainWindow::~MainWindow()
{
// Button timer will be taken care by Qt through its' memory model.
}
void MainWindow::ButtonPressed()
{
// Start the timer when button is pressed.
m_buttonTimer->start();
}
void MainWindow::ButtonReleased()
{
// Stop the timer, but don't delete it, since it can be reused.
m_buttonTimer->stop();
}
void MainWindow::OnTimeout()
{
// On timeout, we stop the timer, so it would not be triggered all over again when not needed.
m_buttonTimer->stop();
// And we fire some wanted signal.
emit SomeTimerSignal();
}
#包括
#包括
#包括
#包括“MainWindow.h”
#包括“ui_main window.h”
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
,m_ui(新ui::MainWindow())
{
m_ui->setupUi(本);
//我们本可以选择在每个信号上实例化计时器,但这样更有效
//创建一次实例,然后适当地使用它。
m_buttonTimer=新的QTimer(本);
//OnTimeout将在10秒后触发。
m_按钮定时器->设置间隔(10000);
连接(m_buttonTimer和QTimer::timeout、this和MainWindow::OnTimeout);
自动布局=新的QVBoxLayout();
自动按钮=新的QPushButton();
按钮->设置文本(“点击并按住我!”);
按钮->设置固定尺寸(150,50);
布局->添加小部件(按钮);
m_ui->centralWidget->setLayout(布局);
//收听正在按下的按钮:https://doc.qt.io/qt-5/qabstractbutton.html#pressed
连接(按钮,&QPushButton::按下,此,&MainWindow::按钮按下);
//收听正在释放的按钮:https://doc.qt.io/qt-5/qabstractbutton.html#released
连接(按钮,&QPushButton::已发布,此,&MainWindow::按钮已发布);
}
MainWindow::~MainWindow()
{
//按钮计时器将由Qt通过其“内存模型”进行管理。
}
void主窗口::按钮按下()
{
//按下按钮后启动计时器。
m_ButtonTime->start();
}
void主窗口::按钮已释放()
{
//停止计时器,但不要删除它,因为它可以重复使用。
m_按钮定时器->停止();
}
void主窗口::OnTimeout()
{
//在超时时,我们停止计时器,这样它就不会在不需要时再次触发。
m_按钮定时器->停止();
//我们发射了一些通缉信号。
发出SomeTimerSignal();
}
为了使用此代码,您可能希望以以下方式实例化应用程序:
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#包括“MainWindow.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();
}
<> P>,在使用QT创建者时,在创建第一个QT项目时,在C++中是非常基本的QT应用程序。希望这段代码澄清了您如何实现所需的想法。如果你还有什么问题,请提出来。希望我能够帮助您。我认为您可以依赖的用法,即当用户单击按钮时,您可以以指定的间隔启动一些QTimer实例,当QTimer超时时,然后触发一个信号。若用户中止该操作,QTimer可以被重置,然后信号将不会被触发。下面可以看到示例代码。但是,它是C++的,但对你来说可能不是问题。 下面是主窗口的定义:
#pragma once
#include <memory>
#include <QMainWindow>
namespace Ui
{
class MainWindow;
}
class MainWindow
: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
signals:
void SomeTimerSignal(); // Signal we are eager to fire on the specific event.
public slots:
void ButtonPressed(); // Button is clicked down.
void ButtonReleased(); // Button is released.
void OnTimeout(); // Timeout handling slot.
private:
std::unique_ptr<Ui::MainWindow> m_ui; // UI mockup that is provided by Qt for us under the hood.
QTimer* m_buttonTimer; // The timer we are going to control.
};
#pragma一次
#包括
#包括
名称空间用户界面
{
类主窗口;
}
类主窗口
:public qmain窗口
{
Q_对象
公众:
主窗口(QWidget*parent=nullptr);
虚拟主窗口();
信号:
void SomeTimerSignal();//表示我们急于触发特定事件。
公众时段:
void ButtonPressed();//按钮被向下单击。
void Button释放();//按钮被释放。
void OnTimeout();//超时处理插槽。
私人:
std::unique_ptr m_ui;//Qt在引擎盖下为我们提供的ui模型。
QTimer*m_buttonimer;//我们要控制的计时器。
};
现在代码本身实现了这个定义