add checkbox to qtreewidgetitem

mari picture mari · Apr 16, 2014 · Viewed 11.2k times · Source

i want to add a check box to my qtreewigetitem, i tried this code to setflag, 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 add check box to my item? thank you in advance

m_eventList->addTopLevelItem(new QTreeWidgetItem);
       QTreeWidgetItem *item = m_eventList->topLevelItem(m_eventList->topLevelItemCount()-1)

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

Answer

David Faure picture David Faure · Sep 4, 2015

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.