我有一个QAbstractListModel的子类,并用GridView附加了这个模型子类。当我从子类中删除行时,GridView会更新,但当我在模型中插入行时,GridView不会更新。我已经实现了removeR
我有一个非常简单的QAbstractListModel子类,并且附加了一个QML GridView。我在GUI上有两个按钮Add/Remove。按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,但它似乎不起作用。正如在其他类似线程中所建议的那样,我也在begin / end InserRows之后调用了layoutChanged / dataChanged,但没有效果。
实际上,我在实际项目中也遇到了几乎同样的问题。我创建了这个应用程序来测试这个beginInsertRows有什么问题,等等。
任何帮助都是非常感谢的。
问候法鲁克·阿尔沙德。
发布于 2012-09-13 04:01:24
您调用beginInsertRows的方式不正确。在函数的文档中,您需要提供新项的第一个索引(行)和所插入项的最后一个“新”索引(行+计数- 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;
}https://stackoverflow.com/questions/12394751
复制相似问题