简体   繁体   中英

Disable a column of a QTreeWidgetItem or remove set CheckBox

I have QTreeWidgetItem set in a QTreeWidget with 2 columns. Both cells got a CheckBox set by setCheckState(...) .

When the user unchecks the CheckBox in my first column I uncheck the second CheckBox in column 2.

Now, I would like to prevent the user to check this second CheckBox again. Is it possible to remove this CheckBox in column 2 or to disable only this cell? So far I have just seen that all the flags work on the complete item and a set CheckBox won't disapear.

Btw. The items are not editable and I don't want to use a QTableWidget / -Item .

Update:

The CheckBox will be automatically inserted by Qt when I call setCheckState for the item:

QTreeWidgetItem *item = new QTreeWidgetItem(ui.TreeWidget);
item->setCheckState(0, Qt::Checked);

After the new the item does not own a CheckBox (by Qt default). Calling setCheckState(...) I automatically insert a CheckBox (here in column 0) with the Qt::CheckState I want.

But after I have done it there's no way to remove the CheckBox - so it seems.

Maybe anyone got a solution how I can get rid of this CheckBox at a later time? Any help is much appreciated!

This will do it:

item->setData(0, Qt::CheckStateRole, QVariant());           //No checkbox at all (what you wanted)

any of these others will show the checkbox space

item->setData(0, Qt::CheckStateRole, Qt::Unchecked);        //Unchecked checkbox
item->setData(0, Qt::CheckStateRole, Qt::Checked);          //Checked checkbox
item->setData(0, Qt::CheckStateRole, Qt::PartiallyChecked); //Partially checked checkbox (gray)

The setCheckState method can not set 'no check state', only Checked, PartiallyChecked or Unckecked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM