Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ QML Wrappers wise or not
Forum Updated to NodeBB v4.3 + New Features

C++ QML Wrappers wise or not

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeparry
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lukeparry
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jcbritobr
          wrote on last edited by
          #4

          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)}
          }@

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved