Skip to content

Instantly share code, notes, and snippets.

@webmaster128
Created February 17, 2015 22:54
Show Gist options
  • Select an option

  • Save webmaster128/1a9dc59cb2b5912a6a25 to your computer and use it in GitHub Desktop.

Select an option

Save webmaster128/1a9dc59cb2b5912a6a25 to your computer and use it in GitHub Desktop.
Q_INVOKABLE and namespaces example
#include "bar.h"
namespace Foo {
Bar::Bar(QObject *parent) : QObject(parent)
{
}
Bar *Bar::run()
{
return this;
}
}
#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();
};
}
TEMPLATE = app
QT += qml quick
SOURCES += main.cpp bar.cpp
HEADERS += bar.h
#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