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

Magzhan Abdibayev

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. Is there a way to change just two elements of C++ model, not whole model as i did?

that's my qml file

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

void ChessFiguresClass::changeModel()
{
    QStringList dataList;

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

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

I'm afraid it is not possible. QList (and QStringList) does not have inner mechanisms to notify Qml items about its changes. Only when model property from QML item is changed, the whole list is read again. I had faced the same problem before and I implemented a string list using QAbstractListModel as base class. 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to change name in context menu dynamicaly by a condition

From Dev

How to change name in context menu dynamicaly by a condition

From Dev

Interacting with delegated QML Component in Repeater from C++

From Dev

how to get next item in repeater c#, if list is given as datasource?

From Dev

QML Repeater for multiple items without a wrapping Item

From Dev

manage QML Repeater from python

From Dev

Change android theme dynamicaly

From Dev

How can I change dynamicaly the opacity of a linegraph in D3?

From Dev

Delete an item from an asp:Repeater using jQuery

From Dev

how to align repeater control item template content

From Dev

change text format of an span dynamicaly

From Dev

How to create a class dynamicaly

From Dev

How to emit signal in context of specific QML Item from C++ code

From Dev

How to emit signal in context of specific QML Item from C++ code

From Dev

How set C++ class to an Item in qml?

From Dev

how can i change the repeater's label value from code behind

From Dev

How to change Menu Item icon from fragment?

From Dev

How to change/update a item from a Drawer layout?

From Dev

How to change/update a item from a Drawer layout?

From Dev

how to change the column (dataset)value names in repeater

From Dev

How to change image source inside LinkButton in Repeater Asp.Net C#

From Dev

How to change Listbox item color from selected Item(object)?

From Dev

Add extra repeater item or loop from the code behind

From Dev

Get input Value from Repeater with Request.Item

From Dev

Binding property to dynamicaly created item of context menu

From Dev

Change QML Listview delegate from C++

From Dev

Remove character from databinder in repeater C#

From Dev

How to bring dynamicaly scope in view

Related Related

  1. 1

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

  2. 2

    How to change name in context menu dynamicaly by a condition

  3. 3

    How to change name in context menu dynamicaly by a condition

  4. 4

    Interacting with delegated QML Component in Repeater from C++

  5. 5

    how to get next item in repeater c#, if list is given as datasource?

  6. 6

    QML Repeater for multiple items without a wrapping Item

  7. 7

    manage QML Repeater from python

  8. 8

    Change android theme dynamicaly

  9. 9

    How can I change dynamicaly the opacity of a linegraph in D3?

  10. 10

    Delete an item from an asp:Repeater using jQuery

  11. 11

    how to align repeater control item template content

  12. 12

    change text format of an span dynamicaly

  13. 13

    How to create a class dynamicaly

  14. 14

    How to emit signal in context of specific QML Item from C++ code

  15. 15

    How to emit signal in context of specific QML Item from C++ code

  16. 16

    How set C++ class to an Item in qml?

  17. 17

    how can i change the repeater's label value from code behind

  18. 18

    How to change Menu Item icon from fragment?

  19. 19

    How to change/update a item from a Drawer layout?

  20. 20

    How to change/update a item from a Drawer layout?

  21. 21

    how to change the column (dataset)value names in repeater

  22. 22

    How to change image source inside LinkButton in Repeater Asp.Net C#

  23. 23

    How to change Listbox item color from selected Item(object)?

  24. 24

    Add extra repeater item or loop from the code behind

  25. 25

    Get input Value from Repeater with Request.Item

  26. 26

    Binding property to dynamicaly created item of context menu

  27. 27

    Change QML Listview delegate from C++

  28. 28

    Remove character from databinder in repeater C#

  29. 29

    How to bring dynamicaly scope in view

HotTag

Archive