开发者

QT QML import ListModel from C++ to QML

How can i change the model of a PathView with c++ code ? i add an objectName to my pathView to find it, then i change the property like this, but when i do that, my list is empty :

QDeclarativeItem *listSynergie  = myClass.itemMain->findChild<QDeclarativeItem*> ("PathViewInscription");
listSynergie->setProperty("model", QVariant::fromValue(dataList));

My data list is fill like this :

QList<QObject*> dataList;
dataList.append(new synergieListObject("Item 1", "1",false));
dataList.append(new synergieListObject("Item 2", "2",true));
dataList.append(new synergieListObject("Item 3", "3",false));
dataList.append(new synergieListObject("Item 4", "4",false));

This is the code of my PathView :

PathView {
    objectName: "PathViewInscription"
    Keys.onRightPressed: if (!moving) { incrementCurrentIndex(); console.log(moving) }
    Keys.onLeftPressed: if (!moving) decrementCurrentIndex()
    id: view
    anchors.fill: parent
    highlight: Image { source: "../spinner_selecter.png"; width: view.width; height: itemHeight+4; opacity:0.3}
    preferredHighlightBegin: 0.5
    preferredHighlightEnd: 0.5
    focus: true
    model: appModel
    delegate: appDelegate

    dragMargin: view.width/2
    pathItemCount: height/itemHeight
    path: Path {
        startX: view.width/2; startY: -itemHeight/2
        PathLine { x: view.width/2; y: view.pathItemCount*itemHeight + itemHeight }
    }
}

and the code of ListModel :

ListModel {
    id: appModel
    ListElement { label: "syn1"; value: "1"; selected:false}
    ListElement { label: "syn2"; value: "2" ; selected:false}
    ListElement { label: "syn3"; value: "3" ; selected:false}
}

what's wrong ? Thanks !

EDIT :

the code of the appDelegate :

    Component {
    id: appDelegate
    Item {
        width: 100; height: 100

        Text {
            an开发者_开发知识库chors { horizontalCenter: parent.horizontalCenter }
            text: label
            smooth: true
        }

        MouseArea {
            anchors.fill: parent
            onClicked: view.currentIndex = index
        }
    }
}

the code of my object :

    #ifndef SYNERGIELISTOBJECT_H
#define SYNERGIELISTOBJECT_H
#include <QObject>

class synergieListObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
    Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)
    Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged)

public:
    synergieListObject(QObject *parent=0);
    synergieListObject(const QString &label,const QString &value,bool selected, QObject *parent=0);

    QString label() const;
    void setLabel(const QString &label);

    QString value() const;
    void setValue(const QString &value);

    bool selected() const;
    void setSelected(const bool &selected);

signals:
    void labelChanged();
    void valueChanged();
    void selectedChanged();

private:
    QString m_label;
    QString m_value;
    bool m_selected;
};

#endif // SYNERGIELISTOBJECT_H

c++ my object :

#include "synergielistobject.h"


synergieListObject::synergieListObject(QObject *parent): QObject(parent)
{
}

synergieListObject::synergieListObject(const QString &label,const QString &value,bool selected, QObject *parent): QObject(parent), m_label(label), m_value(value), m_selected(selected)
{
}

QString synergieListObject::label() const
{
    return m_label;
}

void synergieListObject::setLabel(const QString &label)
{
    if (label != m_label) {
        m_label = label;
        emit labelChanged();
    }
}


QString synergieListObject::value() const
{
    return m_value;
}

void synergieListObject::setValue(const QString &value)
{
    if (value != m_value) {
        m_value = value;
        emit valueChanged();
    }
}


bool synergieListObject::selected() const
{
    return m_selected;
}

void synergieListObject::setSelected(const bool &selected)
{
    if (selected != m_selected) {
        m_selected = selected;
        emit selectedChanged();
    }
}


I have never used QdeclarativeItem to set model in QML . Try this instead

QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("model",  QVariant::fromValue(dataList));

Declare the model as a property of the root. This way you can set model.Or add a function that takes model as argument and sets the model for the view. Then you can call this function from c++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜