【发布时间】: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