【问题标题】:PyQt4 - QGIS form errorPyQt4 - QGIS 表单错误
【发布时间】:2012-05-31 09:07:42
【问题描述】:

我必须在 QGIS 中构建一个表单来自定义 shapefile 中每个多边形的数据输入。 我使用 QtDesigner 创建一个表单 (.ui),其中一些文本框和组合框指向我的 shapefile 的字段。
然后我使用来自 Nathan QGIS Blog 的 python 文件来添加一些逻辑。

Python 代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

nameField = None
myDialog = None

def formOpen(dialog,layerid,featureid):
    global myDialog
    myDialog = dialog
    global nameField
    nameField = dialog.findChild(QTextEdit,"PART")
    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")

    nameField.textChanged.connect(Name_onTextChanged)

    # Disconnect the signal that QGIS has wired up for the dialog to the button box.
    buttonBox.accepted.disconnect(myDialog.accept)

    # Wire up our own signals.
    buttonBox.accepted.connect(validate)
    buttonBox.rejected.connect(myDialog.reject)

def validate():
    # Make sure that the name field isn't empty.
    if not nameField.text().length() > 0:
        nameField.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
        msgBox = QMessageBox()
        msgBox.setText("Field PART must not be NULL.")
        msgBox.exec_()
    else:
        # Return the form as accpeted to QGIS.
        myDialog.accept()

def Name_onTextChanged(text):
    if not nameField.text().length() > 0:
        nameField.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
    else:
        nameField.setStyleSheet("")

所以我在 QGIS 中打开一个编辑会话并使用识别工具单击多边形,但是当我单击自定义表单上的确定按钮时,无论字段 PART 是否为 NULL,都会发生以下错误:

ERROR CODE LINE >>>> if not nameField.text().length() > 0:
ERROR MESSAGE   >>>> AttributeError: 'str' object has no attribute 'text'

我正在运行 QGIS 1.7.4、Python 2.7.2、Windows 7 64 位。
我想念一些东西...拜托,有人可以帮助我吗?

【问题讨论】:

    标签: python pyqt4 qt-designer qgis


    【解决方案1】:

    看起来您遇到的 Python 错误不仅仅是 QGIS 的问题。

    如果不是 nameField.text().length() > 0,您有两个实例:

    def validate():
        if not nameField.text().length() > 0:
    

    def Name_onTextChanged(text):
        if not nameField.text().length() > 0:
    

    最初,nameField 似乎不是这两个函数的输入。所以我猜这些是分配在其他地方的,你已经减少了代码示例。此外,您将文本作为“Name_onTextChanged”的变量输入,但您也尝试将其用作函数“nameField.text().length()”。这可能是个问题。

    通常,Python 会抱怨,因为它无法对变量 nameField 执行操作“text()”,它认为这是一个字符串。 There is no text() function available for strings。看起来 nameField 实际上应该是一个 QTextEdit 对象。

    如果 nameField 是一个 QTextEdit 对象,那么你可以使用 toPlainText() 代替它应该做你需要它做的事情。所以像

    if not nameField.toPlainText().strip().length() > 0:
    

    在这种情况下,我也包含了 .strip(),这样如果文本字段中有空格,您就不会得到肯定的结果。

    这有帮助吗?

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 2012-08-11
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2012-02-18
      • 1970-01-01
      相关资源
      最近更新 更多