QTreeView multiple column, possible?

daisy picture daisy · Nov 29, 2011 · Viewed 9.7k times · Source

I'm using QStandardItemModel with a QTreeView, I wanted the left pane to show the nodes, and the right pane to show value of the node, which is column 0 and column 1 in this case.

Construction of nodes was pretty successful, but when I'm trying to place values into that model with QStandardItem::insertRow(1, XX), the item wasn't showing up at all, is there something I missed?

@update:

Since i recursively create nodes , i use:

void Widget::addNode(QStandardItem *parent, const QVariant & data)
{
     QStandardItem *childKey = ...; // left pane
     QStandardItem *childValue = ...; // right pane

     parent->appendRow (childKey);

}

I can't just use model.setItem() to append childValue , since it went to the wrong row , and QTreeView is not expanded by default, when new node is appended.

Answer

Clare Macrae picture Clare Macrae · Nov 29, 2011

Check your code to make sure that you have told the model how many columns that you want, i.e. that you have called QStandardItemModel::setColumnCount(), to tell the model about the extra columns.

Edit

Then you need to set the value of each item in each column. One way to do this is to use QStandardItemModel::setItem ( int row, int column, QStandardItem * item)

Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted.