PDA

View Full Version : Trouble with Qtreeview and QCheckboxes



denumbaone
24th February 2011, 04:37
I'm trying to get checkboxes into my treeview and can't quite get it to work. I've looked around and can't find anything to solve my problem. I've attached the code that I have so far, but doesn't work:


QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
cities->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
cities->setText(0, tr("Places"));

(new QTreeWidgetItem(cities))->setText(0, tr("USA Cities"));

Thank you in advance for your help

ChrisW67
24th February 2011, 04:51
Your item probably needs to have a non-null QVariant() value returned for its Qt::CheckStateRole. try setting Qt::Checked or Qt::Unchecked for Qt::CheckStateRole.

denumbaone
24th February 2011, 06:18
Thank you for your input, it definitely helped. The code that I used to get it to work was:


cities->setCheckState(0, Qt::Checked);

This was for the parent, I now have to do the same for the children.

One such child is:


(new QTreeWidgetItem(cities))->setText(0, tr("USA Cities"));

I'm not sure how to make this into a checkbox (I've checked the "Help" logs in QT for a idea but came up short when it came to children). Do you have any suggestions?

Thanks again

ChrisW67
24th February 2011, 06:32
You create the item just like the cities node and then addChild() or insertChild() them to the parent item.

denumbaone
24th February 2011, 07:41
Thanks man, you've been a HUGE help. The code that I have is:


QTreeWidgetItem *citiesChild = new QTreeWidgetItem();
cities->insertChild(0, citiesChild); //error here
citiesChild->setText(0, tr("USA Cities"));
citiesChild->setCheckState(0, Qt::Checked);

Works like a charm. Thanks again

ChrisW67
24th February 2011, 23:23
What is the "error here" if it "Works like a charm"?