2014-01-08 29 views
3

我的想法是在列表中創建一堆QObject驅動類的實例(用C++創建)。然後將此列表傳遞給QML,並且可以通過單獨的QML對象查看每個條目。現在我希望能夠將特定實例傳遞迴C++(例如,單擊時)。QObject從C++到QML到QML到C++(在列表中)

下面是一些代碼:

QObject的派生類

class Data : public QObject 
{ 
    Q_OBJECT 
    Q_PROPERTY(QString name READ name NOTIFY nameChanged) 

    Data(std::string n):_name(n){}; 
    QString name(){return QString::fromStdString(_name);}; 
signals: 
    void nameChanged(); 
private: 
    std::string _name; 
} 

控制器(創建列表並接收選擇的實例)

class Controller : public QObject 
{ 
    Q_OBJECT 
    Q_PROPERTY(QQmlListProperty<Data> list READ list NOTIFY listChanged) 

    Controller() 
    { 
     _list.append(new Data("data 1"); 
     _list.append(new Data("data 2"); 
     _list.append(new Data("data 3"); 
    }; 
    QQmlListProperty<Data> list() // <--- provide data to QML 
    { 
     return QQmlListProperty<Grammar>(this, _list); 
    }; 
    void takeThisOne(Data* d)// <--- receive selected instance from QML 
    { 
     //do something with d 
    } 
signals: 
    void listChanged(); 
private: 
    QList<Data*> _list; 
} 

QML主(顯示數據列表)

ApplicationWindow 
{ 
    id: mainWindowContainer 
    width: 800 
    height: 500 

    ListView 
    { 
     id: dataList 
     delegate: Rectangle{ 
        height: 10 
       width: 100 
        Text{text: name} 
        } 
     model: controller.list // <-- what data type are the list items here? 
    } 

    Button 
    { 
     id: btnOpen 
     text: "open selected Data in the DataViewer" 
     onClicked{ 
      // what data type is dataList.currentItem and dataList.currentItem.modelData? 
      var dataViewer = Qt.createComponent("DataViewer.qml").createObject(mainWindowContainer, {data: dataList.currentItem.modelData}); 
      dataViewer.show()} 
    } 
} 

QML DataViewer(顯示數據並將其返回給控制器)​​

Window 
{ 
    height: 400 
    width: 800 
    property variant data // <--- tried 'property Data data', but did not work 

    TextArea 
    { 
     text: data.name 
    } 

    Button 
    { 
     id: btnReturn 
     text: "return to controller" 
     onClicked: {controller.takeThisOne(data)} // <--- does not work 
    } 
} 

我希望這個示例代碼是可以理解的。感謝您的幫助!

編輯:

我做的主要qmlRegisterType<Data>()。還嘗試了qmlRegisterType<Data>("stuff", 1, 0, "Data")並將stuff 1.0導入到DataViewer中。 的問題是,我不知道哪個數據類型我的數據是在不同的點:

Controller: list of Data* 
QML main : list of ??? 
      dataList.currentItem = ??? 
      dataList.currentItem.modelData = ??? 
DataViewer: variant or Data (according to property type, but Data does not work) 
Controller: obviously not Data* as hoped, but what else? 
+0

您是否註冊過QML的'Data'類型?即''qmlRegisterType ();'在你的main()中。 – Dickson

+0

是的,我做到了。我在該問題中添加了一些更多的細節 – Bert

+0

嘗試向DataViewer傳遞'{data:dataList.currentItem}'而不是'dataList.currentItem.modelData'。 'dataList.currentIndex'必須先設置才能獲得'currentItem'。 – Dickson

回答

1

我終於想通了如何做到這一點!訣竅是使用

{data: dataList.model[dataList.currentIndex]}

,而不是{data: dataList.currentItem]}{data: dataList.currentItem.modelData]}在main.qml。儘管我仍然不知道使用了哪些數據類型,以及爲什麼currentItem似乎使用與[dataList.currentIndex]不同的數據類型,這完全適用!