Qt QAbstractListModel insertRows工作不正常
我有一个子类QAbstractListModel,并用GridView附加了这个模型子类。当我从子类中删除行时,GridView会更新,但当我在模型中插入行时,GridView不会更新。我已经实现了删除程序 我有一个非常简单的QAbstractListModel子类,附带了一个QML GridView。我在GUI上有两个添加/删除按钮。按Add向模型和gui更新添加字符串,按remove从模型和gui更新中删除最后一个字符串。我的问题是,当我只添加了一行,而当我删除GUI上的任何内容时。给出了我的模型子类和QML文件的实现 namesmodel.cppQt QAbstractListModel insertRows工作不正常,qt,qml,qabstractlistmodel,Qt,Qml,Qabstractlistmodel,我有一个子类QAbstractListModel,并用GridView附加了这个模型子类。当我从子类中删除行时,GridView会更新,但当我在模型中插入行时,GridView不会更新。我已经实现了删除程序 我有一个非常简单的QAbstractListModel子类,附带了一个QML GridView。我在GUI上有两个添加/删除按钮。按Add向模型和gui更新添加字符串,按remove从模型和gui更新中删除最后一个字符串。我的问题是,当我只添加了一行,而当我删除GUI上的任何内容时。给出了我
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));
}
我正在调用beginInsertRows/endInsertRows和beginRemoveRows/endRemoveRows,但它似乎不起作用。正如在其他类似线程上所建议的那样,我也在begin/end InserRows之后调用了LAYOUTCHENGED/DATACHENGED,但没有效果
事实上,我在实际项目中遇到了几乎相同的问题。我创建此应用程序是为了测试此beginInsertRows等的错误
感谢您的帮助
问候
法鲁克·阿尔沙德。你打电话的方式不正确。在函数的文档中,需要提供新项(行)的第一个索引和插入项的最后一个“新”索引(行+计数-1)
bool NamesModel::insertRows(int行、int计数、常量QModelIndex&parent)
{
int existing_count=this->namesList.count();
beginInsertRows(父、行、行+计数-1);
对于(int i=行;i<(行+计数-1);i++)
此->名称列表.insert(i,QString(“Multi Hello World=%1”).arg(i));
endInsertRows();
返回true;
}
同样的情况也会发生
bool NamesModel::removows(int行、int计数、常量QModelIndex&parent)
{
int existing_count=this->namesList.count();
beginRemoveRows(父级、行、行+计数-1);
for(inti=(行+计数-1);i namesList.removeAt(i);
EndRemoveTows();
返回true;
}
Hi Keith。我尝试了这个方法,但结果相同。我只能添加一行。在addName()函数中,您不需要发出layoutChanged()和dataChanged()信号,因为您已经通知视图已插入行。我假设您使用的是Qt 4.7或4.8,对吗?for(int I=row;I<(row+count-1);I++)不正确。当count==1时,将不添加任何内容。什么是“names”实际QStringList?
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
}
}
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;
}
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;
}