C++ 来自C+的QObject+;到QML到QML到C++;(列表中)

C++ 来自C+的QObject+;到QML到QML到C++;(列表中),c++,qml,C++,Qml,我的想法是在一个列表中有一组QObject驱动类的实例(用C++创建)。然后将此列表传递给QML,每个条目都可以由单独的QML对象查看。现在我希望能够将一个特定的实例传递回C++(例如单击时)。p> 下面是一些代码: QObject派生类 class Data : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name NOTIFY nameChanged) Data(std::string n):_nam

我的想法是在一个列表中有一组QObject驱动类的实例(用C++创建)。然后将此列表传递给QML,每个条目都可以由单独的QML对象查看。现在我希望能够将一个特定的实例传递回C++(例如单击时)。p> 下面是一些代码:

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;
}
控制器(创建列表并接收所选实例)


我终于想出了办法!诀窍是使用

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


而不是main.qml中的
{data:dataList.currentItem]}
{data:dataList.currentItem.modelData]}
。虽然我仍然不知道使用了什么数据类型,以及为什么currentItem似乎使用了与model[dataList.currentIndex]不同的数据类型,但这非常有效

您是否为QML注册了
数据
类型?ie
qmlRegisterType()在你的main()中。是的,我做了。我在问题中添加了更多的细节,尝试将
{data:dataList.currentItem}
而不是
dataList.currentItem.modelData
传递给DataViewer。和
dataList.currentIndex
必须先设置才能获得
currentItem
。我也已经试过了。虽然我可以使用“data.modelData.name”从DataViewer访问数据的name属性,但将数据从DataViewer返回到控制器Controller.takeThisOne(数据)仍然不起作用。schould dataList.currentItem的数据类型是什么?
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;
}
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()}
    }
}
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
    }
}
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?