首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将特定的“`QPushButton`”分配给特定的硬件按钮

将特定的“`QPushButton`”分配给特定的硬件按钮
EN

Stack Overflow用户
提问于 2019-04-21 14:11:31
回答 1查看 346关注 0票数 1

我希望使用PythonandPyQt5为QPushButton分配一个特定的硬件密钥。

下面的代码使用事件筛选器来筛选每个QPushButton的鼠标按钮事件。不幸的是,点击动画并不是这个任务的一部分,只会出现在每个QPushButton上的鼠标左键点击。

是否有办法实现所期望的行为?

main.py

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-21 14:30:23

请试试这个密码。

解释

QPushButton在默认设置下由LeftButton动画。所以我们需要考虑如何用MidButtonRightButton来制作动画。

动画的真实性与setDown(True)setDown(False).So的组合相似或相同--当您在mousePressEventmouseReleaseEvent之间单击by MidButtonRightButton时,就实现了它们。

但仍有一个问题有待解决。因为QPushButton是由LeftButton动画的,所以中间按钮和右边按钮仍然由LeftButton动画。

您想要重合每个other.That的名称--也就是说,LeftButton只通过左键发出动画。( midbutton只在中间按钮上发布动画,RightButton只在右边按钮上发布动画。)

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

因此,您不需要为pushButton实现子类并重写它。

当你点击按钮时,动画也是happens.So i调节的。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55783485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档