PYQT5 信号和插槽 [重复]

Posted

技术标签:

【中文标题】PYQT5 信号和插槽 [重复]【英文标题】:PYQT5 Signal & Slot [duplicate] 【发布时间】:2019-04-20 09:39:52 【问题描述】:

yyy.py(QT Designer生成的编辑后的UI代码)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'yyy.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot, pyqtSignal

class Ui_MainWindow(object):
    ClickAction = pyqtSignal(QtWidgets.QMainWindow)
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setStyleSheet("gridline-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 191, 551))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.treeWidget = QtWidgets.QTreeWidget(self.horizontalLayoutWidget)
        self.treeWidget.setAnimated(True)
        self.treeWidget.setObjectName("treeWidget")
        self.horizontalLayout.addWidget(self.treeWidget)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(470, 150, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(310, 110, 411, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(310, 210, 431, 91))
        self.plainTextEdit.setObjectName("plainTextEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setObjectName("actionClose")
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionClose)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionExit)
        self.menubar.addAction(self.menuFile.menuAction())
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.treeWidget.headerItem().setText(0, _translate("MainWindow", "1"))
        self.treeWidget.headerItem().setText(1, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(2, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(3, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(4, _translate("MainWindow", "New Column"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionClose.setText(_translate("MainWindow", "Close"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        self.pushButton.clicked.connect(self.On_ClickAction)

    def On_ClickAction(self):
        self.ClickAction.emit(self.lineEdit)

正在运行的app.py

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from yyy  import Ui_MainWindow


class Worker(QMainWindow):
    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)

    @pyqtSlot(QMainWindow)
    def pushButtonClicked(self,targetobj):
        targetobj.setText("Clicked")


    def make_Connection(self, uiobj):
        uiobj.ClickAction.connect(self,uiobj)


class AppWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        obj = Worker()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        obj.make_Connection(self.ui)
        self.show()  


app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())

我运行代码时收到的错误消息

Traceback (most recent call last):   File "C:\Python36-32\app.py", line 31, in <module>
    w = AppWindow()   File "C:\Python36-32\app.py", line 26, in __init__
    obj.make_Connection(self.ui)   File "C:\Python36-32\app.py", line 17, in make_Connection
    uiobj.ClickAction.connect(self,uiobj) TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context

【问题讨论】:

只有 QObjects 可以有 pyqtSignals,Ui_MainWindow 不是 QObject,因此它会产生这个错误,你应该阅读:pyqt.sourceforge.net/Docs/PyQt5/designer.html 【参考方案1】:

切勿编辑 QT Designer 创建的用户界面代码!!!

试试看:

main.py

import sys
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from yyy_ui  import Ui_MainWindow


#class Worker(QMainWindow):
#    def __init__(self, parent=None):
#        super(Worker, self).__init__(parent)
#    @pyqtSlot(QMainWindow)
#    def pushButtonClicked(self,targetobj):
#        targetobj.setText("Clicked")
#    def make_Connection(self, uiobj):
#        uiobj.ClickAction.connect(self, uiobj)


class AppWindow(QMainWindow):
    def __init__(self):
        super().__init__()
#        obj = Worker()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
#        obj.make_Connection(self.ui)
#        self.show() 

        self.ui.pushButton.clicked.connect(self.onClickAction)

    @pyqtSlot()
    def onClickAction(self):
        self.ui.plainTextEdit.setPlainText(self.ui.lineEdit.text())

app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())

yyy_ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'yyy.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
#from PyQt5.QtCore import pyqtSlot, pyqtSignal

class Ui_MainWindow(object):

#    ClickAction = pyqtSignal(QtWidgets.QMainWindow)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setStyleSheet("gridline-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 191, 551))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.treeWidget = QtWidgets.QTreeWidget(self.horizontalLayoutWidget)
        self.treeWidget.setAnimated(True)
        self.treeWidget.setObjectName("treeWidget")
        self.horizontalLayout.addWidget(self.treeWidget)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(470, 150, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(310, 110, 411, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(310, 210, 431, 91))
        self.plainTextEdit.setObjectName("plainTextEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setObjectName("actionClose")
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionClose)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionExit)
        self.menubar.addAction(self.menuFile.menuAction())
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.treeWidget.headerItem().setText(0, _translate("MainWindow", "1"))
        self.treeWidget.headerItem().setText(1, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(2, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(3, _translate("MainWindow", "New Column"))
        self.treeWidget.headerItem().setText(4, _translate("MainWindow", "New Column"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionClose.setText(_translate("MainWindow", "Close"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))

#        self.pushButton.clicked.connect(self.On_ClickAction)
#    def On_ClickAction(self):
#        self.ClickAction.emit("self.lineEdit.text")

【讨论】:

以上是关于PYQT5 信号和插槽 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5:使用插槽/信号时,self 是 NoneType

网址未使用不同类中的信号和插槽 PyQt5 定义

PyQt 信号和插槽:“新风格”发射?

PYQT5 - 插槽函数不采用默认参数 [重复]

如何将信号连接到不同线程中的插槽

正确设置 QStackedWidget,信号看不到功能/插槽