문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

You can add multiple columns at once to a parent for the same child node like this:

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

     QList<QStandardItem*> childColumns;
     childColumns<< childKey << childValue;

     parent->appendRow(childColumns);

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top