1
我有一個TreeView
在Qt Quick
和一個類子類QStandardItemModel
。 this.appendRow()
在模型的構造函數中工作得很好。QStandardItemModel appendRow在構造函數被調用後不起作用
但是,如果我在構造函數之後調用它,例如作爲按鈕按下的反應,它什麼也不做。
(同時檢查this->rowCount()
以查看它是否僅顯示,但rowCount不增加)。
我正在使用addRootEntry函數,您可以在下面看到將QStandardItem
添加到根目錄。
void ProjectTreeModel::addRootEntry(const QString& name, const QString& type, const QString& icon)
{
QStandardItem rootEntry = new QStandardItem(name);
rootEntry->setData(icon, ProjectTreeModel_Role_Icon);
rootEntry->setData(type, ProjectTreeModel_Role_Type);
rootEntry->setData(name, ProjectTreeModel_Role_Name);
this->appendRow(rootEntry);
qDebug() << rootEntry; //Is not null
qDebug() << this->rowCount(); //Stays the same
}
不,不幸的是,這是行不通的。項目現在添加,但TreeView不更新。 –