Qml/C++ interface and pointers
-
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}
}
-
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.
-
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
-
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 amycontainer
object.