QAbstractListModel insertRows无法正常工作

时间:2012-09-12 19:11:04

标签: qt qml qabstractlistmodel

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

我有一个非常简单的QAbstractListModel子类,我附加了一个QML GridView。我在GUI上有两个按钮添加/删除。按Add添加一个字符串到模型并更新gui,按remove删除模型中的最后一个字符串和gui更新。我的问题是当我添加只插入一行时,当我删除GUI时没有任何反应。给定是我的模型子类和QML文件的实现。

namesmodel.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));
}

main.qml

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等错误。

感谢任何帮助。

此致 Farrukh Arshad。

1 个答案:

答案 0 :(得分:6)

您拨打beginInsertRows的方式不正确。在函数的文档中,您需要提供新项目(行)的第一个索引和最后一个&#34; new&#34;正在插入的项目的索引(行+计数 - 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;
}