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. Qml/C++ interface and pointers
Forum Updated to NodeBB v4.3 + New Features

Qml/C++ interface and pointers

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 895 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by Cyrille de Brebisson
    #1

    Hello,

    My problem is that I have a C++ "model" (not per standard QT meaning, but a data "chest").
    And I need to create qml controls for each items in my model.
    Obviously, this is a very simplified version of what I am trying to achieve, this example could be handled with a list view and a real model, but in reality my application is much more complex and is not a list view.
    Here is the code that I cam up with. Can you help me modify it to work as I need it to work
    (ie: how do I create a qml custom control that links to my C code using a pointer and not an instance, and how do I write the event manager for the model)?

    Thanks!

    // This class is just a list of myobj with add/delete signals
    class mycontainer : public QObject
    {
    Q_OBJECT
    Q_SIGNALS:
    void moreData(myobj *value);
    void lessData(myobj *value);
    public:
    void add(myobj o) { _data.pushBack(o); emit moreData(&o); }
    void del(myobj *o) { emit lessData(o); _data.removeAll(o); }
    private:
    QList<myobj> _data;
    }

    // this class is my "object" basically just one property called value here
    class myobj : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged)
    Q_SIGNALS:
    void valueChanged(QVariant value);
    public:
    QVariant getValue() { return _value; }
    void setValue(QVariant value);
    private:
    QVariant _value;
    };

    in my qml, I want to add a custom text field that uses myobj.value as a source to my control when more data is added and remove a text field when it gets removed
    // myobjcontrol
    // This is a custom qml object which is just a text field which value is linked toward my C object
    Text {
    myobj { id: obj } // This is an instance of myobj, however, I need it to be a pointer to an existing object. How do I do that?
    text: obj.value
    }

    // Here is the main window. It needs to "monitor" the model and add/remove custom controls when needed
    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    mycontainer {
    id: container
    onMoreData: // What do I do here to add an object in column of type myobjcontrol, which uses the new object as the data source?
    onLessData: // simillary here, how do I find the qml object to remove from my column when this happends?
    }
    Column {
    id: cols

    }
    

    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not wrap your "data chest" with one of Qt's model e.g. QAbstractItemModel ? Because from the looks of it, it seems you are re-inventing the wheel.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cyrille de Brebisson
        wrote on last edited by Cyrille de Brebisson
        #3

        Hello,

        Sorry for the delay in getting back to this...

        If I understand well, what you say is that:

        • my "mycontainer" should become a subclass of QAbstractItemModel (or QAbstractListModel).
        • The myobj will then be "containted" by a QVariant

        But this does not answer my first question: how do I create/delete "GUI" object when an object is added/removed from the Model?

        My application is designed to allow me to control/monitor a number of "sensors" connected to it.
        Each sensor will have a 1 "page"/"tab" GUI, with each sensor type having a different GUI.

        This is what I was trying to describe in my original email. On the C side, I will detect the arrival/departure of sensors. And I need to update the QML GUI based on arrival/departure.
        Each time a sensor arrives, I need to add the proper QML GUI component (linked to a back end C++ object that will send/receive the commands from the sensor).
        Each time a sensor is disconnected, I need to remove the QML GUI component.

        I am planning to subclass my "Sensor" class for each type of sensors.
        I would like each QML gui object to have an attribute which is a link to that specific model type. The aim there would be to have dynamic completion and similar codding niceties available when creating the QML components.

        Cyrille

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No, what I suggested was to create a subclass of QAbstractItemModel that would serve as interface on top of your mycontainer. i.e. MyAbstractItemModel would contain a mycontainer object.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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