qt - QAbstractListModel insertRows 无法正常工作

标签 qt qml qabstractlistmodel

我有一个 QAbstractListModel 的子类,并用 GridView 附加了这个模型子类。当我从我的子类中删除行时,GridView 会更新,但是当我在模型中插入行时,GridView 不会更新。我已经实现了removeR

我有一个非常简单的 QAbstractListModel 子类,并附加了一个 QML GridView。我在 GUI 上有两个按钮添加/删除。按 Add 将一个字符串添加到模型和 gui 更新,按 remove 从模型和 gui 更新中删除最后一个字符串。我的问题是当我只添加一行时,当我删除时,GUI 上什么也没有发生。给出了我的模型子类和 QML 文件的实现。

名称模型.cpp

NamesModel::NamesModel(QObject *parent) :
    QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;
    roles[Qt::UserRole + 1]  = "name";
    setRoleNames(roles);
}
int NamesModel::rowCount(const QModelIndex &parent) const
{
    return this->namesList.count();
}
QVariant NamesModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    switch(role) {
    case (Qt::UserRole + 1):
        return this->namesList.at(index.row());
    default:
        return QVariant();
    }
    return QVariant();
}
bool NamesModel::insertRow(int row, const QModelIndex &parent)
{
    if ( parent.isValid() )
        return false;
    qDebug() << "Row = " << row;
    beginInsertRows(parent, row, row);
    this->namesList.insert(row, ("Hello World " + QString("%1").arg(row)));
    endInsertRows();
    return true;
}
bool NamesModel::insertRows(int row, int count, const QModelIndex &parent)
{
    int existing_count = this->namesList.count();
    beginInsertRows(parent, row, count);
    for ( int i = existing_count; i < (existing_count + count); i++ )
        this->namesList.insert(i, QString("Multi Hello World = %1").arg(i));
    endInsertRows();
    return true;
}
bool NamesModel::removeRows(int row, int count, const QModelIndex &parent)
{
    int existing_count = this->namesList.count();
    beginRemoveRows(parent, row, count);
    for ( int i = (existing_count + count); i < existing_count; i-- )
        this->namesList.removeAt(i);
    endRemoveRows();
    return true;
}

bool NamesModel::removeRow(int row, const QModelIndex &parent)
{
     beginRemoveRows(parent, row, row);
     this->namesList.removeAt(row);
     endRemoveRows();
}

void NamesModel::addName()
{
    int index = this->namesList.count();
    //insertRow(index, QModelIndex());
    insertRows(index, 5, QModelIndex());
    emit layoutChanged();
    emit dataChanged(this->index(index), this->index(this->namesList.count()));
}
void NamesModel::removeName()
{
    int index = this->namesList.count();
    //removeRow(index, QModelIndex()/*this->index(this->namesList.count())*/);
    removeRows(index, 5, QModelIndex());
    emit layoutChanged();
    emit dataChanged(this->index(index), this->index(index + 5));
}

主文件
Rectangle {
    width: 360
    height: 360
    Text {
        height: 20
        width: 360/2;
        text: "Add Name"
        MouseArea {
            anchors.fill: parent
            onClicked: {
                names.addName();
                console.log("Add Name");
            }
        }
    }
    Text {
        height: 20;
        width: 360/2;
        x: 360/2
        text: "Remove Name";
        MouseArea {
            anchors.fill: parent
            onClicked: {
                names.removeName();
                console.log("Remove Name");
            }
        }
    }

    Component {
        id: gridDelegate

        Text {
            width: 19;      // These are the dimensions of icon image.
            height: 16
            font.pixelSize: 12
            text: name;
        }
    }
    GridView {
        y: 21
        boundsBehavior: Flickable.StopAtBounds

        focus: true
        model: names;
        delegate: gridDelegate
    }
}

我正在调用 beginInsertRows/endInsertRows 和 beginRemoveRows/endRemoveRows 但它似乎不起作用。正如其他类似线程所建议的那样,我还在开始/结束 InserRows 之后调用了 layoutChanged/dataChanged,但没有效果。

实际上,我的实际项目几乎遇到了同样的问题。我创建了这个应用程序来测试这个 beginInsertRows 等有什么问题。

任何帮助表示赞赏。

问候
法鲁克·阿尔沙德。

最佳答案

您打电话的方式 beginInsertRows是不正确的。在该函数的文档中,您需要提供新项目(行)的第一个索引和所插入项目的最后一个"new"索引(行 + 计数 - 1)。

bool NamesModel::insertRows(int row, int count, const QModelIndex &parent)
{
    int existing_count = this->namesList.count();
    beginInsertRows(parent, row, row + count - 1);
    for ( int i = row; i < (row + count - 1); i++ )
        this->namesList.insert(i, QString("Multi Hello World = %1").arg(i));
    endInsertRows();
    return true;
}

beginRemoveRows 也是如此.
bool NamesModel::removeRows(int row, int count, const QModelIndex &parent)
{
    int existing_count = this->namesList.count();
    beginRemoveRows(parent, row, row + count - 1);
    for ( int i = (row + count - 1); i <= row; i-- )
        this->namesList.removeAt(i);
    endRemoveRows();
    return true;
}

关于qt - QAbstractListModel insertRows 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12394751/

相关文章:

c++ - Qt-Qml : Binding checkbox to QAbstractListModel

python - 通过 Qt Designer 创建的对话框断开 QGis 插件的信号

c++ - 如何从Qt5中的QVariant获取QString形式的数据?

c++ - 如何从文本编辑QT中获取整个文本

c++ - 使用 C++ 访问 Qt/QML 对象

c++ - QML QTQuick ChartView 将指针传递给 C++

python - 无法在场景中正确定位 QGraphicsRectItem

c++ - 窗口未在 Qt 中呈现

qt - QAbstractListModel和QList适配器

python - PyQT ListView 不响应数据更改信号