Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:23:56+00:00 2026-06-10T19:23:56+00:00

I implemented a cutom model for treeview for having checkboxes inside the treeview. If

  • 0

I implemented a cutom model for treeview for having checkboxes inside the treeview. If I check a parent node all child nodes should be chekced automatically. This basically works but der is a time lack between checking the parent node and updating the subnodes.

from PyQt4 import QtCore, QtGui
import sys

class Node(object):   
    def __init__(self, name, parent=None, checked=False):

        self._name = name
        self._children = []
        self._parent = parent
        self._checked = checked

        if parent is not None:
            parent.addChild(self)

    def addChild(self, child):
        self._children.append(child)

    def insertChild(self, position, child):
        if position < 0 or position > len(self._children):
            return False

        self._children.insert(position, child)
        child._parent = self
        return True

    def name(self):
        return self._name

    def checked(self):
        return self._checked

    def setChecked(self, state):
        self._checked = state

        for c in self._children:
            c.setChecked(state)

    def child(self, row):
        return self._children[row]

    def childCount(self):
        return len(self._children)

    def parent(self):
        return self._parent

    def row(self):
        if self._parent is not None:
            return self._parent._children.index(self)

class TreeModel(QtCore.QAbstractItemModel):
    def __init__(self, root, parent=None):
        super().__init__(parent)
        self._rootNode = root

    def rowCount(self, parent):
        if not parent.isValid():
            parentNode = self._rootNode
        else:
            parentNode = parent.internalPointer()

        return parentNode.childCount()

    def columnCount(self, parent):
        return 1

    def data(self, index, role):
        if not index.isValid():
            return None

        node = index.internalPointer()

        if role == QtCore.Qt.DisplayRole:
            if index.column() == 0:
                return node.name()
        if role == QtCore.Qt.CheckStateRole:
            if node.checked():
                return QtCore.Qt.Checked
            else:
                return QtCore.Qt.Unchecked

    def setData(self, index, value, role=QtCore.Qt.EditRole):

        if index.isValid():
            if role == QtCore.Qt.CheckStateRole:
                node = index.internalPointer()
                node.setChecked(not node.checked())
                return True
        return False

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            return "Nodes"

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsUserCheckable

    def parent(self, index):
        node = self.getNode(index)
        parentNode = node.parent()

        if parentNode == self._rootNode:
            return QtCore.QModelIndex()
        return self.createIndex(parentNode.row(), 0, parentNode)

    def index(self, row, column, parent):
        parentNode = self.getNode(parent)
        childItem = parentNode.child(row)

        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    def getNode(self, index):
        if index.isValid():
            node = index.internalPointer()
            if node:
                return node

        return self._rootNode

    def removeRows(self, position, rows, parent=QtCore.QModelIndex()):

        parentNode = self.getNode(parent)
        self.beginRemoveRows(parent, position, position + rows - 1)

        for row in range(rows):
            success = parentNode.removeChild(position)

        self.endRemoveRows()

        return success

def main_simple():
    app = QtGui.QApplication(sys.argv)

    rootNode   = Node("Root")
    n1 = Node("Node1", rootNode)
    n2 = Node("Node2", rootNode)
    n3 = Node("Node3", rootNode)

    n1_1 = Node("Node1 1", n1)
    n1_2 = Node("Node1 2", n1)
    n1_3 = Node("Node1 3", n1)

    n2_1 = Node("Node2 1", n2)
    n2_2 = Node("Node2 2", n2)
    n2_3 = Node("Node2 3", n2)

    n3_1 = Node("Node3 1", n3)
    n3_2 = Node("Node3 2", n3)
    n3_3 = Node("Node3 3", n3)

    model = TreeModel(rootNode)

    treeView = QtGui.QTreeView()
    treeView.show()
    treeView.setModel(model)

    sys.exit(app.exec_())

if __name__ == '__main__':
    main_simple()

How can I avoid this lack so that the userinterface is more smoothly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:23:58+00:00Added an answer on June 10, 2026 at 7:23 pm

    The model should emit the dataChanged() signal for the children when they are toggled, so that the view can update their checkboxes instantly. According to the documentation, setData should also emit dataChanged for the item that was explicitly changed.

    This should work:

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if index.isValid():
            if role == QtCore.Qt.CheckStateRole:
                node = index.internalPointer()
                node.setChecked(not node.checked())
                self.dataChanged.emit(index, index)               
                self.emitDataChangedForChildren(index)
                return True
        return False
    
    def emitDataChangedForChildren(self, index):
        count = self.rowCount(index)
        if count:            
            self.dataChanged.emit(index.child(0, 0), index.child(count-1, 0))
            for child in range(count):
                self.emitDataChangedForChildren(index.child(child, 0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How should I properly implement data access in my custom model binders? Like in
I have implemented a list of users in my Qt program, using the model/view
This is my Image model, in which I've implemented a method for validating the
I have implemented a custom Table Model as follows: public class MyTableModel extends AbstractTableModel
Alright. I have implemented a custom JTable model that includes the whole @Override public
I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't
I implemented a custom membership provider. I've also implemented my custom AES Encryption/Decryption overriding
I implemented a custom Profile object in code as described by Joel here: How
I've implemented a custom IEqualityComparer for EventLogEntry. public class EventLogEntryListComparison : IEqualityComparer<List<EventLogEntry>>, IEqualityComparer<EventLogEntry> For
I had implemented a custom Gallery with custom Adapter. My Requirement is that however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.