Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /home/user/workspace-test/qt5app/
- ├── build-qt5app-qt5-Debug
- │ ├── main.o
- │ ├── Makefile
- │ ├── moc_myapplication.cpp
- │ ├── moc_myapplication.o
- │ ├── moc_myobject.cpp
- │ ├── moc_myobject.o
- │ ├── moc_qtquick2applicationviewer.cpp
- │ ├── moc_qtquick2applicationviewer.o
- │ ├── myapplication.o
- │ ├── qml
- │ │ └── qt5app
- │ │ └── main.qml
- │ ├── qt5app
- │ └── qtquick2applicationviewer.o
- ├── main.cpp
- ├── myobject.cpp
- ├── myobject.h
- ├── qml
- │ └── qt5app
- │ └── main.qml
- ├── qt5app64.png
- ├── qt5app80.png
- ├── qt5app.desktop
- ├── qt5app_harmattan.desktop
- ├── qt5app.pro
- ├── qt5app.pro.user
- └── qtquick2applicationviewer
- ├── qtquick2applicationviewer.cpp
- ├── qtquick2applicationviewer.h
- └── qtquick2applicationviewer.pri
- //qt5app.pro
- # Add more folders to ship with the application, here
- folder_01.source = qml/qt5app
- folder_01.target = qml
- DEPLOYMENTFOLDERS = folder_01
- # Additional import path used to resolve QML modules in Creator's code model
- QML_IMPORT_PATH =
- # If your application uses the Qt Mobility libraries, uncomment the following
- # lines and add the respective components to the MOBILITY variable.
- # CONFIG += mobility
- # MOBILITY +=
- # The .cpp file which was generated for your project. Feel free to hack it.
- SOURCES += main.cpp \
- myobject.cpp
- # Installation path
- # target.path =
- # Please do not modify the following two lines. Required for deployment.
- include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
- qtcAddDeployment()
- HEADERS += \
- myobject.h
- //myobject.h
- #ifndef MYOBJECT_H
- #define MYOBJECT_H
- #include <QObject>
- #include <QDebug>
- #include <QQmlComponent>
- #include <QQmlEngine>
- class MyObject : public QObject
- {
- Q_OBJECT
- public:
- explicit MyObject(QObject *parent = 0);
- ~MyObject(void) {}
- QObject *object;
- QQmlComponent *component;
- void loadComponent(void);
- signals:
- public slots:
- void continueLoading();
- private:
- QQmlEngine _engine;
- };
- #endif // MYOBJECT_H
- //myobject.cpp
- #include <iostream>
- #include "myobject.h"
- MyObject::MyObject(QObject *parent) :
- QObject(parent)
- {
- }
- void MyObject::loadComponent(void)
- {
- QQmlComponent *component = new QQmlComponent(&_engine);
- component->loadUrl(QStringLiteral("qml/qt5app/main.qml"));
- if(!component->isReady()){
- qWarning("qPrintable: %s", qPrintable(component->errorString()));
- }
- if (component->isLoading()){
- std::cout <<"==== component->isLoading ====";
- QObject::connect(component,
- SIGNAL(statusChanged()),
- this,
- SLOT(continueLoading()));
- }
- else{
- std::cout <<"==== component is not Loading ====";
- continueLoading();
- }
- }
- void MyObject::continueLoading()
- {
- QQmlComponent *component = new QQmlComponent(&_engine);
- component->loadUrl(QStringLiteral("qml/qt5app/main.qml"));
- if (component->isError()) {
- qWarning() << "component->isError()="<< component->errors();
- } else {
- object = component->create();
- std::cout <<"object created";
- }
- }
- //main.qml
- import QtQuick 2.0
- Rectangle {
- width: 360
- height: 360
- function myQmlFunction(msg_cpp) {
- console.log("Got msg_cpp:", msg_cpp)
- return "output"
- }
- }
- //main.cpp
- #include <QtGui/QGuiApplication>
- #include "qtquick2applicationviewer.h"
- #include <QtQuick/QQuickItem>
- #include <QtQuick/QQuickView>
- #include <QQmlEngine>
- #include <QtQml>
- #include <QtCore>
- #include <QtQuick/QtQuick>
- #include <QQmlComponent>
- #include <QtQml/qqml.h>
- #include "myobject.h"
- int main(int argc, char *argv[])
- {
- QGuiApplication app(argc, argv);
- QtQuick2ApplicationViewer viewer;
- viewer.setMainQmlFile(QStringLiteral("qml/qt5app/main.qml"));
- MyObject *myClass = new MyObject();
- myClass->loadComponent();
- QObject *object=myClass->object;
- qDebug() << "step 1";
- QVariant returnedValue;
- QVariant msg_cpp = "C++ message";
- QMetaObject::invokeMethod(object,
- "myQmlFunction",
- Q_RETURN_ARG(QVariant, returnedValue),
- Q_ARG(QVariant, msg_cpp));
- qDebug() << "step 2";
- qDebug() << "Got QML return:" << returnedValue.toString();
- viewer.showExpanded();
- object->deleteLater();
- return app.exec();
- }
Add Comment
Please, Sign In to add comment