Skip to content Skip to sidebar Skip to footer

Unable To Select Checkbox Inside Treeview

I tried creating a treeview with checkboxes but I'm unable to select the checkboxes. on the flag method I had mentioned it as ItemisuserCheckable but still could not get it working

Solution 1:

you need to hold somewhere current item state (checked\unchecked) and change it once setdata() method is called. Your items are always unchecked because you're always returning QVariant(Qt.Unchecked) for them in the data() method.

I've changed a bit your code, see it would work for you:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

classTestItem():
    def__init__(self, name, checked):
        self.checked = checked
        self.name = name

classStbTreeView(QAbstractListModel):
    def__init__(self, args, parent=None):
        super(StbTreeView, self).__init__(parent)

        self.args = []
        for item_name in args:
            self.args.append(TestItem(item_name, False))

        for item in self.args:
            print item.name

    defrowCount(self, parent):
        returnlen(self.args)

    defheaderData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    defflags(self, index):
        return  Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

    defdata(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row()
            print self.args[row].name
            return self.args[row].name

        if role == Qt.CheckStateRole:
            row = index.row()
            print self.args[row].checked
            if self.args[row].checked == False:
                return QVariant(Qt.Unchecked)
            else:
                return QVariant(Qt.Checked)

    defsetData(self, index, value, role):
        if role == Qt.CheckStateRole:
            row = index.row()
            self.args[row].checked = not self.args[row].checked             
        returnTruedefmain():
    myapp = QApplication(sys.argv)

    data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']

    model = StbTreeView(data)
    tree_view = QTreeView()
    tree_view.show()
    tree_view.setModel(model)

    myapp.exec_()

if __name__ == '__main__':
    main()

hope this helps, regards

Solution 2:

Thanks it really worked for me. My Original requirement was to call this view/model on a combo box. I tried calling this but it did not work ... I'm able to see the view inside the combo box but unable to select any of the check boxes. I tried quite a few possibility but did not succeed..

Did a slight modification on your code to call from combo box.

the modified code is:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

classTestItem():
    def__init__(self, name, checked):
        self.checked = checked
        self.name = name

classStbTreeView(QAbstractListModel):
    def__init__(self, args, parent = None):
        super(StbTreeView, self).__init__(parent)

        self.args = []
        for item_name in args:
            self.args.append(TestItem(item_name, False))

        for item in self.args:
            print item.name

    #print 'Value of self.args is %s' % self.argsdefrowCount(self, parent):
        returnlen(self.args)

    defheaderData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    defflags(self, index):
        return  Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

    defdata(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row()
            print self.args[row].name
            return self.args[row].name

        if role == Qt.CheckStateRole:
            row = index.row()
            print self.args[row].checked
            if self.args[row].checked == False:
                return QVariant(Qt.Unchecked)
            else:
                return QVariant(Qt.Checked)

    defsetData(self, index, value, role):
        if role == Qt.CheckStateRole:
            row = index.row()
            self.args[row].checked = not self.args[row].checked             
        returnTrueclassTemplate(QTreeView):
    def__init__(self, parent=None):
        super(Template, self).__init__(parent)

        self.data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']
        self.MainUI()

    defMainUI(self):
        self.model = StbTreeView(self.data)
        self.setModel(self.model)


defmain():
    myapp = QApplication(sys.argv)
    temp = Template()
    temp.show()
    myapp.exec_()

if __name__ == '__main__':
    main()

The code from combo box:

stb_listview = QComboBox()
view = Template()
stb_listview.setView(view)
stb_listview.setModel(view.model)

Post a Comment for "Unable To Select Checkbox Inside Treeview"