我的出发点是以下QML源代码,其中GridView显示了具有项目交换的漂亮动画的ListModel:
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()
}
}
}
}
}
}
而不是QML ListModel我想使用从QStandardItemModel派生的C ++模型:
#include <QStandardItemModel>
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);
};
此模型通过qmlRegisterType<MyTable,1>("MyTable",1,0,"MyTable")
向QML公开。
问题是,我没有得到漂亮的动画效果,我也不确定交换后模型是否正确更新。
相关(有问题的)C ++函数在这里:
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<inx2) {
for (int i=inx1; i<inx2; i++) table[i] = table[i+1];
table[inx2] = tmp;
} else if (inx2<inx1) {
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);
}
}
答案 0 :(得分:2)
我找到了解决方案。这是工作代码(感谢Borut123的建议)。
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<inx2) {
for (int i=inx1; i<inx2; i++) {
QStandardItem* tmp2 = takeItem(i+1);
setItem(i,tmp2);
}
} else if (inx2<inx1) {
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();
}
}