add checkbox to qtreewidgetitem
12,255
Solution 1
The ItemIsUserCheckable flag is already set by default in QTreeWidgetItem, so that's not the issue.
All you need is to do
item->setCheckState(Qt::Unchecked);
and you should see a checkbox.
Solution 2
Try to reorganize your code:
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
item->setCheckState(Qt::Checked);
m_eventList->addTopLevelItem(item);
Another method would be to write your own model and overwrite the flags() method. In this method, you return
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.column() == 0)
{
flags |= Qt::ItemIsUserCheckable;
}
return flags;
Solution 3
Qt::ItemIsUserCheckable
and Qt::ItemIsSelectable
are default setting for QTreeWidget.
item->setCheckState(column, Qt::Unchecked)
is Okay.
https://doc.qt.io/qt-5/qtreewidgetitem.html#flags
Author by
mari
Updated on June 09, 2022Comments
-
mari about 2 years
i want to add a check box to my
qtreewigetitem
, i tried this code tosetflag
, then i add item is selectable for sake of maybe this will solve my problem but nothing happened, would you please help me how can i addcheck box
to my item? thank you in advancem_eventList->addTopLevelItem(new QTreeWidgetItem); QTreeWidgetItem *item = m_eventList->topLevelItem(m_eventList->topLevelItemCount()-1) item->setFlags(item->flags() | Qt::ItemIsUserCheckable |Qt::ItemIsSelectable);