从 class 导入时 PyQt5 按钮 "clicked.connect()" 不工作

PyQt5 button "clicked.connect()" not working when importing from class

我正在 运行 测试 clicked.connect 失败问题。

我想将 CentralWidget 中的所有小部件包装到名为 React 的 class 中。当我在 CentralWidget 中调用 React 时,像 Buttonself.writeLineself.valLabel 这样的小部件会出现,但是当我按下 react Button 时,它没有运行,这是根据 self.writeLine.

更新 self.valLabel

快照:Buttom not working

我怀疑

Button.clicked.connect(self.BottomPressedTest) # no function when pressed

没有做应该做的事。但是不知道为什么会这样。

整个测试代码如下所示,

from PyQt5 import QtCore, QtWidgets, QtGui
import sys


class CentralWidget(QtWidgets.QWidget):

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

        self.mainLayout = QtWidgets.QGridLayout()
        self.setLayout(self.mainLayout)

        panel = React(1).groupbox

        self.mainLayout.addWidget(panel, 0, 0)


class React(QtWidgets.QGroupBox):
    def __init__(self, ch):
        self.value = 0
        self.groupbox = QtWidgets.QGroupBox()

        self.vbox = QtWidgets.QVBoxLayout()
        self.groupbox.setLayout(self.vbox)

        Button = QtWidgets.QPushButton('React')
        Button.clicked.connect(self.BottomPressedTest) # no function when pressed

        self.writeLine = QtWidgets.QSpinBox()#QLineEdit()
        self.writeLine.setFont(QtGui.QFont('Arial', 16))

        self.valLabel = QtWidgets.QLabel()
        self.valLabel.setFont(QtGui.QFont('Arial', 20))
        self.valLabel.setText(f'React: {self.value}')

        self.vbox.addWidget(Button)
        self.vbox.addWidget(self.valLabel)
        self.vbox.addWidget(self.writeLine)

    def BottomPressedTest(self):
        print('React bottom pressed.')
        self.valLabel.setText(f'React: {self.writeLine.text()}')


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):


        super(MainWindow, self).__init__(parent)

        self.setWindowTitle(QtCore.QCoreApplication.applicationName())

        self.centralWidget = CentralWidget()
        self.setCentralWidget(self.centralWidget)


class MainApplication(QtWidgets.QApplication):

    def __init__(self, argv: list):

        super(MainApplication, self).__init__(argv)
        self.mainWindow = MainWindow()
        self.mainWindow.resize(200,200) 
        self.mainWindow.raise_()
        self.mainWindow.show()

def main():

    application = MainApplication([])

    sys.exit(application.exec_())

if __name__ == "__main__":
    main()

编辑

我将 React 中的前几行更改为

class React(QtWidgets.QGroupBox):
    def __init__(self, ch):
        super().__init__()
        self.value = 0
        self.setStyleSheet('QGroupBox { color: white; border: 3px solid grey; }') #

        self.vbox = QtWidgets.QVBoxLayout()
        self.setLayout(self.vbox)

,然后将CentralWidget改为

class CentralWidget(QtWidgets.QWidget):

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

        self.mainLayout = QtWidgets.QGridLayout()
        self.setLayout(self.mainLayout)

        panel = React(1)
        self.mainLayout.addWidget(panel)

现在问题解决了。非常感谢@musicamante and @Passerby

您的 React 对象在 CentralWidget.__init__ 结束时被丢弃,只有 QGroupBox 保留。保存在一个属性中,你的问题就迎刃而解了。

请注意,您的 React 对象继承自 QGroupBox,但不会调用其 __init__ 方法,而是创建一个新的 QGroupBox 作为其成员。