简体   繁体   English

MousePressEvent 在 QPushbutton 上不起作用

[英]MousePressEvent not working on QPushbutton

so in my qtgui i want to know whenever someone is clicking (on any QObject or on the window), Thus i'm using the MousePressEvent.所以在我的 qtgui 中,我想知道何时有人点击(在任何 QObject 或窗口上),因此我使用的是 MousePressEvent。 However when i'm clicking on QPushbuttons the event is not recognized但是,当我单击 QPushbuttons 时,无法识别该事件

There is a simplier version of my code我的代码有一个更简单的版本

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import os
from PyQt5.QtWidgets import QMainWindow

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super(Ui_MainWindow,self).__init__()

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(1920, 1080)
        self.setStyleSheet("\n"
"")
        self.centralwidget = QtWidgets.QWidget(self)
        #self.centralwidget.setStyleSheet("#centralwidget{background-image: url(:/images/assets/back1.jpg);}")
        
        self.centralwidget.setObjectName("centralwidget")
        self.timer = 5000
        
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")

        self.but1 = QtWidgets.QPushButton(self.centralwidget)
        self.but1.setGeometry(QtCore.QRect(1500, 500, 301, 51))
        self.but1.setMouseTracking(True)
        self.but1.setStyleSheet("QPushButton{\n"
                "border-style: outset;""\n"
                " border-radius: 10px;"
                "border-width: 2px;"
                "border-color: gray;"
                "font: 18pt \"Adobe Pi Std\";\n"
                "color: rgb(255, 255, 255);}\n"
                "QPushButton:pressed { border-style : inset; border-color:black}")
        self.but1.setObjectName("but1")
        self.but1.clicked.connect(lambda : self.addtimer())
        self.setCentralWidget(self.centralwidget)
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)


  def mousePressEvent(self,event):
        #super(Ui_MainWindow,self).mousePressEvent(event) i tried this but change nothing
        print("Clicked")

  def addtimer(self):
       self.timer+=5000
       print(self.timer)

def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.but1.setText(_translate("MainWindow", "Explore!"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    
    ui = Ui_MainWindow()
    ui.setupUi()
    ui.show()
    sys.exit(app.exec_())


i highly believe this problem is caused by bad inhertiance, but i can't figure out how to fix it.我高度相信这个问题是由不良继承引起的,但我不知道如何解决它。

If you wonder why i want to know whenever someone click, well the main purpose is to set a Qtimer that will close the window if no click occurs after x time如果您想知道为什么我想知道任何时候有人点击,那么主要目的是设置一个 Qtimer,如果在 x 时间后没有点击,它将关闭 window

Thanks in advance提前致谢

what you need to do is to determinate what kind of mouse event do u want to check then go to pyqt5 documentation and search what event does that (that's what i did) after that is just code the logic to detect that event.你需要做的是确定你想要检查什么样的鼠标事件然后 go 到 pyqt5 文档并搜索什么事件(这就是我所做的)之后只是编码检测该事件的逻辑。

#Below your other self.but1... u add this one to call the function each time u click
self.but1.clicked.connect(lambda: self.mousePressEvent(QMouseEvent.MouseButtonPress))

def mousePressEvent(self, event):
    #Here you check if the event u receive its the one you want to track
    if event == QMouseEvent.MouseButtonPress:
        print("Clicked")

Hope this solution helps you.希望此解决方案对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM