我想捕获子窗口的鼠标事件,但问题是鼠标事件没有传递给这个子窗口.... 在互联网上查找信息后,我发现我需要自定义按钮。但是在执行此操作后我仍然没有得到任何响应。 谁能告诉我还需要做些什么来捕获这个子窗口中的鼠标事件? 这是我的代码:
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_())
答案 0 :(得分:0)
这是解决问题的第二种方式。 因为回复评论不能显示代码块,所以我在这里显示我的代码,我认为我的问题的根本原因是我没有完全关联主窗口和子窗口。是否它正在构建一个关系成员或直接继承一个类,这两个都可以使子窗口信号和触发器不受影响,这是我的代码:
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_())
答案 1 :(得分:-1)
试一试:
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_())