Emit()向其他类发出信号不起作用

时间:2019-04-25 17:07:52

标签: python pyqt pyqt5

我目前正在使用PyQt5在GUI上工作(在python和Qt方面我是菜鸟),我需要从一个类向另一个类发出Signal。

我阅读了有关此内容并在Google周围搜索,还发现了很多有用的东西,但它仍然对我不起作用。

这是我的虚拟代码:

1级:

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


class Class1(QWidget):
    eventButtonPressed = pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.Class1Btn = QPushButton('Button')
        self.Class1Edit = QLineEdit(self)

        self.Class1Btn.clicked.connect(self.clicked)

        # Layout stuff to mimic my real program
        self.Class1Grid = QGridLayout(self)
        self.Class1Grid.addWidget(self.Class1Btn)
        self.Class1Grid.addWidget(self.Class1Edit)
        self.groupBoxLayout1 = QGroupBox(self)
        self.groupBoxLayout1.setLayout(self.Class1Grid)

    def clicked(self):
        self.eventButtonPressed.emit(self.Class1Edit.text())

2级:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1


class Class2(QWidget):
    def __init__(self):
        super().__init__()
        self.Class1OBJ = Class1.Class1(self)
        self.Class1OBJ.eventButtonPressed.connect(self.StuffWhenSignalIsEmitted)

        # Layout stuff to mimic my real program
        self.Class2Edit = QLineEdit(self)
        self.Class2Grid = QGridLayout(self)
        self.Class2Grid.addWidget(self.Class2Edit)
        self.groupBoxLayout2 = QGroupBox(self)
        self.groupBoxLayout2.setLayout(self.Class2Grid)

    def StuffWhenSignalIsEmitted(self, text):
        print('Text from Class 2 Widget: {}'.format(self.Class2Edit))
        print('Text from Class 1 Widget: {}'.format(text))

我的主窗口:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1
import Class2


class MainWindow(QWidget, QApplication):
    def __init__(self):
        super().__init__()
        self.Class1OBJ = Class1.Class1()
        self.Class2OBJ = Class2.Class2()

        self.WinLayout = QVBoxLayout(self)
        self.WinLayout.addWidget(self.Class1OBJ.groupBoxLayout1)
        self.WinLayout.addWidget(self.Class2OBJ.groupBoxLayout2)
        self.setGeometry(1100, 300, 300, 300)
        self.setWindowTitle("GUI")
        self.show()


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

我希望程序在按下按钮时(如果可能)从StuffWhenSignalIsEmitted方法打印语句。所以我想打印第一类的LineEdit中的内容以及第二类中的LineEdit中的内容。

2 个答案:

答案 0 :(得分:0)

尝试一下:

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


class ClassTwo(QWidget):

    def __init__(self):
        super().__init__()
        self.classOneOBJ = ClassOne(self)                # + self
        self.classOneOBJ.eventButtonPressed.connect(self.StuffWhenSignalIsEmitted)

        layout = QGridLayout(self)
        layout.addWidget(self.classOneOBJ)

    def StuffWhenSignalIsEmitted(self, text):
        print('it worked ->{}'.format(text))
        # and do stuff with instance variables of an existing object

class ClassOne(QWidget):
    eventButtonPressed = pyqtSignal(str)

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

        self.lineEdit = QLineEdit() 

        self.Btn = QPushButton('Button')            
        self.Btn.clicked.connect(self.clicked)

        layout = QGridLayout(self)
        layout.addWidget(self.lineEdit)
        layout.addWidget(self.Btn)

    def clicked(self):
        self.eventButtonPressed.emit(self.lineEdit.text())


if __name__ == '__main__':
     app = QApplication(sys.argv)
     main = ClassTwo()
     main.show()
     sys.exit(app.exec_())    

enter image description here

答案 1 :(得分:0)

您似乎认为,如果变量在不同的类中具有相同的名称,则它是相同的变量,因为它们不是,所以它们是不同的对象。 Class2中的self.Class1OBJ与MainWindow中的self.Class1OBJ不同。

所以解决方案就是创建一个单一的self.Class1OBJ:

Class2.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Class2(QWidget):
    def __init__(self):
        super().__init__()

        # Layout stuff to mimic my real program
        self.Class2Edit = QLineEdit(self)
        self.Class2Grid = QGridLayout(self)
        self.Class2Grid.addWidget(self.Class2Edit)
        self.groupBoxLayout2 = QGroupBox(self)
        self.groupBoxLayout2.setLayout(self.Class2Grid)

    def StuffWhenSignalIsEmitted(self, text):
        print('Text from Class 2 Widget: {}'.format(self.Class2Edit))
        print('Text from Class 1 Widget: {}'.format(text))

main.py

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1
import Class2


class MainWindow(QWidget, QApplication):
    def __init__(self):
        super().__init__()
        self.Class1OBJ = Class1.Class1()
        self.Class2OBJ = Class2.Class2()
        # add the following line
        self.Class1OBJ.eventButtonPressed.connect(self.Class2OBJ.StuffWhenSignalIsEmitted)

        self.WinLayout = QVBoxLayout(self)
        self.WinLayout.addWidget(self.Class1OBJ.groupBoxLayout1)
        self.WinLayout.addWidget(self.Class2OBJ.groupBoxLayout2)

        self.setGeometry(1100, 300, 300, 300)
        self.setWindowTitle("GUI")
        self.show()


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