PyQt4 - QGIS form error
Question: I've to build a form in QGIS to customize data input for each polygon in the
shapefile. I use QtDesigner to create a form (.ui), with some textboxes and
comboboxes pointing to the fields of my shapefile.  
Then I use the python file from Nathan QGIS Blog to add some logic.

Python code:

    
    
    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:

    
    
    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.  
I miss something... Please, anybody can help me?


Answer: It looks like you have a Python error more than a problem with QGIS.

You have two instances of if not 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. 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()'. 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. [There is
no text() function available for
strings](http://docs.python.org/2/library/string.html). And it looks like
nameField is actually supposed to be a QTextEdit object.

If nameField is a QTextEdit object, then you can use toPlainText() instead
which should do what you need it to do. 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.

Does that help at all?

