简体   繁体   English

QML,如何从C ++动态更改Repeater的项目

[英]QML,How to dynamicaly change Item of Repeater from C++

I have QML Repeater on Grid, when i clicked on item i emit signal which processed by C++ class, and then changed array in C++ which then assign as model of QML Repeater. 我在网格上有QML Repeater,当我单击项目时,我发出由C ++类处理的信号,然后更改了C ++中的数组,然后将其分配为QML Repeater的模型。 Is there a way to change just two elements of C++ model, not whole model as i did? 有没有办法只更改C ++模型的两个元素,而不是像我那样更改整个模型?

that's my qml file 那是我的qml文件

Grid{
height:width
rows:8
columns: 8

Repeater{
    id: chessPiecesRptr
       ...
   signal chessfigureSelected(int index)


    delegate: Item{
        id:chessPiecesItm

        Image{
            ...
        }


        MouseArea
        {
            anchors.fill:parent
            onClicked: {

                chessPiecesRptr.chessfigureSelected(index)

            }
        }

    }

}

C++ method which update model of QML Repeater 更新QML中继器模型的C ++方法

void ChessFiguresClass::changeModel()
{
    QStringList dataList;

    for(int i=0;i<64;i++)
        dataList.append(QChar(posArray[i]));

    QQmlProperty::write(chessPiecesRptr, "model",   QVariant::fromValue(dataList));
}

Contrary to the accepted answer it is indeed possible without going all the way to implement an entire QAbstractListModel . 与公认的答案相反,确实有可能无需一路实现整个QAbstractListModel

It is true that QStringList doesn't have any way to notify for changes, but all you have to do is wrap it up in a property, something like this: 的确, QStringList没有任何通知更改的方法,但是您要做的就是将其包装在一个属性中,如下所示:

Q_PROPERTY(QVariant myModel READ myModel NOTIFY myModelChanged)
...
QVariant myModel() { return QVariant::fromValue(myStringList); }
...
void myModelChanged(); // signal

And just emit myModelChanged every time you want to reflect a change in the model, be that replacing it with a different model or changing it internally. 每当您想反映模型中的更改时,只需发出myModelChanged即可,无论是将其替换为其他模型还是在内部进行更改。 And use the myModel property for the Repeater 's model. 并将myModel属性用于Repeater的模型。

However, if you have a lot of items in the model or the delegates are complex, it is always a good idea to implement your own QAbstractListModel , because with the approach above, the repeater will recreate the entire model every time it "changes", whereas the QAbstractListModel will only update the actual changes. 但是,如果模型中有很多项目或委托很复杂,那么实现自己的QAbstractListModel总是一个好主意,因为采用上述方法,转发器每次“更改”时都会重新创建整个模型,而QAbstractListModel将仅更新实际更改。

I'm afraid it is not possible. 恐怕是不可能的。 QList (and QStringList) does not have inner mechanisms to notify Qml items about its changes. QList(和QStringList)没有内部机制来通知Qml项其更改。 Only when model property from QML item is changed, the whole list is read again. 仅当更改了QML项目中的模型属性时,才会再次读取整个列表。 I had faced the same problem before and I implemented a string list using QAbstractListModel as base class. 我之前也遇到过同样的问题,我使用QAbstractListModel作为基类实现了一个字符串列表。 The header looks like this: 标头看起来像这样:

#ifndef _SIMPLEMODEL_H_
#define _SIMPLEMODEL_H_

#include <QtCore>



class SimpleStringModel : public QAbstractListModel
{
    Q_PROPERTY(int count READ count NOTIFY countChanged)
    Q_DISABLE_COPY(SimpleStringModel)
    Q_OBJECT
public:
    explicit SimpleStringModel(QObject* parent = 0);
    SimpleStringModel(const QList<QString>& initList, QObject* parent = 0);
    virtual ~SimpleStringModel();

private:
    enum Roles{
        ModelDataRole = Qt::UserRole+1
    };

public:
    int count() const;

public:

    void append(const QString& item);
    void insert(int index, const QString& item);
    void append(const QList<QString>& items);
    void insert(int index, const QList<QString>& items);
    bool removeOne(const QString& item);
    int removeAll(const QString& item);
    void removeAt(int index);
    QList<QString> list() const;

signals:
    void countChanged();

    // QAbstractItemModel interface
public:
    virtual int rowCount(const QModelIndex &parent) const;
    virtual QVariant data(const QModelIndex &index, int role) const;
    virtual QHash<int, QByteArray> roleNames() const;

private:
    QList<QString> m_data;
};


#endif //_SIMPLEMODEL_H_

You can get all the code here . 您可以在此处获取所有代码。 I hope this help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM