1

如何使用 C++ 代码更改 PathView 的模型?我将 objectName 添加到我的 pathView 以找到它,然后我像这样更改属性,但是当我这样做时,我的列表是空的:

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

我的数据列表是这样填写的:

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));

这是我的 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 }
    }
}

和 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}
}

怎么了 ?谢谢 !

编辑 :

appDelegate 的代码:

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

        Text {
            anchors { horizontalCenter: parent.horizontalCenter }
            text: label
            smooth: true
        }

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

我的对象的代码:

    #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++ 我的对象:

#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();
    }
}
4

1 回答 1

3

我从未使用 QdeclarativeItem 在 QML 中设置模型。试试这个

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

将模型声明为根的属性。这样您就可以设置模型。或者添加一个以模型为参数并为视图设置模型的函数。然后你可以从 C++ 调用这个函数。

于 2011-06-01T12:52:49.380 回答