Python 将特定的“QPushButton”分配给特定的硬件按钮
我想使用Python和PyQt5为Python 将特定的“QPushButton”分配给特定的硬件按钮,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我想使用Python和PyQt5为QPushButton分配一个特定的硬件键 以下代码使用事件筛选器为每个QPushButton筛选鼠标按钮事件。不幸的是,单击动画不是此任务的一部分,仅在每次QPushButton上单击鼠标左键时显示 有没有一种方法可以实现期望的行为 main.py import sys from PyQt5.QtCore import QEvent, QObject, Qt from PyQt5.QtWidgets import QApplication, QMainWind
QPushButton
分配一个特定的硬件键
以下代码使用事件筛选器为每个QPushButton
筛选鼠标按钮事件。不幸的是,单击动画不是此任务的一部分,仅在每次QPushButton
上单击鼠标左键时显示
有没有一种方法可以实现期望的行为
main.py
import sys
from PyQt5.QtCore import QEvent, QObject, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
loadUi("mainwindow.ui", self)
self.pushButton_left.installEventFilter(self)
self.pushButton_middle.installEventFilter(self)
self.pushButton_right.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress:
if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_left":
print("Left click")
elif event.button() == Qt.MiddleButton and obj.objectName() == "pushButton_middle":
print("Middle click")
elif event.button() == Qt.RightButton and obj.objectName() == "pushButton_right":
print("Right click")
return QObject.event(obj, event)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1000</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="locale">
<locale language="English" country="UnitedKingdom"/>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton_left">
<property name="text">
<string>For left mouse button</string>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pushButton_middle">
<property name="text">
<string>For middle mouse button</string>
</property>
</widget>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<widget class="QPushButton" name="pushButton_right">
<property name="text">
<string>For right mouse button</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1000</width>
<height>28</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
主窗口
0
0
1000
200
主窗口
用于鼠标左键
Qt::水平
40
20
用于鼠标中键
Qt::水平
40
20
用于鼠标右键
0
0
1000
28
请尝试此代码
解释
QPushButton
在默认设置下由LeftButton
设置动画。
因此,我们需要考虑如何通过MidButton
和RightButton
设置动画
动画的真实性与setDown(True)
和setDown(False)
的组合相似或相同。因此,当您在mousePressEvent
和mouseReleaseEvent
之间单击MidButton
和RightButton
时,我实现了它们
但仍然存在一个问题。因为QPushButton
是通过LeftButton
设置动画的,所以中间按钮和右侧按钮仍然是通过LeftButton
设置动画的
您希望名称相互重合。也就是说,LeftButton仅通过LeftButton发射动画。(MidButton仅在MidButton上发射动画,RightButton仅在RightButton上发射动画。)
为此,有一种方法可以在eventFilter
中实现return True。
这意味着eventFilter
为eventFilter
的事件和小部件本身的事件提供服务
因此,您不需要为按钮实现子类并覆盖它
当你点击按钮时,动画也会发生。所以我调整了它
import sys
from PyQt5.QtCore import QEvent, QObject, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
loadUi("mainwindow.ui", self)
self.pushButton_left.installEventFilter(self)
self.pushButton_middle.installEventFilter(self)
self.pushButton_right.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonDblClick:
if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":
return True
elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":
return True
if event.type() == QEvent.MouseButtonPress:
if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_left":
print("Left click")
elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":
obj.setDown(False)
return True
elif event.button() == Qt.MiddleButton and obj.objectName() == "pushButton_middle":
obj.setDown(True)
print("Middle click")
elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":
obj.setDown(False)
return True
elif event.button() == Qt.RightButton and obj.objectName() == "pushButton_right":
obj.setDown(True)
print("Right click")
elif event.type() == QEvent.MouseButtonRelease:
if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_left":
print("Left click")
elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":
return True
elif event.button() == Qt.MiddleButton and obj.objectName() == "pushButton_middle":
obj.setDown(False)
print("Middle click")
elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":
return True
elif event.button() == Qt.RightButton and obj.objectName() == "pushButton_right":
obj.setDown(False)
print("Right click")
return QObject.event(obj, event)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
这是一个很好的开始,但是当用鼠标左键单击时,所有的
QPushButton
都会显示一个动画。@Atlanttore我明白了,我错过了。好的,我不明白你的意思,您能更好地解释一下吗理想的行为是当点击按钮时,QPushButton
应显示无点击动画。@user9402680鼠标左键点击的点击动画仍然是每个QPushButton
@Atlantore的一部分,您指出的可能会令人困惑,您不希望显示按下按钮的外观,或者不希望显示从未按下按钮到按下按钮的转换button@user9402680很好,您的代码实现了所需的行为。