【问题标题】:QStandardItemModel appendRow doesn't work after constructor was called调用构造函数后,QStandardItemModel appendRow 不起作用
【发布时间】:2016-08-17 03:39:04
【问题描述】:

我在Qt Quick 中有一个TreeView 和一个子类QStandardItemModelthis.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
}

【问题讨论】:

    标签: c++ qt user-interface qt5


    【解决方案1】:

    在你的 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,因为它通常不是必需的。

    【讨论】:

    • 不,很遗憾,这不起作用。项目现已添加,但 TreeView 未更新。
    猜你喜欢
    • 2012-07-30
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2018-07-17
    相关资源
    最近更新 更多