簡體   English   中英

QObject從C ++到QML到QML到C ++(在列表中)

[英]QObject from C++ to QML to QML to C++ (in a list)

我的想法是在列表中創建一堆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 main(顯示數據列表)

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?

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

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

而不是{data: dataList.currentItem]}{data: dataList.currentItem]}{data: dataList.currentItem.modelData]} 雖然我仍然不知道使用了什么數據類型以及為什么currentItem似乎使用與模型[dataList.currentIndex]不同的數據類型,但這非常有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM