简体   繁体   English

无法使用 QStandardItemModel 在 Qtreeview 中创建孩子

[英]Can’t make child in Qtreeview using QStandardItemModel

After reading some examples I am still missing here something.在阅读了一些示例之后,我仍然在这里缺少一些东西。 I have Qtreeview for view and QStandardItemModel for the data interface, also using QSortFilterProxyModel subclass but I don't know if its relevant.我有用于视图的 Qtreeview 和用于数据接口的 QStandardItemModel,也使用 QSortFilterProxyModel 子类,但我不知道它是否相关。 This is my logic: First I create the model with the QWidget as the parent:这是我的逻辑:首先,我创建 model 并以 QWidget 作为父级:

QStandardItemModel m_model = new QStandardItemModel(0,4,parent);
then setSourceModel(m_model)  for the widget

Set the treeview with QSortFilterProxyModel.使用 QSortFilterProxyModel 设置 treeview。 something like this:像这样的东西:

GroupProxyModel = new GroupSortFilterProxyModel;
GroupProxyModel->setDynamicSortFilter(true);
setSourceModel(createSubjectModel(parent));

ui.treeView_mainwindow->setModel(GroupProxyModel);
ui.treeView_mainwindow->setSortingEnabled(true);

Then later I fill the first row like this:然后我像这样填充第一行:

QList<QStandardItem *> items;
items.insert(0,new QStandardItem("Test 0"));
items.at(0)->setEditable(false);
m_model->insertRow(0,items);

Until now every thing working fine and I see the row with the data.到目前为止,一切正常,我看到了数据行。 But when I like to add child to the row like this:但是当我喜欢像这样将孩子添加到行中时:

QModelIndex parentQModelIndex = m_model->item(0,0)->index();
m_model->insertRows(0,1,parentQModelIndex);
m_model->insertColumns(0,1,parentQModelIndex);
QModelIndex indexB = m_model->index(0, 0, parentQModelIndex);
m_model->setData(indexB,"Child test",Qt::DisplayRole);

But I don't see the child, why?但是我没有看到孩子,为什么?

That's not how QStandardItemModel works - to add a child, call appendRow(s)/insertRow(s) on the parent QStandardItem:这不是 QStandardItemModel 的工作方式 - 要添加一个孩子,请在父 QStandardItem 上调用 appendRow(s)/insertRow(s):

QStandardItem* child = new QStandardItem( tr("Child test") );
...
QStandardItem* parentItem = m_model->item( 0, 0 );
parentItem->appendRow( child );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM