【问题标题】:Assign specific `QPushButton` to specific hardware button将特定的“QPushButton”分配给特定的硬件按钮
【发布时间】:2019-04-24 22:32:13
【问题描述】:

我想使用 Python 和 PyQt5 为 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()

主窗口.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>

【问题讨论】:

  • 我不明白你,你能解释一下吗
  • 期望的行为是QPushButton 在点击按钮时不应该显示点击动画。
  • @user9402680 鼠标左键单击动画仍然是每个QPushButton 的一部分。
  • @Atalanttore 您指出的内容可能令人困惑,您不希望按钮的外观被按下,或者您不想显示从未按下按钮到按下按钮的过渡
  • @user9402680 太好了,您的代码实现了所需的行为。

标签: python pyqt pyqt5


【解决方案1】:

请试试这个代码。

说明

QPushButton 在默认设置下由LeftButton 进行动画处理。 所以我们需要考虑如何通过MidButtonRightButton对其进行动画处理。

动画的道理类似于setDown(True)setDown(False)的组合。所以我在mousePressEventmouseReleaseEvent之间点击MidButtonRightButton时实现它们。

但是还有一个问题需要解决。因为QPushButtonLeftButton动画的,所以中间按钮和右键仍然是LeftButton动画。

你想名字一致。也就是说,LeftButton只通过leftbutton发出动画。(MidButton只在midbutton上发出动画,RightButton只在rightbutton上发出动画。)

为此,有一种方法可以在 eventFilter 中实现 return True。 这意味着eventFilter 服务于eventFilter 的事件和小部件本身的事件。

因此,您无需为 pushButton 实现子类并覆盖它。

当你dblclick按钮时,动画也会发生。所以我调节了它。

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 在单击鼠标左键时都会显示动画。
  • @Atalanttore 我明白了,我想念它。好的。
猜你喜欢
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2017-12-11
相关资源
最近更新 更多