QWidget Application with QML and Registered Types

I have a QWidget application that uses QML. I have a class that I am using to expose some of our utility functions.

I digested the problem down to the following code (I'll explain my problem below the code):

First, here's the file main.cpp

(I've cut most of it includes

for brevity):

#include "main.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow window;
    window.show();

    return app.exec();
}

      

Here's the included main.h

:

class MyUtils : public QObject
{
    Q_OBJECT

public:
    MyUtils(QObject* parent = nullptr)
        : QObject(parent)
    {
    }

    virtual ~MyUtils() = default;

    Q_INVOKABLE QString doSomething()
    {
        return QString("I did something!");
    }

    static QObject* MyUtilsProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)

        qDebug() << "MyUtils Invoked!";
        return new MyUtils();
    }
};

class MyView : public QQuickWidget
{
    Q_OBJECT

public:
    MyView(QWidget* parent = nullptr)
        : QQuickWidget(parent)
    {
        setResizeMode(QQuickWidget::SizeRootObjectToView);
        setSource(QUrl("qrc:/main.qml"));
    }

    virtual ~MyView() = default;
};


class MainWindow : public QMainWindow
{
    Q_OBJECT

    QTabWidget   _tabView;

public:
    MainWindow(QWidget * parent = 0)
        : QMainWindow(parent)
    {
        qmlRegisterSingletonType<MyUtils>("MyUtilities", 1, 0, "myutils", &MyUtils::MyUtilsProvider);
        setCentralWidget(&_tabView);
        _tabView.addTab(new MyView(), "Tab 1");
    }
};

      

And lastly, here is my QML file:

import QtQuick 2.1
import MyUtilities 1.0

Rectangle
{
    Text
    {
        text: myutils.doSomething()
        anchors.centerIn: parent
    }
}

      

What I am trying to do is register the class MyUtils

as a singleton, which I can then include in my QML and use. The problem is when I run this I get the following message from the application output:

QML debugging is enabled. Use this only in a safe environment.

Qml debugging is enabled. Only use this in a safe environment!

qrc: /main.qml: 8: ReferenceError: myutils is undefined

I've tried putting qmlRegisterSingletonType

in main()

before the object is created QApplication

(and other giggle places), but so far I haven't been able to get this to work.

I noticed that if I put a breakpoint or message qDebug()

in a method MyUtils::MyUtilsProvider

, it never gets called. This makes me think that maybe my class is MyView

using a different object QQmlEngine

than the one that qmlRegisterSingletonType

registers the singleton with. But if so, then I don't know how to get this engine and then go to the constructor MyView

.

Can someone please tell me what I am doing wrong and how can I get this to work?

Thank!

+3


source to share


1 answer


QML component names must start with capital letters:

qmlRegisterSingletonType<MyUtils>("MyUtilities", 1, 0, "Myutils",
                                  &MyUtils::MyUtilsProvider);

      



and therefore

text: Myutils.doSomething()

      

+4


source







All Articles