stringview的动态更新

时间:2016-01-09 10:06:55

标签: qt qml qtquick2

我想通过替换充当QML ListView模型的QStringList(驻留在C ++对象中)来更新ListView。我替换模型QStringList的内容,但依赖于此模型的ListView显示不会更改以反映新内容。这是代码:

QML:

import QtQuick 2.4
import QtQuick.Controls 1.2

Rectangle {
    id: frame
    objectName: "root"
    height: 400;  width: 600

    signal vendorChangedSignal(string vendortext)

ListView {
    width: 100; height: 100

    model: ModelCollection.cbModel
    delegate: Rectangle {
        height: 25
        width: 100
        Text { text: modelData }
    }
}
Button {
    x: 20;  y:300
    text: "Change"
    onClicked: {
        frame.vendorChangedSignal("x")
    }
}

} // Rectangle

和C ++类:

class ModelClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QStringList cbModel READ cbModel WRITE setCbModel NOTIFY cbModelChanged)

    public:
        ModelClass();
        ~ModelClass();

        static ModelClass *instance();

        QString vendorName;
        QString productName;

        QStringList ProductList2();
        QStringList m_comboList;
        Q_INVOKABLE QStringList cbModel();
        void setCbModel(QStringList cbModel);
        void printList(QStringList plist);

    public slots:
        void newVendorSlot(const QString &vendortext);

    signals:
        void cbModelChanged();

    private:
        static ModelClass *m_instance;
        QStringList productList2;
};

和C ++实现方法:

void printList(QStringList plist);

ModelClass *ModelClass::m_instance = NULL;

ModelClass::ModelClass() {
    m_instance = this;
}

ModelClass::~ModelClass() {
    m_instance = NULL;
}

QStringList ModelClass::cbModel() {
    qDebug() << "combomodel() called";
    printList(m_comboList);
    return(m_comboList);
}

void ModelClass::setCbModel(QStringList cbModel) {
    m_comboList = cbModel;
    qDebug() << "set comboModel() called" << m_comboList[0] << "..." <<     m_comboList[3];
}

void ModelClass::newVendorSlot(const QString &msgText) {
    vendorName = msgText;
    () << "C++ newVendorSlot: vendorName:" << vendorName;
    setCbModel(ProductList2());
}

QStringList ModelClass::ProductList2() { // This is only for test purposes
    productList2.clear();
    productList2 << "beans" << "cabbage" << "carrots" << "beetroot" << "potatos";
    return(productList2);
}

void ModelClass::printList(QStringList plist) {  // Only for test purposes.
    QString string1;
    foreach(string1,plist)
        qDebug() << string1;
}

最后,C ++主要代码:

int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
    ModelClass ModelList;
    QQuickView view;

    QStringList dataList1;  // Create QML model
    dataList1.append("oranges");
    dataList1.append("peaches");
    dataList1.append("bananas");
    dataList1.append("grapes");

    ModelList.setCbModel(dataList1);  // store model in object ModelList

    QQmlContext *ctxt = view.rootContext(); // Create QML handle for model:
    ctxt->setContextProperty("ModelCollection", &ModelList);

    view.setSource(QUrl("qrc:main.qml"));

    QObject *dcSelectPanel = view.rootObject(); // Connect QML signal to ModelList slot
    QObject::connect(dcSelectPanel,SIGNAL(vendorChangedSignal(QString)),
                     &ModelList,SLOT(newVendorSlot(QString)));

    view.show();
    return app.exec();
}

生成此控制台输出:

  

设置comboModel()调用&#34; oranges&#34; ...&#34;葡萄&#34;
  combomodel()称为
  &#34;桔子&#34;
  &#34;桃子&#34;
  &#34;香蕉&#34;
  &#34;葡萄&#34;

按下QML按钮时,会显示:

  

C ++ newVendorSlot:vendorName:&#34; x&#34;
  set comboModel()调用&#34; beans&#34; ...&#34;甜菜根&#34;

表示QStringList替换正常。但是StringView显示不会显示,仍会显示水果的名称。

有没有人有任何建议?

1 个答案:

答案 0 :(得分:1)

Using C++ Models with Qt Quick Views说:

  

注意:视图无法知道QStringList的内容已更改。如果QStringList发生更改,则需要通过再次调用QQmlContext :: setContextProperty()来重置模型。

我建议使用基于QAbstractItemModel的模型来避免必须执行此操作,但如果您想使用QStringList,则需要存储指向QML上下文的指针在ModelClass中,以便您可以设置上下文属性。