PDA

View Full Version : adding elements to QTreeWidget column 2



mastupristi
25th May 2011, 12:01
I have a 2 columns QTreeWidget.
Only first column is populated. How can I populate the second column?

thanks

Santosh Reddy
25th May 2011, 14:02
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.

Tree widget items are used to hold rows of information for tree widgets. Rows usually contain several columns of data, each of which can contain a text label and an icon.

The QTreeWidgetItem class is a convenience class that replaces the QListViewItem class in Qt 3. It provides an item for use with the QTreeWidget class.

Items are usually constructed with a parent that is either a QTreeWidget (for top-level items) or a QTreeWidgetItem (for items on lower levels of the tree).

For example, the following code constructs a top-level item to represent cities of the world, and adds a entry for Oslo as a child item:



QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
cities->setText(0, tr("Cities"));
QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
osloItem->setText(0, tr("Oslo")); //Column 1
osloItem->setText(1, tr("Yes")); //Column 2

mastupristi
25th May 2011, 14:36
For example, the following code constructs a top-level item to represent cities of the world, and adds a entry for Oslo as a child item:



QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
cities->setText(0, tr("Cities"));
QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
osloItem->setText(0, tr("Oslo")); //Column 1
osloItem->setText(1, tr("Yes")); //Column 2

Ok thanks,
now suppose to have column 3 not populated, and you want to populate column 3 for "Oslo".
how can you do?

thanks

Santosh Reddy
25th May 2011, 15:31
a QTreeWidgetItem represents the a complete row, it will have all the columns in it, you just can select the column of your interest, no need create them. This is a convenience class.

Do you mean to have custom Widget for Column 3, then you need to subclass QTreeWidgetItem.


When subclassing QTreeWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.

If want ot have custom widget in column 3, then sub-class QTreeWidgetItem


When subclassing QTreeWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.