Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtGui/QGuiApplication>
- #include <QQmlContext>
- #include <QQuickView>
- #include <QDebug>
- #include "qtquick2applicationviewer.h"
- #include "QAbstractListModel"
- class Model : public QAbstractListModel
- {
- Q_OBJECT
- public:
- Model() {}
- int rowCount(const QModelIndex &parent) const
- {
- return mList.size();
- }
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const{
- if (index.row() < 0 || index.row() >= mList.size()) {
- return QVariant();
- }
- return mList.at(index.row());
- }
- Q_INVOKABLE QVariant get(int index) {
- return data(createIndex(index, 0));
- }
- Q_INVOKABLE void append(QVariant element) {
- beginInsertRows(QModelIndex(), mList.size() + 1, mList.size() + 1);
- mList.append(element.toMap().value("name").toString());
- endInsertRows();
- }
- private:
- QList<QString> mList;
- };
- int main(int argc, char *argv[])
- {
- QGuiApplication app(argc, argv);
- QtQuick2ApplicationViewer viewer;
- Model model;
- viewer.rootContext()->setContextProperty("model", &model);
- viewer.setMainQmlFile(QStringLiteral("qml/quick/main.qml"));
- viewer.showExpanded();
- return app.exec();
- }
- #include "main.moc"
- import QtQuick 2.2
- Item {
- width: 360
- height: 360
- Component.onCompleted: {
- model.append({name: "blah"});
- console.assert(model.get(0) === "blah");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement