简体   繁体   English

PyQt4-QGIS表单错误

[英]PyQt4 - QGIS form error

I've to build a form in QGIS to customize data input for each polygon in the shapefile. 我必须在QGIS中构建一个表单,以自定义shapefile中每个多边形的数据输入。 I use QtDesigner to create a form (.ui), with some textboxes and comboboxes pointing to the fields of my shapefile. 我使用QtDesigner创建一个窗体(.ui),其中一些文本框和组合框指向我的shapefile的字段。
Then I use the python file from Nathan QGIS Blog to add some logic. 然后,我使用Nathan QGIS Blog中的python文件添加一些逻辑。

Python code: 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("")

So I open an edit session in QGIS and I click on a polygon with Identify tool, but when I clik on OK button on my customized form, regardless field PART is NULL or not, the following error occurs: 因此,我在QGIS中打开一个编辑会话,并使用“识别”工具单击一个多边形,但是当我单击自定义表单上的“确定”按钮时,无论字段PART是否为NULL,都会发生以下错误:

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

I'm running QGIS 1.7.4, Python 2.7.2, Windows 7 64-bit. 我正在运行QGIS 1.7.4,Python 2.7.2,Windows 7 64位。
I miss something... Please, anybody can help me? 我想念某事...请,有人可以帮助我吗?

It looks like you have a Python error more than a problem with QGIS. 看来您遇到的是Python错误,而不是QGIS问题。

You have two instances of if not nameField.text().length() > 0: 如果没有nameField.text()。length()> 0,则有两个实例:

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

and

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

Initially, it looks like nameField is not an input for either of these functions. 最初,似乎nameField不是这两个函数的输入。 So I guess these are assigned somewhere else and you've reduced the code example. 因此,我想这些已分配到其他位置,并且您减少了代码示例。 Also, you have text as a variable input for 'Name_onTextChanged' but you also try and use it as a function 'nameField.text().length()'. 另外,您可以将文本作为“ Name_onTextChanged”的变量输入,但也可以尝试将其用作函数“ nameField.text()。length()”。 This might be a problem. 这可能是个问题。

Generally, Python is complaining because it cannot perform the operation 'text()' on the variable nameField, which it believes is a string. 通常,Python抱怨是因为它无法对变量nameField(它认为是字符串)执行操作'text()'。 There is no text() function available for strings . 没有用于字符串的text()函数 And it looks like nameField is actually supposed to be a QTextEdit object. 看起来nameField实际上应该是QTextEdit对象。

If nameField is a QTextEdit object, then you can use toPlainText() instead which should do what you need it to do. 如果nameField是QTextEdit对象,则可以使用toPlainText()代替它应该执行的操作。 So something like 所以像

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

In this instance, I have included .strip() as well so that you do not get a positive result if there are white spaces in text field. 在这种情况下,我还包含了.strip(),因此,如果文本字段中有空格,则不会得到肯定的结果。

Does that help at all? 这些帮助有用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM