Unsolved QAbstractListModel methods from QML
-
Hello there!
I'm trying to find out a way to call QAbstractListModel methods from QML in the Delegate file. The aforementioned means that I can not use
setContextProperty
approach (I have model inside model and so on). I can not usemodel
in Delegate's file cause it works withQQmlDMAbstractItemModelData
and only withdata(...)
andsetData(...)
methods. So my question is: how to do that? -
What exactly do you want to achieve ?
What methods do you want to call from QML ? -
@Karim Well, I have several things that I need to implement. E.g., I need to emit signal in my QAbstractListModel, where argument is a parameter that is stored in the list item. All my items that are stored in the QAbstractListModel aren't QObjects, so I cannot use their signals and slots.
-
Could you please post an example code-snippet of what you need to implement ?
-
@Karim I have tried to reduce unuseful information and code
struct DeviceParameter { ParameterType m_type; QString m_name; QString m_comment; QVariant m_value; ParameterID m_parameterID; ParameterDisplayRole m_parameterDisplayRole; }
class DeviceParametersList: public QAbstractListModel { .... // All these methods are implemented virtual int rowCount(const QModelIndex& parent) const override; virtual QVariant data(const QModelIndex& index, int role) const override; virtual bool setData(const QModelIndex& index, const QVariant& value, int role) override; virtual QHash<int, QByteArray> roleNames() const override; signals: // This signals I want to emit from QML Q_INVOKABLE void readParameter(ParameterID parameterID) const; Q_INVOKABLE void writeParameter(ParameterID parameterID) const; private: QList<DeviceParameter> m_parametersList; };
So I display my
DeviceParameter
list in QML and have two buttons in the each delegate (read and write buttons). I want to callreadParameter
orwriteParameter
method with argument. This argument should be taken from the deletage's item, where read or write button was clicked. -
readParameters()
andwriteParameters()
. Are they signals or methods ?
If they're signals, you'd better create a signal in QML, and connect it to the C++ side
It's explained here: Connecting to QML signalsIf you plan to use
ParameterID
data type in QML side, don't forget to register it properly in the C++ side.K.