C++ 将行添加到自定义C+;时未更新QML ListView+;模型

C++ 将行添加到自定义C+;时未更新QML ListView+;模型,c++,qt,qml,qt5.8,C++,Qt,Qml,Qt5.8,我为频道中存在的用户列表实现了自己的模型。如果填充了模型并读取了数据,则效果很好,但不起作用的是实时更新,如果添加了新内容,则仅在调整大小事件后才会显示 bool UserModel::insertUser( QString channelId, QSharedPointer<RocketChatUser> user ) { auto values = userMap.values( channelId ); int index = values.count();

我为频道中存在的用户列表实现了自己的模型。如果填充了模型并读取了数据,则效果很好,但不起作用的是实时更新,如果添加了新内容,则仅在调整大小事件后才会显示

bool UserModel::insertUser( QString channelId, QSharedPointer<RocketChatUser> user )
{
    auto values =  userMap.values( channelId );
    int index = values.count();
    auto qIndex = QModelIndex();
    if ( channelId == current ) {
        beginInsertRows( qIndex, index, index );
    }

    userMap.insert( channelId, user );

    if ( channelId == current ) {
        endInsertRows();
    }
}


class UserModel: public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QString currentChannel READ getCurrent WRITE setCurrent NOTIFY currentChannelChanged)
    enum UserRoles {
          UserName = Qt::UserRole + 1,
          UserId
      };
public:
    UserModel( QObject *parent = 0 );
    int rowCount( const QModelIndex &parent = QModelIndex() ) const;
    QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
    bool insertUser(QString,QSharedPointer<RocketChatUser>);
    QString getCurrent() const;
    void setCurrent(const QString &value);
    void onCurrentChannelChanged(const QString &newText);

protected:
    QHash<int, QByteArray> roleNames() const;
    QSet<QString> duplicateCheck;
    QMultiMap<QString, QSharedPointer<RocketChatUser>> userMap;
    QString current;
signals:
    void currentChannelChanged(const QString &newText);
};
QML是这样的:

Drawer {
    z: 9
    width: Math.min(window.width, window.height) / 3 * 2
    height: window.height
    edge: Qt.LeftEdge
    ListView {
        anchors.fill: parent
        spacing: 1
        header: ToolBar {
            Text {
                text: qsTr("Users in channel")
                anchors.centerIn: parent
            }
            width: parent.width - 1
        }
        id: userListView
        model: userModel
        delegate: RowLayout {
        width: parent.width
        Button {
            anchors.fill: parent
            text: model.username
        }
        /*BusyIndicator{
                    id: loadingUserIndicator
                                anchors.centerIn: parent
                                        }*/
        }
        Component.onCompleted: {
            userModel.currentChannel = channelView.currentChannel
        }
    }
}

在这几行代码中唯一需要注意的是,如果您编辑备份数据结构,则应始终调用
*InsertRows
,如文档中所述。您的
userModel
不是局部变量,对吗?还是在你的主楼里?因为我想你知道如果超出范围会发生什么。我猜您的
rowCount
工作正常吗?一个问题可能是您在视图末尾发出“添加行”的信号,但不一定要在末尾添加数据。您可以尝试发出正确的插入位置
Drawer {
    z: 9
    width: Math.min(window.width, window.height) / 3 * 2
    height: window.height
    edge: Qt.LeftEdge
    ListView {
        anchors.fill: parent
        spacing: 1
        header: ToolBar {
            Text {
                text: qsTr("Users in channel")
                anchors.centerIn: parent
            }
            width: parent.width - 1
        }
        id: userListView
        model: userModel
        delegate: RowLayout {
        width: parent.width
        Button {
            anchors.fill: parent
            text: model.username
        }
        /*BusyIndicator{
                    id: loadingUserIndicator
                                anchors.centerIn: parent
                                        }*/
        }
        Component.onCompleted: {
            userModel.currentChannel = channelView.currentChannel
        }
    }
}