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. Cannot click on check box in QTreeWidget when QComboBox is added

Cannot click on check box in QTreeWidget when QComboBox is added

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.6k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    fantaz
    wrote on last edited by
    #1

    Hi,
    When I set QTreeWidgetItem as

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

    and add QComboBox to that item via

    treeWidget->setItemWidget(item, 0,cmb);
    

    I cannot uncheck or check the combo box.
    What is going on?

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

      Hi,

      setItemWidget will put the widget in the cell over the model data and the item. You should have set autoFileBackground on cmb to true to avoid that visual situation.

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

        Setting the autoFillBackground property to true does change the visual representation, but it does not solve my problem. I still cannot check or uncheck the checkbox.
        Here is the code I've been working on. Maybe I'm doing something else wrong...

        QTreeWidget* treeWidget = ui->treeWidget;
        QTreeWidgetItem* myItem = new QTreeWidgetItem(treeWidget, QStringList("MyItem"));
        // create a regular subitem with checkbox only
        QTreeWidgetItem* subItem = new QTreeWidgetItem(myItem, QStringList("SubItem"));
        subItem->setFlags(subItem->flags() | Qt::ItemIsUserCheckable);
        subItem->setCheckState(0, Qt::Checked);
        myItem->addChild(subItem);
        
        // create the combo box
        QComboBox* combo = new QComboBox(treeWidget);
        combo->addItem("Option 1");
        combo->addItem("Option 2");
        combo->setAutoFillBackground(true); // this one does not have any effect
        // add another subitem which will have checkbox AND combo box
        QTreeWidgetItem* subItem2 = new QTreeWidgetItem(myItem, QStringList(""));
        subItem2->setFlags(subItem2->flags() | Qt::ItemIsUserCheckable);
        subItem2->setCheckState(0, Qt::Checked);
        treeWidget->setItemWidget(subItem2, 0, combo);
        myItem->addChild(subItem2);
        
        treeWidget->addTopLevelItem(myItem);
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The point of setItemWidget is that you "overwrite" content of the cell i.e. you want the widget to show in place of the content of the widget. If you want to have both a check box and a combo box you should consider writing a QStyledItemDelegate.

          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
          1
          • D Offline
            D Offline
            Devopia53
            wrote on last edited by
            #5

            According to the Qt document(setItemWidget):

            "This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead."

            Also, read the following article.

            Can't change state of checkable QListViewItem with custom widget

            F 1 Reply Last reply
            3
            • D Devopia53

              According to the Qt document(setItemWidget):

              "This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead."

              Also, read the following article.

              Can't change state of checkable QListViewItem with custom widget

              F Offline
              F Offline
              fantaz
              wrote on last edited by
              #6

              @Devopia53 Thanks for the link. It did clarify why it doesn't work.

              D 1 Reply Last reply
              0
              • F fantaz

                @Devopia53 Thanks for the link. It did clarify why it doesn't work.

                D Offline
                D Offline
                Devopia53
                wrote on last edited by
                #7

                @fantaz Welcome!

                I have a little tricky solution:

                 connect(ui->treeWidget, &QTreeWidget::itemClicked,
                            [&](QTreeWidgetItem *item, int column){
                                    if (ui->treeWidget->itemWidget(item, column))
                                        item->setCheckState(column, (item->checkState(column) == Qt::Checked) ? Qt::Unchecked : Qt::Checked); });
                

                I hope that you see.

                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