qml 不刷新 放大还原,QML:GridView在C ++中更改其模型后不更新

My starting point is the following QML source, where GridView is showing ListModel with nice animation of item swaps:

import QtQuick 1.1

GridView {

id: mainGrid

width: 825; height: 400; cellWidth: 165; cellHeight: 95

model: myModel

delegate: myButton

ListModel {

id: myModel

function createModel() {

for (var i=1; i<=20; i++) {

append({"display":i})

}

}

function listItems() {

console.log("LIST OF ITEMS")

for (var i=0; i<=19; i++) {

console.log("model["+i+"]="+get(i).display)

}

}

function moveItem(inx1,inx2) {

move(inx1,inx2,1)

}

Component.onCompleted: {

createModel()

listItems()

}

}

Component {

id: myButton

Item {

id: item

width: 160; height: 90

Rectangle {

id: box

parent: mainGrid

x: item.x; y: item.y; width: item.width; height: item.height; border.width: 1

Behavior on x {NumberAnimation { duration: 1000;} }

Behavior on y {NumberAnimation { duration: 1000;} }

Text {

anchors.centerIn: parent

text: display

}

MouseArea {

anchors.fill: parent

onClicked: {

mainGrid.model.moveItem(index,0)

mainGrid.model.listItems()

}

}

}

}

}

}

Instead of QML ListModel I want to use a C++ model which is derived from QStandardItemModel:

#include

class MyTable : public QStandardItemModel

{

private:

Q_OBJECT

int table[20];

public:

explicit MyTable(QObject *parent = 0) : QStandardItemModel(parent) {}

Q_INVOKABLE void createModel();

Q_INVOKABLE void moveItem(int inx1, int inx2);

};

This model is exposed to QML via qmlRegisterType("MyTable",1,0,"MyTable").

The problem is, that I do not get the nice animation effect and I am also not sure if after the swap the model is correctly updated.

The related (problematic) C++ functions are here:

void MyTable::createModel()

{

for (unsigned int i=0; i<20; i++) {

table[i]=i;

QStandardItem* item = new QStandardItem(QString("%1").arg(table[i]));

appendRow(item);

}

}

void MyTable::moveItem(int inx1, int inx2) {

//SOME MATH, IT WORKS

int tmp = table[inx1];

if (inx1

for (int i=inx1; i

table[inx2] = tmp;

} else if (inx2

for (int i=inx1; i>inx2; i--) table[i] = table[i-1];

table[inx2] = tmp;

}

//UPDATE MODEL - IS SOMETHING MISSING HERE?

for (unsigned int i=0; i<20; i++) {

QStandardItem* item = new QStandardItem(QString("%1").arg(table[i]));

setItem(i,0,item);

}

}

解决方案

I found the solution. Here is the working code (thanks Borut123 for suggestions).

class MyTable : public QStandardItemModel

{

private:

Q_OBJECT

public:

explicit MyTable(QObject *parent = 0) : QStandardItemModel(parent) {}

Q_INVOKABLE void createModel();

Q_INVOKABLE void moveItem(int inx1, int inx2);

Q_INVOKABLE void listItems();

};

void MyTable::createModel()

{

for (unsigned int i=0; i<20; i++) {

QStandardItem* item = new QStandardItem(QString("%1").arg(i));

appendRow(item);

}

}

void MyTable::moveItem(int inx1, int inx2) {

bool ok = false;

if (inx1 < inx2) ok = beginMoveRows(QModelIndex(),inx1,inx1,QModelIndex(),inx2+1);

if (inx1 > inx2) ok = beginMoveRows(QModelIndex(),inx1,inx1,QModelIndex(),inx2);

if (!ok) return;

QStandardItem* tmp1 = takeItem(inx1);

if (inx1

for (int i=inx1; i

QStandardItem* tmp2 = takeItem(i+1);

setItem(i,tmp2);

}

} else if (inx2

for (int i=inx1; i>inx2; i--) {

QStandardItem* tmp2 = takeItem(i-1);

setItem(i,tmp2);

}

}

setItem(inx2,tmp1);

endMoveRows();

}

void MyTable::listItems() {

qDebug() << "LIST OF ITEMS";

for (unsigned int i=0; i<20; i++) {

qDebug() << QString("table[%1]=%2").arg(i).arg(item(i)->text()).toAscii().data();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值