Created
February 17, 2015 22:54
-
-
Save webmaster128/1a9dc59cb2b5912a6a25 to your computer and use it in GitHub Desktop.
Q_INVOKABLE and namespaces example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "bar.h" | |
| namespace Foo { | |
| Bar::Bar(QObject *parent) : QObject(parent) | |
| { | |
| } | |
| Bar *Bar::run() | |
| { | |
| return this; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <QObject> | |
| namespace Foo { | |
| class Bar : public QObject | |
| { | |
| Q_OBJECT | |
| public: | |
| explicit Bar(QObject *parent = 0); | |
| // Q_INVOKABLE Foo::Bar *run(); | |
| Q_INVOKABLE Bar *run(); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TEMPLATE = app | |
| QT += qml quick | |
| SOURCES += main.cpp bar.cpp | |
| HEADERS += bar.h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QGuiApplication> | |
| #include <QQmlApplicationEngine> | |
| #include <QtQml> | |
| #include "bar.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| QGuiApplication app(argc, argv); | |
| qmlRegisterType<Foo::Bar>("MyModules", 1, 0, "QMLFooBar"); | |
| QByteArray qml = QString( | |
| "import QtQuick 2.3 \n" | |
| "import QtQuick.Controls 1.2 \n" | |
| "import MyModules 1.0 \n" | |
| " \n" | |
| "ApplicationWindow { \n" | |
| " visible: true \n" | |
| " width: 640 \n" | |
| " height: 480 \n" | |
| " \n" | |
| " QMLFooBar { \n" | |
| " id: fb \n" | |
| " } \n" | |
| " \n" | |
| " Button { \n" | |
| " text: 'test!' \n" | |
| " anchors.centerIn: parent \n" | |
| " onClicked: fb.run() && console.log('ok') \n" | |
| " } \n" | |
| "} \n" | |
| ).toUtf8(); | |
| QQmlApplicationEngine engine; | |
| engine.loadData(qml); | |
| return app.exec(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment