PyQt5:QPushButton 双击? [英] PyQt5: QPushButton double click?
本文介绍了PyQt5:QPushButton 双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到一个好的答案:有没有办法双击执行某个功能,然后单击另一个功能??例如:
I can't find a good answer for this: is there a way for double click to execute a certain function, and single click one other function?? For example:
def func1(self):
print('First function')
def func2(self):
print('Second function')
self.ui.button.clicked.connect(self.func1)
self.ui.button.doubleClicked.connect(self.func2)
我看到 QTreeview
可以双击,但 QPushButton
不行.谢谢!
I've seen double clicking is possible for the QTreeview
but not a QPushButton
. Thanks!
推荐答案
您可以通过扩展 QPushButton
类轻松自己添加功能:
You can add the functionality easily yourself by extending QPushButton
class:
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class QDoublePushButton(QPushButton):
doubleClicked = pyqtSignal()
clicked = pyqtSignal()
def __init__(self, *args, **kwargs):
QPushButton.__init__(self, *args, **kwargs)
self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.clicked.emit)
super().clicked.connect(self.checkDoubleClick)
@pyqtSlot()
def checkDoubleClick(self):
if self.timer.isActive():
self.doubleClicked.emit()
self.timer.stop()
else:
self.timer.start(250)
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.button = QDoublePushButton("Test", self)
self.button.clicked.connect(self.on_click)
self.button.doubleClicked.connect(self.on_doubleclick)
self.layout = QHBoxLayout()
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.resize(120, 50)
self.show()
@pyqtSlot()
def on_click(self):
print("Click")
@pyqtSlot()
def on_doubleclick(self):
print("Doubleclick")
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
但是,我不会推荐它.用户不希望双击按钮.您可以参考命令按钮 Microsoft 指南.
However, I would not recommend it. Users do not expect to double-click buttons. You can refer to Command Buttons Microsoft guidelines.
这篇关于PyQt5:QPushButton 双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文