C++ QStandardItemModel appendRow不';调用构造函数后无法工作

C++ QStandardItemModel appendRow不';调用构造函数后无法工作,c++,qt,user-interface,qt5,C++,Qt,User Interface,Qt5,我在Qt Quick中有一个TreeView,还有一个类子类QStandardItemModelthis.appendRow()在模型的构造函数中工作得非常好 但是,如果我在构造函数之后调用它,例如,作为对某些按钮按下的反应,它什么也不做 (还选中了this->rowCount(),查看它是否只是没有显示,但rowCount没有增加) 我正在使用下面的addRootEntry函数向根添加QStandardItem void ProjectTreeModel::addRootEntry( cons

我在
Qt Quick
中有一个
TreeView
,还有一个类子类
QStandardItemModel
this.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
}
void ProjectTreeModel::addRootEntry(常量QString&name、常量QString&type、常量QString&icon)
{
QStandardItem rootEntry=新的QStandardItem(名称);
rootEntry->setData(图标、ProjectTreeModel\u角色\u图标);
rootEntry->setData(类型,ProjectTreeModel\u角色\u类型);
rootEntry->setData(名称、ProjectTreeModel\u角色\u名称);
此->追加行(根条目);

qDebug()在
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\u Role\u Name
设置为
Qt::DisplayRole

确保在
TreeView
上设置了
model
属性


考虑不要子类化
QStandardItemModel
,因为它通常不是必需的。

在您的
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\u Role\u Name
设置为
Qt::DisplayRole

确保在
TreeView
上设置了
model
属性


考虑不要子类化
QStandardItemModel
,因为这通常不是必需的。

不,很遗憾这不起作用。现在添加了项,但树视图没有更新。不,很遗憾这不起作用。现在添加了项,但树视图没有更新。