如何在QML文件中使用属性的名称作为字符串查找QML Context属性

时间:2015-05-05 13:59:17

标签: c++ qt qml

我有一个注册为QML上下文属性的模型,如下所示:

modelPtr = new MyModel();
engine_.rootContext()->setContextProperty(modelName, modelPtr);

在我的QML文件中,而不是将modelName硬编码为属性绑定,如下所示:

ListView {
    model: modelName
} 

有没有办法可以通过字符串名称搜索我想要的模型?

到目前为止,我已经尝试过:

Item {
    property string modelName
    ListView {
         id: myListView
         Component.onCompleted: {
             myListView.model = Qt.binding(function() { return modelManager.getModel(modelName) } )
         }
    }
}

其中modelManager包含系统使用的所有模型(并且是注册的上下文属性)和函数" getModel"将QObject *返回到所需的模型。此方法似乎不适用于模型更新。

1 个答案:

答案 0 :(得分:0)

它应该有用,但如果没有看到你的模型,就很难说出现了什么问题。这对我有用:

#include <QAbstractItemModel>
#include <QStandardItemModel>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

class ModelManager : public QObject
{
    Q_OBJECT
public:
    ModelManager() {
        QStandardItemModel *model1 = new QStandardItemModel(4, 1, this);
        for (int row = 0; row < 4; ++row) {
            QStandardItem *item = new QStandardItem(QString("model1 row %0, column 0").arg(row));
            model1->setItem(row, 0, item);
        }
        mModels.insert("model1", model1);

        QStandardItemModel *model2 = new QStandardItemModel(4, 1, this);
        for (int row = 0; row < 4; ++row) {
            QStandardItem *item = new QStandardItem(QString("model2 row %0, column 0").arg(row));
            model2->setItem(row, 0, item);
        }
        mModels.insert("model2", model2);
    }

    Q_INVOKABLE QObject *getModel(const QString &modelName) {
        return mModels.value(modelName);
    }

    Q_INVOKABLE void changeStuff() {
        QStandardItemModel *model = static_cast<QStandardItemModel*>(mModels.value("model1"));
        QStandardItem *item = model->item(0, 0);
        item->setText("model1 row 0 column 0 - changed");
    }

private:
    QHash<QString, QAbstractItemModel*> mModels;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    ModelManager modelManager;

    qRegisterMetaType<QAbstractItemModel*>();

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("modelManager", &modelManager);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

#include "main.moc"

QML:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    ComboBox {
        id: comboBox
        model: ["model1", "model2"]
    }

    ListView {
        id: listView
        width: 200
        height: 200
        anchors.centerIn: parent
        model: modelManager.getModel(comboBox.currentText)
        delegate: Rectangle {
            width: listView.width
            height: text.implicitHeight

            Text {
                id: text
                anchors.centerIn: parent
                text: display
            }
        }
    }

    Button {
        text: "Change stuff"
        anchors.horizontalCenter: listView.horizontalCenter
        onClicked: modelManager.changeStuff()
    }
}