Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 use model in Delegate's file cause it works with QQmlDMAbstractItemModelData and only with data(...) and setData(...) methods. So my question is: how to do that?



  • Hi @St-Stanislav

    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.



  • @St-Stanislav

    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 call readParameter or writeParameter method with argument. This argument should be taken from the deletage's item, where read or write button was clicked.



  • @St-Stanislav

    readParameters() and writeParameters(). 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 signals

    If you plan to use ParameterID data type in QML side, don't forget to register it properly in the C++ side.

    K.


Log in to reply