Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Configuring checkbox when using QTreeWidget
QtWS25 Last Chance

Configuring checkbox when using QTreeWidget

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5qtreewidgetcheckbox
4 Posts 3 Posters 11.2k 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.
  • MATTKM Offline
    MATTKM Offline
    MATTK
    wrote on last edited by
    #1

    Hello, I was trying to make something like this using PyQt 5.11.2 and Qt Designer.
    Example

    So I marked the "UserCheckable" flag to expect a checkbox next to items "XXX", "YYY", and "ZZZ".

    0_1531334692211_Checkable.png

    However, the checkboxes don't appear.
    Is it not possible to configure the items to have checkboxes with Qt Designer?
    Do I have to type the code "ItemIsUserCheckable" manually?

    -Regards

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

      Hi,

      After a quick test, it seems to you have to use setCheckState(Qt::Unchecked) or setCheckState(Qt::Checked) if you want to have the checkbox appearing in your GUI.

      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
      3
      • MATTKM Offline
        MATTKM Offline
        MATTK
        wrote on last edited by
        #3

        Thanks for your reply, SGaist.
        0_1531365745457_DesignerVersion.png

        I wish to solve this using the Qt Designer.
        I'm using Qt Designer 5.9.1.
        Then, is this a bug or a known issue from Qt Designer?

        -Regards

        VRoninV 1 Reply Last reply
        0
        • MATTKM MATTK

          Thanks for your reply, SGaist.
          0_1531365745457_DesignerVersion.png

          I wish to solve this using the Qt Designer.
          I'm using Qt Designer 5.9.1.
          Then, is this a bug or a known issue from Qt Designer?

          -Regards

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @MATTK said in Configuring checkbox when using QTreeWidget:

          is this a bug or a known issue from Qt Designer?

          No, it's 100% intended behaviour. the condition is checked here

          You have 2 options:

          • set the Qt::CheckStateRole for the indexes you want to have a checkbox. In your widget constructor you'd call something like setUnchecked(ui->treeWidget->model());
          void setUnchecked(QAbstractItemModel* model, const QModelIndex& parent = QModelIndex()){
              if(!model)
                  return;
              for(int i=0, maxRow=model->rowCount(parent);i<maxRow;++i){
                  for(int j=0, maxCol=model->columnCount(parent);j<maxCol;++j){
                      const QModelIndex currIdx = model->index(i,j,parent);
                      model->setData(currIdx,Qt::Unchecked,Qt::CheckStateRole);
                      if(model->hasChildren(currIdx))
                          setUnchecked(model,currIdx);
                  }
              }
          }
          
          • subclass the delegate to check the flag instead of Qt::CheckStateRole and call something like ui->treeWidget->setItemDelegate(new CheckFlagDelegate(this)); in your widget constructor
          class CheckFlagDelegate : public QStyledItemDelegate{
              Q_OBJECT
              Q_DISABLE_COPY(CheckFlagDelegate)
          public:
              explicit CheckFlagDelegate(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){}
          protected:
              void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const Q_DECL_OVERRIDE{
                  QStyledItemDelegate::initStyleOption(option,index);
                  if(index.model()->flags(index) & Qt::ItemIsUserCheckable)
                      option->features |= QStyleOptionViewItem::HasCheckIndicator;
              }
          };
          

          Since the functionality is defined in a QObject (the delegate) and not in a QWidget this is not something designer can and probably ever will manage

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          • Login

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