[Interest] SingletonType functions not visible in QML

Ben Lau xbenlau at gmail.com
Fri May 22 16:06:50 CEST 2015


On 22 May 2015 at 21:56, mark diener <rpzrpzrpz at gmail.com> wrote:

> Hello:
>
> I have a test qmlRegisterSingletonType project as listed below.
>
> The singleton Q_INVOKABLE functions are somehow not visible or in
> "scope".
>
> Anybody have ideas about this?
>
> ##### sing.h
>
> #include <QObject>
>
> #include <QtQml>
>
> #include <QString>
>
> #include <QDebug>
>
>
> class Sing : public QObject
>
> {
>
>     Q_OBJECT
>
> public:
>
>     explicit Sing(QObject *gparent = 0) { Q_UNUSED(gparent) }
>
>     ~Sing() { }
>
>
>     Q_INVOKABLE int Buttonpress(QObject* gparm)
>
>     {
>
>         qDebug() << "ButtonPress:" << gparm ;
>
>         return 22 ;
>
>     }
>
>
>     Q_INVOKABLE QString Getcolor(QObject* gparm)
>
>     {
>
>         qDebug() << "GetColor:" << gparm ;
>
>         return "red" ;
>
>     }
>
>
> public slots:
>
> signals:
>
>
> };
>
>
> ###### main.cpp
>
> #include <QApplication>#include <QQmlApplicationEngine>#include <QQmlEngine>
> #include "sing.h"
> static QObject *singprovider(QQmlEngine *gengine, QJSEngine *gscriptEngine){    Q_UNUSED(gengine)    Q_UNUSED(gscriptEngine)    Sing* gsing = new Sing();    return gsing;}
> int main(int argc, char *argv[]){    QApplication app(argc, argv);    QQmlApplicationEngine engine;
>     int gres = qmlRegisterSingletonType<Sing>("Singapi", 1, 0, "Sing", singprovider);
>     qDebug() << "Register Singleton:" << gres ;
>     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
>     return app.exec();}
>
> ###### main.qml
> import QtQuick 2.4import QtQuick.Controls 1.3import QtQuick.Window 2.2import QtQuick.Layouts 1.1import Singapi 1.0 as Sing
>
>
In this case, you should use Sing.Sing to access your singleton object , or
take out "as Sing" from the import statement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150522/3f79bc73/attachment.html>


More information about the Interest mailing list