python - 如何在 python 中从 QLineEdit 中读出文本?

标签 python qt-creator qgis qlineedit

我为我的插件创建了一个带有 3 个按钮的启动 GUI。这非常有效,如果我单击其中一个按钮,则会启动一个特定的操作。到目前为止,这是有效的。如果我单击其中一个按钮,则会出现一个带有两个按钮“确定”和“取消”的新 GUI,并且会出现一个 lineedit。如果我按下取消,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 之后包含 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)

这只是类(class)的一小部分,但这是我对如何从 LineEdit 小部件读取文本有疑问的部分。

您有什么想法或建议吗?

谢谢,如果这是一个双重帖子,我很抱歉,但我还没有找到适合我的问题的答案。

最佳答案

documentation 中所述, QLineEdit 的文本可以用它的方法 text 检索。

text = dlg.ui.lineEdit.text()

请注意,它是一个 QString,而不是一个常规字符串,但是当您使用 "%s"% text 格式化它时,这应该不是问题。

关于python - 如何在 python 中从 QLineEdit 中读出文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182057/

相关文章:

python - 让 pylint 识别自定义模块加载器

c++ - 如何在 QT 中获得调整大小 handle ?

c++ - Qt Creator 构建项目 - 没有这样的文件或目录

python - Die Ordnungszahl 63 wurde in der DLL "libexpat.dll"nicht gefunden

python - 如何通过 PyQGIS 使用 cpt-city 目录中的颜色渐变

python - 相当于 MATLAB spy 的 scipy

Python Pyforms : how to use css in Pyforms or html-api in pyforms

java - 从 PHP 脚本调用 Java(或 python 或 perl)

c++ - 在 QTCreator Undefined Reference 中使用库

c++ - 具有无响应 keyPressEvent 的 QGraphicsPixmapItem