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. Qt.CheckStateRole not work in Pyside 6.3.1
QtWS25 Last Chance

Qt.CheckStateRole not work in Pyside 6.3.1

Scheduled Pinned Locked Moved Solved Qt for Python
pysideqt for pythonpython
6 Posts 4 Posters 906 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.
  • A Offline
    A Offline
    alonginhh
    wrote on last edited by
    #1

    Hi everyone,

    I upgraded the Pyside6 from 6.2.3 to 6.3.1, but I found the Qt.CheckStateRole didn't show correctly in QTreeWidgetItem。The check icon didn't show .

    self.setData(0, Qt.CheckStateRole, Qt.Checked)
    

    PySide6.3.1
    PySide6.3.1

    PySide6.2.3
    PySide6.2.3

    Could someone help this? THANK YOU!

    1 Reply Last reply
    0
    • A alonginhh

      Thanks for the replies.

      Here is my code.

      import sys
      
      from PySide6.QtCore import Qt
      from PySide6.QtWidgets import (
          QAbstractItemView,
          QApplication,
          QHeaderView,
          QLabel,
          QMainWindow,
          QTreeWidget,
          QTreeWidgetItem,
          QVBoxLayout,
          QWidget,
      )
      
      
      class MyTreeItem(QTreeWidgetItem):
          def __init__(self, title):
              super().__init__()
              self.title = title
              self.setText(0, self.title)
              self.setData(0, Qt.CheckStateRole, Qt.Unchecked)
          def setChecked(self, checked):
              """Set the check state of the item."""
              self.setData(
                  0,
                  Qt.CheckStateRole,
                  Qt.Checked if checked else Qt.Unchecked,
              )
      
      
      class MyTreeWidget(QTreeWidget):
          def __init__(self, contents, parent=None):
              super().__init__(parent)
              self.setAllColumnsShowFocus(True)
              self.setDragDropMode(QAbstractItemView.NoDragDrop)
              self.setSelectionMode(QAbstractItemView.MultiSelection)
              self.setStyleSheet("QTreeView::item{margin-top:1px;margin-bottom:1px;}")
              self.setColumnCount(1)
              self.setHeaderLabels(["Title"])
      
              # add item to the tree
              my_items = []
              for content in contents:
                  my_items.append(MyTreeItem(content))
              self.addTopLevelItems(my_items)
          def selectionChanged(self, selected, deselected):
              """Selection changed."""
              super().selectionChanged(selected, deselected)
              self.blockSignals(True)
              for index in selected.indexes():
                  item = self.itemFromIndex(index)
                  # did not work at PySide6.3.1
                  item.setChecked(True)
                  # item.setData(0, Qt.CheckStateRole, Qt.Checked)
              for index in deselected.indexes():
                  item = self.itemFromIndex(index)
                  # did not work at PySide6.3.1
                  item.setChecked(False)
                  # item.setData(0, Qt.CheckStateRole, Qt.Unchecked)
              self.blockSignals(False)
      
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super().__init__()
              self.setWindowTitle('TREEWIDGET_DEMO')
      
              # self.setMinimumSize(400, 460)
              main_layout = QVBoxLayout()
      
              label = QLabel("Demo")
              label.setAlignment(Qt.AlignCenter)
              main_layout.addWidget(label)
      
              data = [
                  "Demo 01",
                  "Demo 02",
                  "Demo 03",
                  "Demo 04",
                  "Demo 05",
                  "Demo 06",
              ]
              self.my_list_tree = MyTreeWidget(data)
              self.my_list_tree.setFixedHeight(200)
              main_layout.addWidget(self.my_list_tree, 1)
      
              widget = QWidget()
              widget.setLayout(main_layout)
              self.setCentralWidget(widget)
      
      
      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      app.exec()
      

      The selectionChanged method has different behavior between PySide6.2.3 and 6.3.1.

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

      @alonginhh
      Not sure, but I think the bug report suggests converting Qt.Checked/Unchecked to int() to make it work for now?

      A 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        Which platform are you running this on ?
        How did you install it ?

        Can you provide a minimal runnable script that shows this behaviour ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • CristianMaureiraC Offline
          CristianMaureiraC Offline
          CristianMaureira
          wrote on last edited by
          #3

          Might be related to https://bugreports.qt.io/browse/PYSIDE-1930, there are many other issues duplicating the same wrong behavior. Will be fixed in 6.3.2

          1 Reply Last reply
          1
          • A Offline
            A Offline
            alonginhh
            wrote on last edited by
            #4

            Thanks for the replies.

            Here is my code.

            import sys
            
            from PySide6.QtCore import Qt
            from PySide6.QtWidgets import (
                QAbstractItemView,
                QApplication,
                QHeaderView,
                QLabel,
                QMainWindow,
                QTreeWidget,
                QTreeWidgetItem,
                QVBoxLayout,
                QWidget,
            )
            
            
            class MyTreeItem(QTreeWidgetItem):
                def __init__(self, title):
                    super().__init__()
                    self.title = title
                    self.setText(0, self.title)
                    self.setData(0, Qt.CheckStateRole, Qt.Unchecked)
                def setChecked(self, checked):
                    """Set the check state of the item."""
                    self.setData(
                        0,
                        Qt.CheckStateRole,
                        Qt.Checked if checked else Qt.Unchecked,
                    )
            
            
            class MyTreeWidget(QTreeWidget):
                def __init__(self, contents, parent=None):
                    super().__init__(parent)
                    self.setAllColumnsShowFocus(True)
                    self.setDragDropMode(QAbstractItemView.NoDragDrop)
                    self.setSelectionMode(QAbstractItemView.MultiSelection)
                    self.setStyleSheet("QTreeView::item{margin-top:1px;margin-bottom:1px;}")
                    self.setColumnCount(1)
                    self.setHeaderLabels(["Title"])
            
                    # add item to the tree
                    my_items = []
                    for content in contents:
                        my_items.append(MyTreeItem(content))
                    self.addTopLevelItems(my_items)
                def selectionChanged(self, selected, deselected):
                    """Selection changed."""
                    super().selectionChanged(selected, deselected)
                    self.blockSignals(True)
                    for index in selected.indexes():
                        item = self.itemFromIndex(index)
                        # did not work at PySide6.3.1
                        item.setChecked(True)
                        # item.setData(0, Qt.CheckStateRole, Qt.Checked)
                    for index in deselected.indexes():
                        item = self.itemFromIndex(index)
                        # did not work at PySide6.3.1
                        item.setChecked(False)
                        # item.setData(0, Qt.CheckStateRole, Qt.Unchecked)
                    self.blockSignals(False)
            
            
            class MainWindow(QMainWindow):
                def __init__(self):
                    super().__init__()
                    self.setWindowTitle('TREEWIDGET_DEMO')
            
                    # self.setMinimumSize(400, 460)
                    main_layout = QVBoxLayout()
            
                    label = QLabel("Demo")
                    label.setAlignment(Qt.AlignCenter)
                    main_layout.addWidget(label)
            
                    data = [
                        "Demo 01",
                        "Demo 02",
                        "Demo 03",
                        "Demo 04",
                        "Demo 05",
                        "Demo 06",
                    ]
                    self.my_list_tree = MyTreeWidget(data)
                    self.my_list_tree.setFixedHeight(200)
                    main_layout.addWidget(self.my_list_tree, 1)
            
                    widget = QWidget()
                    widget.setLayout(main_layout)
                    self.setCentralWidget(widget)
            
            
            app = QApplication(sys.argv)
            window = MainWindow()
            window.show()
            app.exec()
            

            The selectionChanged method has different behavior between PySide6.2.3 and 6.3.1.

            JonBJ 1 Reply Last reply
            0
            • A alonginhh

              Thanks for the replies.

              Here is my code.

              import sys
              
              from PySide6.QtCore import Qt
              from PySide6.QtWidgets import (
                  QAbstractItemView,
                  QApplication,
                  QHeaderView,
                  QLabel,
                  QMainWindow,
                  QTreeWidget,
                  QTreeWidgetItem,
                  QVBoxLayout,
                  QWidget,
              )
              
              
              class MyTreeItem(QTreeWidgetItem):
                  def __init__(self, title):
                      super().__init__()
                      self.title = title
                      self.setText(0, self.title)
                      self.setData(0, Qt.CheckStateRole, Qt.Unchecked)
                  def setChecked(self, checked):
                      """Set the check state of the item."""
                      self.setData(
                          0,
                          Qt.CheckStateRole,
                          Qt.Checked if checked else Qt.Unchecked,
                      )
              
              
              class MyTreeWidget(QTreeWidget):
                  def __init__(self, contents, parent=None):
                      super().__init__(parent)
                      self.setAllColumnsShowFocus(True)
                      self.setDragDropMode(QAbstractItemView.NoDragDrop)
                      self.setSelectionMode(QAbstractItemView.MultiSelection)
                      self.setStyleSheet("QTreeView::item{margin-top:1px;margin-bottom:1px;}")
                      self.setColumnCount(1)
                      self.setHeaderLabels(["Title"])
              
                      # add item to the tree
                      my_items = []
                      for content in contents:
                          my_items.append(MyTreeItem(content))
                      self.addTopLevelItems(my_items)
                  def selectionChanged(self, selected, deselected):
                      """Selection changed."""
                      super().selectionChanged(selected, deselected)
                      self.blockSignals(True)
                      for index in selected.indexes():
                          item = self.itemFromIndex(index)
                          # did not work at PySide6.3.1
                          item.setChecked(True)
                          # item.setData(0, Qt.CheckStateRole, Qt.Checked)
                      for index in deselected.indexes():
                          item = self.itemFromIndex(index)
                          # did not work at PySide6.3.1
                          item.setChecked(False)
                          # item.setData(0, Qt.CheckStateRole, Qt.Unchecked)
                      self.blockSignals(False)
              
              
              class MainWindow(QMainWindow):
                  def __init__(self):
                      super().__init__()
                      self.setWindowTitle('TREEWIDGET_DEMO')
              
                      # self.setMinimumSize(400, 460)
                      main_layout = QVBoxLayout()
              
                      label = QLabel("Demo")
                      label.setAlignment(Qt.AlignCenter)
                      main_layout.addWidget(label)
              
                      data = [
                          "Demo 01",
                          "Demo 02",
                          "Demo 03",
                          "Demo 04",
                          "Demo 05",
                          "Demo 06",
                      ]
                      self.my_list_tree = MyTreeWidget(data)
                      self.my_list_tree.setFixedHeight(200)
                      main_layout.addWidget(self.my_list_tree, 1)
              
                      widget = QWidget()
                      widget.setLayout(main_layout)
                      self.setCentralWidget(widget)
              
              
              app = QApplication(sys.argv)
              window = MainWindow()
              window.show()
              app.exec()
              

              The selectionChanged method has different behavior between PySide6.2.3 and 6.3.1.

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

              @alonginhh
              Not sure, but I think the bug report suggests converting Qt.Checked/Unchecked to int() to make it work for now?

              A 1 Reply Last reply
              2
              • JonBJ JonB

                @alonginhh
                Not sure, but I think the bug report suggests converting Qt.Checked/Unchecked to int() to make it work for now?

                A Offline
                A Offline
                alonginhh
                wrote on last edited by alonginhh
                #6

                @JonB Yes, you are right. converting Qt.Checked/Unchecked to int() to make it work in PySide6.3.1. Thank you @SGaist @CristianMaureira @JonB !! Problem solved.

                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