[Qgis-user] Issues using a custom form with python logic

Jakob Lanstorp jlanstorp at gmail.com
Mon Apr 27 01:31:45 PDT 2015


I have a combobox and a lineedit in a custom form in QGIS with three issues.
This used to work in earlier QGIS version, but some of the signals seems to
be deprecated.

I have custom build Qt form with a QComBox and a QLineEdit like this:

<http://osgeo-org.1560.x6.nabble.com/file/n5203032/Image1.jpg> 

1. When I pick an animal and press ok the animal item is not written to the
table. In QGIS it is databined to a column name Animal and the Edit widge is
a Text Edit.
2. If nothing is entered in the Name lineedit the forms closes without
showing the validation error.
3. The record is entered twice if an animal is selected from the list, and a
value is entered as name.

*Python code:*

/#!/usr/local/bin/python
# coding: latin-1

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

animal_field = None
my_dialog = None

def formOpen(dialog,layer,feature):
  global my_dialog
  global animal_field
  global name_field
  
  my_dialog = dialog
  
  # Reference fields
  animal_field = dialog.findChild(QComboBox,"Animal")
  name_field = dialog.findChild(QLineEdit,"Name")
  buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")
	
  #print "animal_field: %s" % animal_field
  #print "animal_field.currentText(): %s" % animal_field.currentText()
 
  # Add some animal
  animal_field.clear()
  animal_field.addItems(["Dog","Cat","Bird"])  
 
  # Signals
  name_field.textChanged.connect(name_onTextChanged)
  
  buttonBox.accepted.disconnect(my_dialog.accept)  #deprecated to
my_dialog.save()
	
  # Wire up our own signals.
  buttonBox.accepted.connect(validate)
  buttonBox.rejected.connect(my_dialog.reject) # reject deprecated

def validate(): 
  # Make sure that the name field isn't empty.
  if not len(name_field.text()) > 0 or name_field.text() == 'NULL':
    name_field.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
    messageBox("Pet name of animal is missing!")
    return
  
  # Return the form as accepted to QGIS. Deprecated from my_dialog.accept()
to my_dialog.save()
  my_dialog.accept()
	
def name_onTextChanged(text):
  if not name_field:
    name_field.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
  else:
    name_field.setStyleSheet("")

def messageBox(text):
    msgBox = QMessageBox()
    msgBox.setText(text)
    msgBox.exec_()/


*Animal_test.ui:*

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>281</width>
    <height>172</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>120</y>
     <width>201</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QWidget" name="layoutWidget">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>50</y>
     <width>221</width>
     <height>48</height>
    </rect>
   </property>
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label_1">
      <property name="text">
       <string>Animal</string>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QComboBox" name="Animal"/>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="label_2">
      <property name="text">
       <string>Name</string>
      </property>
     </widget>
    </item>
    <item row="1" column="1">
     <widget class="QLineEdit" name="Name"/>
    </item>
   </layout>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>20</y>
     <width>221</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    
     <pointsize>12</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    
   </property>
   <property name="text">
    <string>Pick an pet type and name it</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
 <designerdata>
  <property name="gridDeltaX">
   <number>10</number>
  </property>
  <property name="gridDeltaY">
   <number>10</number>
  </property>
  <property name="gridSnapX">
   <bool>true</bool>
  </property>
  <property name="gridSnapY">
   <bool>true</bool>
  </property>
  <property name="gridVisible">
   <bool>true</bool>
  </property>
 </designerdata>
</ui>

Can anyone help me with one or more of my issues?



-----
Jakob Lanstorp
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Issues-using-a-custom-form-with-python-logic-tp5203032.html
Sent from the Quantum GIS - User mailing list archive at Nabble.com.



More information about the Qgis-user mailing list