c++ - XML 在 ListView 中没有改变

标签 c++ qml blackberry-10 blackberry-cascades

我正在尝试使用 C++ 在我的 .qml 文件中(当我单击按钮时)使 XML 数据模型发生动态变化。为此,我返回了一个 Qt 属性 (GroupDataModel)。但是在返回对象后,ListView 没有改变,尽管我看到模型属性再次返回。

OBS:如果我从 .qml 中的 XMLDataModel 加载它,而不是用 C++ 代码加载它,它就可以工作。

这是我的 XmlTest.hpp:

#ifndef XmlTest_HPP_
#define XmlTest_HPP_

#include <QObject>
#include <bb/cascades/GroupDataModel>

namespace bb { namespace cascades { class Application; }}

class XmlTest : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bb::cascades::GroupDataModel* model READ model NOTIFY onModelChanged);
public:
    XmlTest(bb::cascades::Application *app);
    virtual ~XmlTest() {}

    Q_INVOKABLE
    bb::cascades::GroupDataModel *model();

    Q_INVOKABLE
    void setGroupDataModel();
signals:
    void onModelChanged();
private:
    bb::cascades::GroupDataModel *m_model;
};

#endif /* XmlTest_HPP_ */

和 XmlTest.cpp:

#include "XmlTest.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/data/XmlDataAccess>

using namespace bb::cascades;
using namespace bb::data;

XmlTest::XmlTest(Application *app)
: QObject(app)
{
    m_model = new GroupDataModel();
    qRegisterMetaType<GroupDataModel *>("GroupDataModel *");

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_xmlTest", this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}

GroupDataModel *XmlTest::model()
{
    qDebug("Returning m_model");
    return m_model;
}

void XmlTest::setGroupDataModel()
{
    XmlDataAccess xml;
    QVariant xmlData = xml.load(QDir::currentPath() + "/app/native/assets/models/model.xml");
    m_model->clear();
    m_model->insertList(xmlData.toList());
    qDebug("File loaded");
    emit this->onModelChanged();
}

我的 main.qml 文件(只是一个带有按钮的 ListView):

import bb.cascades 1.0

Page {
    Container {
        id: mainContainer
        layout: DockLayout {}
        ListView {
            id: listView
            dataModel: _xmlTest.model
            //dataModel: XmlDataModel {
            //    source: "models/model2.xml"
            //}
            onDataModelChanged: {
                console.log("Data model changed!"); 
            }
            listItemComponents: [
                ListItemComponent {
                    type: "user"
                    StandardListItem {
                        title: ListItemData.realname
                        description: ListItemData.name
                    }
                },
                ListItemComponent {
                    type: "option"
                    StandardListItem {
                        title: ListItemData.title
                    }
                }
            ]
        }
        Button {
            text: "Click"
            onClicked: {
                console.log("Trying to load file");
                _xmlTest.setGroupDataModel();
            }
            verticalAlignment: VerticalAlignment.Bottom
            horizontalAlignment: HorizontalAlignment.Center
        }
    }
}

以及我要加载的 XML:

<root>
    <user name="myUsername" realname="My Real Name"/>
    <option title="Option 1"/>
    <option title="Option 2"/>
    <option title="Option 3"/>
    <option title="Option 4"/>
    <option title="Option 5"/>
</root>

最佳答案

Q_INVOKABLE 函数是某种槽,因此它们不能返回对象。 您应该找到 ListView 并放置数据模型。

  • 在id后添加QML: objectName: "listview"
  • 在 C++ 中获取 ListView :ListView* listView = root->findChild("listView");
  • 设置数据模型:listView->setDataModel(m_model);

关于c++ - XML 在 ListView 中没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14327025/

相关文章:

c++ - 在两个共享对象之间共享变量

qt - QML 条件绑定(bind)未按预期工作

c++ - BlackBerry 10 中的反向地理编码

c++ - 如何在 Cascades、Blackberry 10 中使用 Qt/QML/C++ 从另一个 qml 文件更改一个 qml 文件中的标签文本?

c++ - CBlobCache 用法 - ATL Server 库

c++ - 将接口(interface)或对函数的引用作为参数传递

c++ - QML ListView 多选

qt - 错误: Cannot assign QObject* to QQuickItem*

c++ - 黑莓 10 中的 Web 服务

使用 Link inTime 替换的 C++ 单元测试