[PyQt] Creating check boxes dynamically inside the listview based on the layers loaded in QGIS

Ahamed Farook farookrceg at gmail.com
Fri May 1 17:58:26 BST 2015

I have a two list views named "Datacheckerlistview1" and
"Datacheckerlistview2" in my gui. Now i had loaded layers in QGIS. Based on
the number of layers loaded, i want check boxes to be created dynamically
with the layer name inside the "Datacheckerlistview1"..( for ex: if there
are 4 layers loaded, i want 4 check boxes to be created dynamically inside
the Datacheckerlistview1). I had also attached my code below, i don't find
any check boxes inside the list view with the code below. I am new to
Python as well as QGIS. Help would be appreciated.

import os

from PyQt4 import QtGui, uic

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'DataCheckerModule_dialog_base.ui'))

class DataCheckerClassDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        super(DataCheckerClassDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect

    def dynamicCheckBoxes(self):
        """ Adds Checkboxes inside the listview dynamically based on the
number of layers loaded in QGIS. """

        canvas = qgis.utils.iface.mapCanvas()
        allLayers = canvas.layers()
        model = QStandardItemModel()
        for i in allLayers:
            item = QStandardItem('Item %s' % allLayers.name())
            check = Qt.checked if randint(0,1) == 1 else Qt.Unchecked

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150501/ecac6e12/attachment.html>

More information about the PyQt mailing list