PYQT5信号和插槽

时间:2018-11-17 11:23:34

标签: python pyqt pyqt5 signals-slots

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

1 个答案:

答案 0 :(得分:1)

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

尝试一下:

main.py

public class AppInfo {
internal var label: CharSequence? = null
internal var packageName: CharSequence? = null
internal var icon: Drawable? = null
internal var isInFav: Boolean? = false

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

enter image description here