PDA

View Full Version : QTreeWidget select item



^NyAw^
25th October 2007, 16:39
Hi,

I'm porting my application from Qt 4.1 to 4.3.

I have a QTreeWidget that selects items depending on internal data. In Qt 4.1 I use "QTreeWidget ::setItemSelected", but now this method is obsolete and I don't realy know how to get it works. I readed that I have to use "QTreeWidgetItem::setItemSelected" instead. My QTreeWidget is configured as "SingleSelection" mode selection and "SelectItems" as behavior selection.

I'm right if I say that I have to use "QTreeWidget::setCurrentItem(item)" and "QTreeWidgetItem::setSelected(true)" to the same item?

Thanks,

wysota
25th October 2007, 18:27
the selected item may be different than the current item, so it's really up to you to decide if you want the selected item to be current or not.

^NyAw^
25th October 2007, 19:18
Hi,



the selected item may be different than the current item, so it's really up to you to decide if you want the selected item to be current or not.


Yes, I want.

Thanks, but I have another question:
I have a root item, I create a child and I insert a QComboBox into column 1 of this child. I've connected the "QComboBox::currentIndexChanged(int)" to a SLOT. When the SLOT is called I want to get the item that is in the column 0.



...
pqItem = new QTreeWidgetItem(pqRoot);
pqComboBox = new QComboBox();
m_pqTree->setItemWidget(pqItem ,1,pqComboBox);
connect(pqComboBox,SIGNAL(currentIndexChanged(int) ),this,SLOT(setMode(int)));
...



void myClass::setMode(int)
{
//Here I want to get the QTreeWidgetItem*. I have tested QObject::sender()->parent(), but I don't really know if it is its parent.
}


The problem is that when I click on the QComboBox, the QTreeWidgetItem* is not selected and I don't want to force first click the item an then let change the combo.

Thanks,

wysota
25th October 2007, 22:36
The item has nothing to do with the combobox. You can save an association between the index and the widget, but I don't see what you need it for? Could you explain what you are trying to obtain?

^NyAw^
26th October 2007, 00:25
Hi,

What I want is like an item that in first column has the name and an icon representing its state(green=enabled, red=disabled). The combo is inserted in the second column of the item, so whe the user change the combo(enabled or disabled are the possibilities), the icon of the item is changed.

I have solved by a different way that is working well for me.
I have internal data that is represented by the tree. When I change one combo to disabled or enabled, I change the internal data(is a object list) and I do a "for" getting the "state" variable and depending on the position of the list I change the corresponding item.

Thanks for replying,

wysota
26th October 2007, 00:44
A strange requirement... but if you insist this is what you want, I'd provide a custom delegate that would return a combobox as an editor (see QAbstractItemDelegate::createEditor). Then either the model should react to setData in that column and/or you should also reimplement setModelData() in the delegate to set the icon in the appropriate column. Or even make the delegate display icon in column 1 based on the contents of another column.

Your solution seems a bit crude and slow, but if it does what you want...

^NyAw^
26th October 2007, 10:12
Hi,

Thanks.

The problem is that I don't have a model. I have readed about view/model programming but the problem is that I did wrote my code pure C++, creating LinkedLists manually, ... Then I decided wich toolkit to use and mixed the code. By this I use QTreeWidget instead of QTreeView, because I don't know how to convert my code to a model.

I know that this lines of code are slow but it is not important.

The problem of using a model/view interface is that I really don't know how to rewrite the code struct to use it.

Thanks your help,

wysota
26th October 2007, 15:26
Of course you have a model. QTreeWidget uses an internal model that uses QTreeWidgetItems, so you can call model methods like you would for real models, so the delegate approach is still valid.