kikeenrique

Untitled

Jan 29th, 2014
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. /home/user/workspace-test/qt5app/
  2. ├── build-qt5app-qt5-Debug
  3. │   ├── main.o
  4. │   ├── Makefile
  5. │   ├── moc_myapplication.cpp
  6. │   ├── moc_myapplication.o
  7. │   ├── moc_myobject.cpp
  8. │   ├── moc_myobject.o
  9. │   ├── moc_qtquick2applicationviewer.cpp
  10. │   ├── moc_qtquick2applicationviewer.o
  11. │   ├── myapplication.o
  12. │   ├── qml
  13. │   │   └── qt5app
  14. │   │       └── main.qml
  15. │   ├── qt5app
  16. │   └── qtquick2applicationviewer.o
  17. ├── main.cpp
  18. ├── myobject.cpp
  19. ├── myobject.h
  20. ├── qml
  21. │   └── qt5app
  22. │       └── main.qml
  23. ├── qt5app64.png
  24. ├── qt5app80.png
  25. ├── qt5app.desktop
  26. ├── qt5app_harmattan.desktop
  27. ├── qt5app.pro
  28. ├── qt5app.pro.user
  29. └── qtquick2applicationviewer
  30.     ├── qtquick2applicationviewer.cpp
  31.     ├── qtquick2applicationviewer.h
  32.     └── qtquick2applicationviewer.pri
  33.  
  34.  
  35.  
  36.  
  37. //qt5app.pro
  38. # Add more folders to ship with the application, here
  39. folder_01.source = qml/qt5app
  40. folder_01.target = qml
  41. DEPLOYMENTFOLDERS = folder_01
  42.  
  43. # Additional import path used to resolve QML modules in Creator's code model
  44. QML_IMPORT_PATH =
  45.  
  46. # If your application uses the Qt Mobility libraries, uncomment the following
  47. # lines and add the respective components to the MOBILITY variable.
  48. # CONFIG += mobility
  49. # MOBILITY +=
  50.  
  51. # The .cpp file which was generated for your project. Feel free to hack it.
  52. SOURCES += main.cpp \
  53.     myobject.cpp
  54.  
  55. # Installation path
  56. # target.path =
  57.  
  58. # Please do not modify the following two lines. Required for deployment.
  59. include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
  60. qtcAddDeployment()
  61.  
  62. HEADERS += \
  63.     myobject.h
  64.  
  65.  
  66.  
  67. //myobject.h
  68. #ifndef MYOBJECT_H
  69. #define MYOBJECT_H
  70.  
  71. #include <QObject>
  72. #include <QDebug>
  73. #include <QQmlComponent>
  74. #include <QQmlEngine>
  75.  
  76. class MyObject : public QObject
  77. {
  78.     Q_OBJECT
  79. public:
  80.     explicit MyObject(QObject *parent = 0);
  81.     ~MyObject(void) {}    
  82.     QObject *object;
  83.     QQmlComponent *component;
  84.    
  85.     void loadComponent(void);
  86. signals:
  87.    
  88. public slots:
  89.     void continueLoading();
  90.    
  91. private:
  92.     QQmlEngine _engine;
  93. };
  94.  
  95. #endif // MYOBJECT_H
  96.  
  97.  
  98. //myobject.cpp
  99. #include <iostream>
  100. #include "myobject.h"
  101.  
  102. MyObject::MyObject(QObject *parent) :
  103.     QObject(parent)
  104. {
  105. }
  106.  
  107. void MyObject::loadComponent(void)
  108. {
  109.     QQmlComponent *component = new QQmlComponent(&_engine);
  110.     component->loadUrl(QStringLiteral("qml/qt5app/main.qml"));
  111.    
  112.     if(!component->isReady()){
  113.         qWarning("qPrintable: %s", qPrintable(component->errorString()));
  114.     }
  115.    
  116.     if (component->isLoading()){
  117.         std::cout <<"==== component->isLoading ====";
  118.         QObject::connect(component,
  119.                          SIGNAL(statusChanged()),
  120.                          this,
  121.                          SLOT(continueLoading()));
  122.     }
  123.     else{
  124.         std::cout <<"==== component is not Loading ====";
  125.         continueLoading();
  126.     }
  127. }  
  128.  
  129. void MyObject::continueLoading()
  130. {
  131.     QQmlComponent *component = new QQmlComponent(&_engine);
  132.     component->loadUrl(QStringLiteral("qml/qt5app/main.qml"));
  133.    
  134.     if (component->isError()) {
  135.         qWarning() << "component->isError()="<< component->errors();
  136.     } else {
  137.         object = component->create();
  138.         std::cout <<"object created";
  139.     }
  140. }
  141.  
  142. //main.qml
  143. import QtQuick 2.0
  144.  
  145. Rectangle {
  146.    width: 360
  147.    height: 360
  148.    function myQmlFunction(msg_cpp) {
  149.        console.log("Got msg_cpp:", msg_cpp)
  150.        return "output"
  151.    }
  152. }
  153.  
  154. //main.cpp
  155. #include <QtGui/QGuiApplication>
  156. #include "qtquick2applicationviewer.h"
  157. #include <QtQuick/QQuickItem>
  158. #include <QtQuick/QQuickView>
  159. #include <QQmlEngine>
  160. #include <QtQml>
  161. #include <QtCore>
  162. #include <QtQuick/QtQuick>
  163. #include <QQmlComponent>
  164. #include <QtQml/qqml.h>
  165. #include "myobject.h"
  166.  
  167. int main(int argc, char *argv[])
  168. {
  169.     QGuiApplication app(argc, argv);
  170.  
  171.     QtQuick2ApplicationViewer viewer;
  172.     viewer.setMainQmlFile(QStringLiteral("qml/qt5app/main.qml"));
  173.  
  174.     MyObject *myClass = new MyObject();
  175.     myClass->loadComponent();
  176.  
  177.     QObject *object=myClass->object;
  178.     qDebug() << "step 1";
  179.  
  180.     QVariant returnedValue;
  181.     QVariant msg_cpp = "C++ message";
  182.     QMetaObject::invokeMethod(object,
  183.                               "myQmlFunction",
  184.                               Q_RETURN_ARG(QVariant, returnedValue),
  185.                               Q_ARG(QVariant, msg_cpp));
  186.     qDebug() << "step 2";
  187.    
  188.     qDebug() << "Got QML return:" << returnedValue.toString();
  189.  
  190.     viewer.showExpanded();
  191.     object->deleteLater();
  192.     return app.exec();
  193. }
Add Comment
Please, Sign In to add comment