QML没有从C ++属性注册属性更改

时间:2016-09-27 08:15:07

标签: c++ qt qml

我在自定义select * FROM tablename WHERE MATCH (col1,col2,...) AGAINST ('+value*' IN BOOLEAN MODE); 子类(实现Boolean search)的1. assertEquals(actual, expected); 2. try { assertEquals(actual, expected); } catch(AssertionError e) { e.printStackTrace(); } 中显示数据。添加和删​​除项目工作正常,QML会收到有关更改和转换正常工作的通知 现在我正在尝试在模型中设置Item的属性并让QML对其做出反应。问题是,QML中的GridView没有被调用。

这是C ++中的属性:

QAbstractListItem

这就是GridView的样子:

onPropertyChanged

程序启动时,// item.h Q_PROPERTY(bool pToBeDeleted READ toBeDeleted NOTIFY toBeDeletedChanged) // item.cpp void Item::requestDelete() { toBeDeleted_m = true; qDebug() << "emitting"; emit toBeDeletedChanged(); } 的{​​{1}}设置为我的itemmodel。
这是QML类型,看不到变化:

// main.qml
GridView {
    id: grid

    // ...

    model: empty
    delegate: customComponent {
        toBeDeleted: pToBeDeleted
    }
    ListModel {
        id: empty
    }
}

现在,当我从模型内部调用方法时,这样:

grid

输出显示model但不显示// customComponentForm.ui.qml Item { property bool toBeDeleted: false } // customComponent.qml CustomComponentForm { onToBeDeletedChanged: { console.debug("change") } }

我试图加入

this->items.at(i++)->requestDelete();

确实导致emitting被称为有时,但这也导致了一些错误的行为

change

1 个答案:

答案 0 :(得分:0)

这里出了两件事。首先,因为++ at

this->items.at(i++)->requestDelete();

dataChanged emit有错误的索引导致错误的项目被更新。其次,

emit dataChanged(createIndex(i, 0), createIndex(i, 0));

错过了第三个参数,因为在另一次尝试中我尝试以错误的方式内联定义Vector,我没有立即发现这是问题。这里的正确电话会是

QVector<int> v;

v.append(Qt::UserRole + 7 + 1);
// pToBeDeleted being the 7th property, always check this with
// roleNames()[Qt::UserRole + i + 1]. It should say your property.

emit dataChanged(createIndex(i, 0), createIndex(i, 0), v);

我的错误。

但另一方面,由于角色名称索引似乎与平台有关,并且表明模型的变化在某种程度上是一种肮脏的方法,因此更好的解决方案(如Kevin Krammer所示)将重写itemmodel只包含一个属性,即QObject项。这样,QML 通知了更改项目的属性。