Advertisement
Guest User

Untitled

a guest
Sep 18th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <QGuiApplication>
  2. #include <QQmlPropertyMap>
  3. #include <QQmlContext>
  4. #include <QQmlApplicationEngine>
  5. #include <QQuickWindow>
  6. #include <QTimer>
  7. #include <QDebug>
  8.  
  9. class Object : public QObject
  10. {
  11.     Q_OBJECT
  12. public:
  13.     Object(QObject *parent = 0)
  14.         : QObject(parent),
  15.       m_propertyMap(0)
  16.     {
  17.         m_propertyMap = new QQmlPropertyMap;
  18.     m_propertyMap->insert("name", QVariant(QString("John Smith")));
  19.     m_propertyMap->insert("phone", QVariant(QString("555-5555")));
  20.  
  21.     QTimer::singleShot(2000, this, SLOT(changeData()));
  22.     }
  23.  
  24.     QQmlPropertyMap *propertyMap() { return m_propertyMap; }
  25.  
  26. private Q_SLOTS:
  27.     void changeData() {
  28.         m_propertyMap->insert("name", "Smith John");
  29.         m_propertyMap->insert("phone", "5555-555");
  30.     }
  31.  
  32.     void dataChanged(const QString &key, const QVariant &data) {
  33.         qDebug() << "property(" << key << ") = " << data;
  34.     }
  35.  
  36. private:
  37.     QQmlPropertyMap *m_propertyMap;
  38.  
  39. };
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.     QGuiApplication app(argc, argv);  
  44.     Object object;
  45.     QQmlApplicationEngine engine(QUrl::fromLocalFile("main.qml"));
  46.     QObject *topLevel = engine.rootObjects().value(0);
  47.     QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
  48.     if (!window) {
  49.         qWarning("Error: Your root item has to be a Window.");
  50.         return -1;
  51.     }
  52.  
  53.     QQmlContext *ctxt = engine.rootContext();
  54.     ctxt->setContextProperty("testData", object.propertyMap());
  55.     window->show();
  56.  
  57.     return app.exec();
  58. }
  59.  
  60. #include "main.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement