Issue
I am running a test for clicked.connect failing issue.
I want to wrap all the widgets in the CentralWidget
to a class called React
. When I call React
in CentralWidget
, widgets like Button
, self.writeLine
and self.valLabel
will show up, but when I pressed the react Button
, it's not functioning, which is to update self.valLabel
according to self.writeLine
.
A snap shot: Buttom not working
I suspect that
Button.clicked.connect(self.BottomPressedTest) # no function when pressed
is not doing what is supposed to do. But have no clue why this is happening.
The entire test code is shown below,
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()
Edit
I changed the first few line in React
to
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)
, and then change CentralWidget
to
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)
Now the problem is solved. Many thanks to @musicamante and @Passerby
Solution
Your React object is thrown away at the end of CentralWidget.__init__
, with only the QGroupBox persisting. Save it in an attribute and your problem will be solved.
Note that your React object inherits from QGroupBox, but does not call its __init__
method, instead creating a new QGroupBox as a member.
Answered By - Passerby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.