简体   繁体   中英

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. 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_())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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