2016-04-23 240 views
1

我有一個TreeViewQt Quick和一個類子類QStandardItemModelthis.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 
} 

回答

0

addRootEntry功能試試這個:

QStandardItem *rootEntry = new QStandardItem(name); 

rootEntry->setData(icon, ProjectTreeModel_Role_Icon); 
rootEntry->setData(type, ProjectTreeModel_Role_Type); 
rootEntry->setData(name, ProjectTreeModel_Role_Name); 

QStandardItem *parentItem = invisibleRootItem(); 
parentItem->appendRow(rootEntry); 

確保 ProjectTreeModel_Role_Name設置爲Qt::DisplayRole

確保您在TreeView上設置了model屬性。

考慮不要繼承QStandardItemModel,因爲它通常不是必需的。

+0

不,不幸的是,這是行不通的。項目現在添加,但TreeView不更新。 –