Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read out the text from QLineEdit in python?

I have created for my plugin a start GUI with 3 buttons. This works very well and if I click on one of the buttons a specific action is started. So far this works. If I click on one of the buttons a new GUI with two buttons "ok" and "cancel" and a lineedit appears. If I push on cancel the GUI will be closed, if I push on ok, I want the program to read the text from the editline and store it in a variable. This doesnßt work so far.

Here is the class containing the dialog:

from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import QDialog, QLineEdit

from ui_grz import Ui_Dialog

class grzDialog(QDialog):

    def __init__(self):
        QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

This is the class that contians the structure of the GUI after creating the GUI with QT Designer and the command pyuic4:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(387, 153)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 110, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(10, 10, 361, 51))
        self.label.setObjectName(_fromUtf8("label"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(10, 60, 351, 31))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "GRZ Analyse", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Dialog", "<html><head/><body><p><span style=\" font-weight:600;\">Bitte geben Sie hier den Schwellenwert für die GRZ-Analyse ein:</span></p><p>Bitte achten Sie auf eine korrekte Schreibweise (bspw. 2.5):</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))

And in this class I need the variable:

# Import the PyQt and QGIS libraries
from PyQt4.QtCore import * 
from PyQt4.QtGui import *
from qgis.core import *

# Import the code for the dialog
from ubgrzdialog import grzDialog

class quickAnalysis:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface

    def grzAnalysis(self):

        dlg = grzDialog()
        dlg.show()
        result = dlg.exec_()
        if result == 1:

            text = dlg.text()
            QMessageBox.information(self.iface.mainWindow(),"test", "%s" %(text), QMessageBox.Ok)

This is only one short part of the class, but this is the part where I have the question how to read the text from the LineEdit widget.

Do you have any ideas or suggestions?

Thanks and sorry if this is a double post, but I haven´t found an appropriate answer for my problem.

like image 462
Sven Avatar asked Aug 29 '12 16:08

Sven


1 Answers

As mentioned in the documentation, the text of a QLineEdit can be retrieved with its method text.

text = dlg.ui.lineEdit.text()

Note that it's a QString, not a regular string, but that shouldn't be a problem as you format it with your "%s" % text.

like image 198
Pierre GM Avatar answered Oct 06 '22 01:10

Pierre GM