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

Ahamed Farook farookrceg at gmail.com
Sat May 2 12:06:47 BST 2015


Hi Florian,

Thanks for the response. I tried your suggestion as below.

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):
        """Constructor."""
        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
        self.setupUi(self)

    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
            item.setCheckState(check)
            item.setCheckable(True)
            model.appendRow(item)
            return model


        DatacheckerlistView1.setModel(model)
        DatacheckerlistView1.show()

And also in the main python file, i made an action like when this
DatacheckerlistView1 is clicked, then it should call the class something
named  "addLayers"(in main Python file)  which is the inheritance of
dynamicCheckBoxes method from DataCheckerClassDialog (our base class of
ui). As a result nothing happened when i clicked the list view after the
layers had been added in QGIS. Am i missing anything?

Thanks,
M.Farook

*Regards,*
*Ahamed Farook M,*
*Systems Programmer,*
*KCube Consultancy Services Pvt Ltd,*
*Eldams Road,*
*Chennai - 18*
*Email: farook at kcubeconsulting.com <farook at kcubeconsulting.com>*
*Ph: +91-9952216024*



On Sat, May 2, 2015 at 3:15 PM, Florian Bruhin <me at the-compiler.org> wrote:

> Hi,
>
> * Ahamed Farook <farookrceg at gmail.com> [2015-05-01 22:28:26 +0530]:
> >     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
> >             item.setCheckState(check)
> >             item.setCheckable(True)
> >             model.appendRow(item)
>
> It seems you create a new QStandardItemModel here, but never use it -
> you'd probably have to do something like   yourview.setModel(model)
> at some point.
>
> Florian
>
> --
> http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
>    GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
>          I love long mails! | http://email.is-not-s.ms/
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150502/e1f5f67c/attachment.html>


More information about the PyQt mailing list