TransWikia.com

Get text from qformlayout

Geographic Information Systems Asked on April 15, 2021

I created a QFormLayout and I want to save the text that user types from the QLineEdit into some variables. So in my code, I want to get user input from e1,e2, e3, and e4. Here is my code:

import sys

class Dialog(QDialog):
    def __init__(self):
        super(Dialog, self).__init__()
        self.createFormGroupBox()
        
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)
        
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        self.setWindowTitle("Form Layout - pythonspot.com")
        
    def createFormGroupBox(self):
        self.formGroupBox = QGroupBox("Form layout")
        self.layout = QFormLayout()
        self.e1 = QLineEdit()
        self.e2 = QLineEdit()
        self.e3 = QLineEdit()
        self.e4 = QLineEdit()
        self.layout.addRow(QLabel("Date debut:"), self.e1)
        self.layout.addRow(QLabel("Heure debut:"), self.e2)
        self.layout.addRow(QLabel("Date fin:"), self.e3)
        self.layout.addRow(QLabel("heure fin:"), self.e4)
        self.formGroupBox.setLayout(self.layout)

app = QApplication(sys.argv)
dialog = Dialog()
dialog.show()
#sys.exit(dialog.exec_())

One Answer

The following code will work for you but you should have a look at the official Qt documentation, its really complete. because depending on what you want to do you might need QLineEdit.textEdited or QLineEdit.textChanged for example

import sys

class Dialog(QDialog):
    def __init__(self):
        super(Dialog, self).__init__()
        self.createFormGroupBox()

        self.savedText = {}

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.formGroupBox)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        self.setWindowTitle("Form Layout - pythonspot.com")


    def createFormGroupBox(self):
        self.formGroupBox = QGroupBox("Form layout")
        self.layout = QFormLayout()
        self.e1 = QLineEdit()
        self.e1.textEdited.connect(self.make_saveTextEdit(1))
        self.e2 = QLineEdit()
        self.e2.textEdited.connect(self.make_saveTextEdit(2))
        self.e3 = QLineEdit()
        self.e3.textEdited.connect(self.make_saveTextEdit(3))
        self.e4 = QLineEdit()
        self.e4.textEdited.connect(self.make_saveTextEdit(4))
        self.layout.addRow(QLabel("Date debut:"), self.e1)
        self.layout.addRow(QLabel("Heure debut:"), self.e2)
        self.layout.addRow(QLabel("Date fin:"), self.e3)
        self.layout.addRow(QLabel("heure fin:"), self.e4)
        self.formGroupBox.setLayout(self.layout)

    def make_saveTextEdit(self, x):
        def saveTextEdit(text):
            self.savedText[x] = text
        return saveTextEdit
   

app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()
print(dialog.savedText)
app.exit()

Correct answer by Louis Cottereau on April 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP