QT QML将ListModel从C ++导入QML

问题描述

| 如何使用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,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();
    }
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)