C++ QML Wrappers wise or not



  • Hi everyone and happy new year,

    Using Qt5.2/3. I would like to use some QObject derived classes in QML.The obvious and certainly easiest thing is to register the types with qmlRegisterType and add additional Q_INVOKABLE and Q_PROPERTYs : something like

    @
    class Network : public QObject
    {
    public:
    Q_OBJECT

    public:
    Network(QObject *parent = 0);
    ~Network();

    virtual void callMessage() { qDebug() << "Network::callMessage();"; this->metaObject()->invokeMethod(this, "message"); }
    Q_INVOKABLE void message() { qDebug() << "Network::message();"; }
    Q_INVOKABLE QObject * create(const QString &type);
    

    };
    @

    My overall aim to allow override of methods (using invokeMethod) in QML and invoke these from c++ which would provide powerful funcitonality.

    Ideally, I would prefer to avoid adding QML directly to the original classes. I would like to keep the class clean from QML because I may want to provide other language bindings later. I looked at c++ interface paper from

    "https://www.qtdeveloperdays.com/2013/sites/default/files/presentation_pdf/QtDevDaysSFO-2013_WrappingCppForQml_final.pdf":https://www.qtdeveloperdays.com/2013/sites/default/files/presentation_pdf/QtDevDaysSFO-2013_WrappingCppForQml_final.pdf

    e.g.
    @
    class QMLNetwork : public QObject
    {
    public:
    Q_OBJECT

    public:
    QMLNetwork(QObject *parent = 0);
    ~QMLNetwork();

    Q_INVOKABLE void callMessage() { this->_network->callMessage()}
    Q_INVOKABLE void message() { qDebug() << "Network::message();"; }
    

    private:
    Network *_network;
    };
    @

    I know this should work in practice, I think this would prevent me from using QML to override methods.

    I thought of creating a wrapper inheriting from the original type to provide a new interface similar to Boost Python. However, I don't think this would work going from a c++ instance to a qml instance.

    Does anyone have any thoughts?

    Please let me know if anything needs clarifying.

    Cheers,
    Luke



  • May be some more details are required. I did not understand what do you mean by "using QML to override methods". What do you mean here ? You are writing a class in C++ and exposing to QML. You can create instance of these classes in QML. After this what would you like to do in QML with these instances ? This may help or some good sample on what you are trying at QML will help.



  • Hi,

    So in QML, we use the Network object as a prototypes orinherit from this to modify the behaviour such as changing the message method.. e.g.

    @
    import myapi

    Network {
    id: myNetwork
    function message() { console.log("calling qml Network.mymesssage()") }
    }
    @

    If the callMessage is called from c++ for this QML Object instance, the qml message method is called.

    This is trivial if it's a one-one mapping using qmlRegisterType, but using an additional wrapper I don't think I can achieve this. Maybe I'm asking too much...



  • If you write a signal in your Network c++ class and emit it doesn't solve your problem?
    @
    class Network : public QObject
    {
    public:
    Q_OBJECT

    public:
    Network(QObject *parent = 0);
    ~Network();
    signals:
    void message(message);
    };@

    so, in qml:

    @import myapi

    Network {
    id: myNetwork
    onMessage: {console.log(message)}
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.