[英]PyQt4 - QGIS form error
我必須在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錯誤,而不是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()'。 沒有用於字符串的text()函數 。 看起來nameField實際上應該是QTextEdit對象。
如果nameField是QTextEdit對象,則可以使用toPlainText()代替它應該執行的操作。 所以像
if not nameField.toPlainText().strip().length() > 0:
在這種情況下,我還包含了.strip(),因此,如果文本字段中有空格,則不會得到肯定的結果。
這些幫助有用?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.