Python Forum
Simple printing the text for a QLineEdit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple printing the text for a QLineEdit
#11
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.
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_())
Reply


Messages In This Thread
RE: Simple printing the text for a QLineEdit - by deanhystad - Mar-04-2021, 12:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,839 Nov-04-2022, 09:04 PM
Last Post: malonn
  How to accept only float number in QLineEdit input ism 5 28,507 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 7,933 Oct-21-2020, 02:35 PM
Last Post: arbiel
  Two QlineEdit box, which interrelated GMCobraz 1 2,436 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 7,186 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 3,233 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,331 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,729 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 7,780 Aug-15-2019, 02:37 PM
Last Post: Denni
  How to validate multiple QLineEdit widgets without addressing them separately? mart79 3 4,275 Aug-08-2019, 12:50 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020