I am fairly new to pyQGIS plugin development. In my "goodNeighbor_dialog_base.ui" file I have two comboBoxes 1 and 2. When my plugin loads, I am successfully able to load all layers in comboBox1 by calling a custom function. I have another function getcolumns(self) that should populate comboBox2 with attributes of the layer selected in comboBox1, when the user selects a layer in it. To do that I am using the "currentIndexChanged" event as shown in the last line of the code segment
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'goodNeighbor_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
self.dlg = goodNeighborDialog()
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&goodNeighbor')
self.toolbar = self.iface.addToolBar(u'goodNeighbor')
self.toolbar.setObjectName(u'goodNeighbor')
self.dlg.comboBox1.currentIndexChanged.connect(self.getcolumns)
then I have written the following function
def getcolumns(self):
print("HI")
self.dlg.comboBox2.clear()
layer_name = self.dlg.layerCombo.currentText()
print("you have selected " + layer_name)
The problem is, the function "getcolumns" is not triggering at all.
2 Answers 2
to connecting to the SIGNAL emit by the QOobjet as named currentIndexChanged. One way is :
QObject.connect(self.dlg.comboBox1,SIGNAL("currentIndexChanged(int)"),self.getcolumns)
But there is also the decorator way as it's recommend in the PyQt doc : http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html#the-pyqtslot-decorator
look at this answer to see another example : example
-
thanks ..... I was able to solve the problem by removing the dialog creation "self.dlg = goodNeighborDialog()" from add_action() to __init__(self, iface) after comparing with another plugin and it worked. However, I tried your method as well , but I got NameError: global name 'QObject' is not defined.user80602– user806022017年10月11日 02:20:46 +00:00Commented Oct 11, 2017 at 2:20
-
QObject is a PyQt object so you should import it before use
PyQt4.QtCore import *
Hugo Roussaffa– Hugo Roussaffa2017年10月11日 09:07:12 +00:00Commented Oct 11, 2017 at 9:07
Any event statement in the __init__(self, iface)
function only gets called once during initial plugin load. You need to put the combo event statement at the end of the initGui()
function so it gets called on any index change like.
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/MyPluginName/icon.png'
self.add_action(
icon_path,
text=self.tr(u'Plugin Name'),
callback=self.run,
parent=self.iface.mainWindow())
self.dlg.comboBox1.currentIndexChanged.connect(self.getcolumns)
self.dlg.comboBox1.currentIndexChanged.connect(self.getcolumns)
in theinitGui( self )
function. Do you receive any errors in the Python Console when you change selection in your combobox?