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

  • Home
  • SEARCH
  • 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 8914653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:46:41+00:00 2026-06-15T04:46:41+00:00

I have a QTreeWidget with QTreeWidgetItems . Method QTreeWidgetItem.flags() returns me Qt.ItemFlags instance. The

  • 0

I have a QTreeWidget with QTreeWidgetItems. Method QTreeWidgetItem.flags() returns me Qt.ItemFlags instance.

The Qt.ItemFlags type is a typedef for QFlags. It stores an OR combination of ItemFlag values.

Well, assume now that I have one of my QTreeWidgetItems and I need to find out — is it checkable or not? Other words, I need to find occurrence ItemFlag (for my case, it’s Qt.ItemIsUserCheckable) in Qt.ItemFlags instance.

How to do it?

Here’s a simple and tidy example that you can modify (the interested part marked by comment lines):

import sys
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.treeWidget = QtGui.QTreeWidget()
        self.treeWidget.setHeaderHidden(True)
        self.addItems(self.treeWidget.invisibleRootItem())
        self.treeWidget.itemClicked.connect (self.handleClicked)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.treeWidget)
        self.setLayout(layout)

    def addItems(self, parent):
        column = 0

        clients_item = QtGui.QTreeWidgetItem(parent, ['Clients'])
        clients_item.setData(column, QtCore.Qt.UserRole, 'data Clients')
        clients_item.setExpanded(True)

        item_1 = QtGui.QTreeWidgetItem(clients_item, ['Item 1'])
        item_1.setData(column, QtCore.Qt.UserRole, 'data Item 1')
        item_1.setCheckState(column, QtCore.Qt.Unchecked)

        item_2 = QtGui.QTreeWidgetItem(clients_item, ['Item 2'])
        item_2.setData(column, QtCore.Qt.UserRole, 'data Item 2')
        item_2.setCheckState(column, QtCore.Qt.Unchecked)

    def handleClicked(self, item, column):
        if item.checkState(column) == QtCore.Qt.Checked:
            print "checked", item, item.text(column)
        if item.checkState(column) == QtCore.Qt.Unchecked:
            print "NOT checked", item, item.text(column)

        # this part doesn't work ===============================
        # begin of part
        flags = item.flags()
        if QtCore.Qt.ItemIsUserCheckable in flags:
            print "is checkable", item, item.text(column)
        else:
            print "is NOT checkable", item, item.text(column)
        # end of part ==========================================    

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
  • 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-15T04:46:43+00:00Added an answer on June 15, 2026 at 4:46 am

    Like many of the “flags” and “attributes” in Qt, ItemFlags uses bit masks to combine multiple options into a single value. This can be tested using bitwise operators.

    Here is how you would check if the item is checkable:

    flags = item.flags()
    if flags & QtCore.Qt.ItemIsUserCheckable:
        print "is checkable", item, item.text(column)
    

    Bitwise Masking

    It works like this:

    Qt.NoItemFlags          0   It does not have any properties set.
    Qt.ItemIsSelectable     1   It can be selected.
    Qt.ItemIsEditable       2   It can be edited.
    Qt.ItemIsDragEnabled    4   It can be dragged.
    Qt.ItemIsDropEnabled    8   It can be used as a drop target.
    Qt.ItemIsUserCheckable  16  It can be checked or unchecked by the user.
    Qt.ItemIsEnabled        32  The user can interact with the item.
    Qt.ItemIsTristate       64  The item is checkable with three separate states.
    

    So the value will be some combination of these values. Lets say it is Selectable, Editable, and enabled. For simplicity I will just use the numeric values instead of the flags. We combine them with a logical OR:

    flags = 1 | 2 | 32
    # 35
    

    And to test the presence of a value we use a logical AND:

    # is it editable?
    flags & 2
    # 2 # would evaluate to True
    
    # is it checkable?
    flags & 16
    # 0 # would evaluate to False
    

    You could also toggle the flags state in the maks using an XOR operation.

    # toggle off the editable flag
    flags ^= 2
    # toggle on the editable flag
    flags ^= 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QTreeWidget with a bunch of QTreeWidgetItems. Each QTreeWidgetItem has a timestamp
I have a QTreeWidget with a bunch of QTreeWidgetItems. Each item has a couple
I have a QTreeWidgetItem added to a QTreeWidget : QTreeWidgetItem* item = new QTreeWidgetItem(ui->trwPairs);
I have a list of QTreeWidgetItems (with children) in a QTreeWidget. I do not
I have a QTreeWidget which needs to be populated with a large sum of
I have to populate a QTreeWidget with items (or children of items) that may
Is it possible to have individual indentation of items in a QTreeWidget? In specific,
I use a QTreeWidget to display multicolumn items, and I want to have a
I can't seem to get any mouse clicks in a QTreeWidget. I have tried...
I have a QTreeWidget that reads data from an XML file. If at any

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.