Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Item in QListWidget cannot be checked even set the flags
Forum Updated to NodeBB v4.3 + New Features

Item in QListWidget cannot be checked even set the flags

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Moon River
    wrote on last edited by Moon River
    #1

    Hi, I am trying to make items in QListWidget checkable by user.
    I followed the instructions in Qt Flags reference to set the QListWidgetItem's flags as ItemIsUserCheckable and set the check state.
    However, when running the program, I found the checkboxes for items are grey and uncheckable.


    Here is my code:

    # import pyside2 modules
    
    app = QApplication()
    widget = QListWidget()
    for i in range(5):
        item = QListWidgetItem(str(i))
        
        item.setFlags(QtCore.Qt.ItemIsUserCheckable)
        item.setCheckState(QtCore.Qt.Unchecked)
        widget.addItem(item)
    
    # For comparison
    item = QListWidgetItem('6')
    widget.addItem(item)
    checkBox = QtWidgets.QCheckBox("This can be checkable")
    widget.setItemWidget(item,checkBox)
    
    widget.show()
    app.exec_()
    

    The program screen:
    e23b5c25-7f46-4e9d-a520-b043ccf24584-image.png

    The items[0-4] seems to be uncheckable.
    The last one can be checkable because it is a checkbox item.

    But items[0-4] should be checkable according to the reference and this tutorial

    This is what I want: (Items are strings, not checkboxes)
    88e9b906-4525-4bc5-9a21-1c9229a74134-image.png (picture from the above tutorial )


    So what did I miss? How could I solve it?

    Thanks for any help!

    JonBJ 1 Reply Last reply
    0
    • M Moon River

      Hi, I am trying to make items in QListWidget checkable by user.
      I followed the instructions in Qt Flags reference to set the QListWidgetItem's flags as ItemIsUserCheckable and set the check state.
      However, when running the program, I found the checkboxes for items are grey and uncheckable.


      Here is my code:

      # import pyside2 modules
      
      app = QApplication()
      widget = QListWidget()
      for i in range(5):
          item = QListWidgetItem(str(i))
          
          item.setFlags(QtCore.Qt.ItemIsUserCheckable)
          item.setCheckState(QtCore.Qt.Unchecked)
          widget.addItem(item)
      
      # For comparison
      item = QListWidgetItem('6')
      widget.addItem(item)
      checkBox = QtWidgets.QCheckBox("This can be checkable")
      widget.setItemWidget(item,checkBox)
      
      widget.show()
      app.exec_()
      

      The program screen:
      e23b5c25-7f46-4e9d-a520-b043ccf24584-image.png

      The items[0-4] seems to be uncheckable.
      The last one can be checkable because it is a checkbox item.

      But items[0-4] should be checkable according to the reference and this tutorial

      This is what I want: (Items are strings, not checkboxes)
      88e9b906-4525-4bc5-9a21-1c9229a74134-image.png (picture from the above tutorial )


      So what did I miss? How could I solve it?

      Thanks for any help!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Moon-River
      Note that your tutorial showed the line:

      item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
      

      You need something like Python:

      item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
      

      else you overwrite all flags, and lose those like Qt::ItemIsEnabled and Qt::ItemIsEditable, etc.

      https://doc.qt.io/qt-5/qlistwidgetitem.html#details

      By default, items are enabled, selectable, checkable, and can be the source of drag and drop operations

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Moon River
        wrote on last edited by Moon River
        #3

        @JonB
        Wow! Thanks for your help!
        It works! ^_^

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved