如何在Python中从QLineEdit读取文本?

7
我已经为我的插件创建了一个带有3个按钮的开始GUI,这很好用,如果我点击其中一个按钮,将启动特定的操作。到目前为止,这个工作正常。如果我点击其中一个按钮,会出现一个新的GUI,其中包括两个按钮“确定”和“取消”,以及一个行编辑器。如果我点击取消,GUI将关闭,如果我点击确定,我希望程序从编辑行中读取文本并将其存储在一个变量中。到目前为止,这还没有实现。

这里是包含对话框的类:

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)

这是一个类,包含使用QT Designer和pyuic4命令创建GUI之后的结构。
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))

在这个类中,我需要用到变量:
# 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)

这只是类中的一小部分,但我希望知道如何从LineEdit小部件中读取文本。

你有什么想法或建议吗?

感谢您,如果这是一个重复发布的帖子,对不起,但我没有找到适当的解决方法。

1个回答

10
文档中提到的,可以使用text方法获取QLineEdit的文本内容。
text = dlg.ui.lineEdit.text()

请注意它是一个QString,而不是普通的字符串,但只要您使用"%s" % text格式化它,这应该不会成为问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接