我是PyQt的新手,我无法解决这个问题。我正在尝试安装一个eventFilter,这样当按下并按住QPushButton时,系统会以一定的速率增加一个值(这可能是一个QTimer)。我在同一页面上有第二个QPushButton,当它被按下并保持时,应该降低该值。系统需要区分单击和按住。这是我到目前为止所做的,这不错,但我相信这是一种更有效的方法。
class App(QtGui.QMainWindow, App_Ui_MainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
QtGui.QMainWindow.__init__(self)
App_Ui_MainWindow.__init__(self)
self.setupUi(self)
#Set Up mouse and install event filter.
self.mouse_state = app.mouseButtons()
self.mouse_state == QtCore.Qt.LeftButton
self.increment_Button.installEventFilter(self)
self.decrement_Button.installEventFilter(self)
self.hoveringover = 'Null'
#Timer for when mouse is held down.
self.mouseheld = QtCore.QTimer()
self.mouseheld.timeout.connect(self.update)
self.mouseheld.start(500) #trigger every second.
self.mouseheld.timeout.connect(self.mouse_held_event)
#To figure out which/if we are hovering over a button.
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Enter:
self.hoveringover = obj.objectName()
elif event.type() == QtCore.QEvent.Leave:
self.hoveringover = 'Null'
return super(App, self).eventFilter(obj, event)
def mouse_held_event(self):
#Set up mouse.
self.is_mouse_held = 1
self.mouse_state = app.mouseButtons()
self.mouse_state == QtCore.Qt.LeftButton
if int(self.mouse_state) == 1:
if self.hoveringover == 'increment_Button':
self.increment()
elif self.hoveringover == 'decrement_Button':
self.decrement()