QObject from C++ to QML to QML to C++ (in a list)

Bert

My idea is to have a bunch of instances of a QObject drived class in a list (created in C++). This list is then passed to QML and each entry can be viewd by a separate QML Object. Now I want to be able to pass a specific instance back to C++ (e.g. when clicked).

Here is some code:

QObject derived class

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

Controller (creating list and receiving selected instance)

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 main (displaying 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()}
    }
}

QML DataViewer (displaying data and returning it to the controller)

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

I hope this example code is understandable. Thanks for helping!

EDIT:

I'm doing qmlRegisterType<Data>() in the main. Also tried qmlRegisterType<Data>("stuff", 1, 0, "Data") and importing stuff 1.0 into DataViewer. The problem is, that I don't know which data type my Data is at different points:

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?
Bert

I finally figured out how to do this! The trick is to use

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

instead of {data: dataList.currentItem]} or {data: dataList.currentItem.modelData]} in the main.qml. Though I still don't know what data types are used and why currentItem seems to use a different data type than model[dataList.currentIndex], this works perfectly!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related