[Interest] SingletonType functions not visible in QML

mark diener rpzrpzrpz at gmail.com
Fri May 22 15:56:34 CEST 2015


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
Window {
    width: Screen.width    height: Screen.height    visible: true
    ColumnLayout {        anchors.centerIn: parent
        Button {            id: gbutton            text: qsTr("Press
Me 1")            onClicked:            {
Sing.Buttonpress(gbutton) ;            }        }
        TextInput {            id: ginput            text: qsTr("Press
Me 2")            color: Sing.Getcolor(ginput)        }    }
}

###### Application Output on Run and button Press:

Debugging starts

QML debugging is enabled. Only use this in a safe environment.

QML Debugger: Waiting for connection on port 49970...

Register Singleton: 10

qrc:/main.qml:30: TypeError: Property 'Getcolor' of object [object Object]
is not a function

qrc:/main.qml:23: TypeError: Property 'Buttonpress' of object [object
Object] is not a function
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150522/852bf85d/attachment.html>


More information about the Interest mailing list