2011-05-31 121 views
1

我怎样才能改变与C++代码PathView的模式? 我的对象名添加到我的pathView找到它,然后我改变这样的属性,但是当我这样做,我的列表是空的:QT QML进口的ListModel从C++到QML

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(); 
    } 
} 
+0

你必须张贴在SynergieListObject的“数据”功能和的appDelegate源。 – Abhijith 2011-05-31 12:56:50

+0

我加了,谢谢 – NicoMinsk 2011-05-31 14:14:29

+0

我从来没有用过QdeclarativeItem在QML中设置模型。试试这个,而不是'QDeclarativeContext * ctxt = view.rootContext(); ctxt-> setContextProperty( “模型”,的QVariant :: fromValue(DataList控件));' – Abhijith 2011-05-31 15:22:09

回答

3

我从来没有用QdeclarativeItem在QML中设置模型。试试这个代替

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

声明模型作为根的属性。这样你可以设置model.Or添加一个函数,它将模型作为参数并为视图设置模型。然后你可以从C++中调用这个函数。