简体   繁体   English

子窗口无法触发PyQt5中的鼠标事件

[英]Child window failed to trigger mouse event in PyQt5

I want to capture mouse events for the child window,but the problem is Mouse events are not passed to this child window.... After finding information on the Internet, I found that I need to customize the button.But I still did not get any respond after doing this. 我想捕获子窗口的鼠标事件,但是问题是鼠标事件没有传递到该子窗口。...在Internet上找到信息之后,我发现我需要自定义按钮。但是我仍然没有这样做后得到任何回应。 Can anyone tell me what else I need to do to capture mouse events inside this Child window? 谁能告诉我在该“子”窗口中捕获鼠标事件还需要做什么? Here is my code: 这是我的代码:

from PyQt5 import QtCore,QtWidgets
from PyQt5.QtWidgets import *
import sys

class My_top_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 532)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.Products = MyButton()
        self.verticalLayout.addWidget(self.Products)
        self.Products.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.Products.setText(_translate("Dialog", "top window"))

    def accept(self):
        self.dialog = QDialog()
        ui = My_second_Dialog()
        ui.setupUi(self.dialog)
        self.dialog.show()

class My_second_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 480)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.fetch = MyButton()
        self.verticalLayout.addWidget(self.fetch)
        self.fetch.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.fetch.setText(_translate("Dialog", "second window"))

    def accept(self):
        QMessageBox.information(self, "success",
                                "success.")
class MyButton(QPushButton):
    def __init__(self, parent=None):
        super(MyButton, self).__init__(parent)
        self.setFixedSize(111, 111)

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == QtCore.Qt.LeftButton:
            self.clicked.emit(True)
            self.parent().mousePressEvent(QMouseEvent)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = QDialog()
    ui = My_top_Dialog()
    ui.setupUi(dialog)
    dialog.show()
    sys.exit(app.exec_())

It's the second way to solve the problem. 这是解决问题的第二种方法。 Because the reply comment cannot show the code block, so I show my code here,I think the root cause of my problem is that I don't have associated main window and child window Completely.Whether it is building a relationship from a member or inheriting a class directly ,both of these Can make child window signal and trigger unaffected,Here is my code: 因为回复注释无法显示代码块,所以在这里显示我的代码,我认为问题的根本原因是我没有完整地关联主窗口和子窗口。它是从成员还是从关系中建立关系直接继承一个类,这两个都可以使子窗口信号触发不受影响,这是我的代码:

from PyQt5 import QtCore,QtWidgets
from PyQt5.QtWidgets import *
import sys

class My_top_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 532)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.Products = MyButton()
        self.verticalLayout.addWidget(self.Products)
        self.Products.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.Products.setText(_translate("Dialog", "top window"))

    def accept(self):
        dialog = My_second_Dialog()
        dialog.exec_()

class My_second_Dialog(QDialog,My_top_Dialog):
    def __init__(self,parent = None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 480)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.fetch = MyButton()
        self.verticalLayout.addWidget(self.fetch)
        self.fetch.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.fetch.setText(_translate("Dialog", "second window"))

    def accept(self):
        QMessageBox.information(self, "success",
                                "success.")
class MyButton(QPushButton):
    def __init__(self, parent=None):
        super(MyButton, self).__init__(parent)
        self.setFixedSize(111, 111)

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == QtCore.Qt.LeftButton:
            self.clicked.emit(True)
            self.parent().mousePressEvent(QMouseEvent)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = QDialog()
    ui = My_top_Dialog()
    ui.setupUi(dialog)
    dialog.show()
    sys.exit(app.exec_())

Try it: 试试吧:

from PyQt5 import QtCore,QtWidgets
from PyQt5.QtWidgets import *
import sys

class My_top_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 532)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.Products = MyButton()
        self.verticalLayout.addWidget(self.Products)
        self.Products.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.Products.setText(_translate("Dialog", "top window"))

    def accept(self):
        self.dialog = QDialog()
        self.ui     = My_second_Dialog()
        self.ui.setupUi(self.dialog)
        self.dialog.show()

class My_second_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 480)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.fetch = MyButton()
        self.verticalLayout.addWidget(self.fetch)
        self.fetch.clicked.connect(self.accept)
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.fetch.setText(_translate("Dialog", "second window"))

    def accept(self):
        QMessageBox.information(self.fetch, "Success", "success.")   # +++ self.fetch


class MyButton(QPushButton):
    def __init__(self, parent=None):
        super(MyButton, self).__init__(parent)
        self.setFixedSize(111, 111)

    def mousePressEvent(self, event): #QMouseEvent):
        if event.button() == QtCore.Qt.LeftButton:
            self.clicked.emit(True)
            #self.parent().mousePressEvent(QMouseEvent)               # --- 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = QDialog()
    ui = My_top_Dialog()
    ui.setupUi(dialog)
    dialog.show()
    sys.exit(app.exec_())

在此处输入图片说明

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

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