Mar-04-2021, 12:58 PM
Your subclass idea will not work. There is no relationship between the hardware tab object and the enterNewHardware object. The textboxes in the enterNewHardware object are not the same textboxes that are in the hardwareTab.
When I push the enButton it creates a new newHardware object. This calls the hardwareTab.__init__() and creates a snTextBox widget. It makes a different snTextBox widget (id 0x28f730f3440). Also notice that nothing was printed for snTextBox.text().
To make this work you need to pass the original hardware tab object, the one that has the text boxes that hold the text you entered. This works:
Of course the other thing you could do is make the order function a method of the hardwareTab.
from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, \ QMainWindow, QWidget, QVBoxLayout, QTabWidget import sys class mainWindow(QMainWindow): def __init__(self): super().__init__() self.tabForm = QTabWidget() self.tabForm.addTab(hardwareTab(), "HARDWARE") self.setCentralWidget(self.tabForm) class hardwareTab(QWidget): def __init__(self): super().__init__() self.snTextBox = QLineEdit() self.enButton = QPushButton("ENTER NEW HARDWARE") layout = QVBoxLayout(self) layout.addWidget(self.snTextBox) layout.addWidget(self.enButton) print('HWT', self) print('textbox', self.snTextBox) print('text', self.snTextBox.text()) self.enButton.clicked.connect(newHardware) class newHardware(hardwareTab): def __init__(self): super().__init__() print('NHW', self) print('textbox', self.snTextBox) print('text', self.snTextBox.text()) if __name__ == "__main__": APP = QApplication(sys.argv) WINDOW = mainWindow() WINDOW.show() sys.exit(APP.exec_())
Output:HWT <__main__.hardwareTab(0x28f73018240) at 0x0000028F739FDD00>
textbox <PySide2.QtWidgets.QLineEdit(0x28f730188c0) at 0x0000028F739FDD40>
text
HWT <__main__.newHardware(0x28f730f3b00) at 0x0000028F739FDE80>
textbox <PySide2.QtWidgets.QLineEdit(0x28f730f3440) at 0x0000028F739FDF00>
text
NHW <__main__.newHardware(0x28f730f3b00) at 0x0000028F739FDE80>
textbox <PySide2.QtWidgets.QLineEdit(0x28f730f3440) at 0x0000028F739FDF00>
text
The first three lines in the output are the hardwareTab object being created. Notice the object ID for the textbox (0x28f730188c0).When I push the enButton it creates a new newHardware object. This calls the hardwareTab.__init__() and creates a snTextBox widget. It makes a different snTextBox widget (id 0x28f730f3440). Also notice that nothing was printed for snTextBox.text().
To make this work you need to pass the original hardware tab object, the one that has the text boxes that hold the text you entered. This works:
from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, \ QMainWindow, QWidget, QVBoxLayout, QTabWidget import sys class mainWindow(QMainWindow): def __init__(self): super().__init__() self.tabForm = QTabWidget() self.tabForm.addTab(hardwareTab(), "HARDWARE") self.setCentralWidget(self.tabForm) class hardwareTab(QWidget): def __init__(self): super().__init__() self.snTextBox = QLineEdit() self.enButton = QPushButton("ENTER NEW HARDWARE") print('HWT', self) print('textbox', self.snTextBox) print('text', self.snTextBox.text()) layout = QVBoxLayout(self) layout.addWidget(self.snTextBox) layout.addWidget(self.enButton) self.enButton.clicked.connect(lambda: newHardware(self)) def newHardware(tab): print('NHW', tab) print('textbox', tab.snTextBox) print('text', tab.snTextBox.text()) if __name__ == "__main__": APP = QApplication(sys.argv) WINDOW = mainWindow() WINDOW.show() sys.exit(APP.exec_())
Output:HWT <__main__.hardwareTab(0x16a37f56ce0) at 0x0000016A3893DC00>
textbox <PySide2.QtWidgets.QLineEdit(0x16a37f56a20) at 0x0000016A3893DC40>
text
NHW <__main__.hardwareTab(0x16a37f56ce0) at 0x0000016A3893DC00>
textbox <PySide2.QtWidgets.QLineEdit(0x16a37f56a20) at 0x0000016A3893DC40>
text This is the text I entered
Notice that the object ID's printed in the hardwareTab.__init__() match the ID's of the objects passed to the newHardware() function. These are the QLineEdit objects you are looking for. Also notice that the text I entered in the snTextBox (This is the text I entered) is printed out by the newHardware() function.Of course the other thing you could do is make the order function a method of the hardwareTab.
from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, \ QMainWindow, QWidget, QVBoxLayout, QTabWidget import sys class mainWindow(QMainWindow): def __init__(self): super().__init__() self.tabForm = QTabWidget() self.tabForm.addTab(hardwareTab(), "HARDWARE") self.setCentralWidget(self.tabForm) class hardwareTab(QWidget): def __init__(self): super().__init__() self.snTextBox = QLineEdit() self.enButton = QPushButton("ENTER NEW HARDWARE") layout = QVBoxLayout(self) layout.addWidget(self.snTextBox) layout.addWidget(self.enButton) self.enButton.clicked.connect(self.newHardware) def newHardware(self): print(self.snTextBox.text()) ## ORDER CODE GOES HERE if __name__ == "__main__": APP = QApplication(sys.argv) WINDOW = mainWindow() WINDOW.show() sys.exit(APP.exec_())