Important: Please read the Qt Code of Conduct -

[solved] Problem returning embeded custom type developt in Qt from within QML

  • Hello guys,

    I have a little problem. I have a class which looks something like this:
    public class MyClass : public QObject {
    Q_INVOKABLE RClock clock() const { return _clock; }

    What I want to do in QML is, return the RClock element and access each of it's methods. So I thought to build a supclass of QObject, make each method Q_INVOKABLE and try in QML something like this:
    function doSomething(){
    var time = data.clock()
    myHour.text = "" + time.hour()

    But everything I get on the command line of Qt Creator is that time is undefined.

    Anyone an idea what I'm doing wrong?

    myHour is a Text element somewhere else in the QML file which should print the value of the hour of my custom clock.

    Best regards

  • Did you register RClock as QML type with qmlRegisterType()?

  • No, i only register the MyClass object to the view with @view->rootContext()->setContextProperty(myClassObject)@

    and the RClock is embeded in MyClass so I should think that I can call the getter of MyClass returning the RClock and then calling RClock's getter methods to get the date and time stored as integer values in it.

    How should I do it with qmlRegisterType()?? It's not a visual item which is not derived from QGraphicsViewItem

  • Ok I found out what I've forgotten.

    I had to add the following to the qml file:
    RClock {
    id: myClock

    And in RClock I added Q_PROPERTY's for each value.
    And as you mentioned the qmlRegisterType() method was also needed.
    Now everything works fine.

Log in to reply