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. QAbstractListModel methods from QML
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel methods from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 689 Views
  • 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by St.Stanislav
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Karim
      wrote on last edited by
      #2

      Hi @St-Stanislav

      What exactly do you want to achieve ?
      What methods do you want to call from QML ?

      S 1 Reply Last reply
      1
      • K Karim

        Hi @St-Stanislav

        What exactly do you want to achieve ?
        What methods do you want to call from QML ?

        S Offline
        S Offline
        St.Stanislav
        wrote on last edited by
        #3

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

        K 1 Reply Last reply
        0
        • S St.Stanislav

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

          K Offline
          K Offline
          Karim
          wrote on last edited by
          #4

          @St-Stanislav

          Could you please post an example code-snippet of what you need to implement ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            St.Stanislav
            wrote on last edited by
            #5

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

            K 1 Reply Last reply
            0
            • S St.Stanislav

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

              K Offline
              K Offline
              Karim
              wrote on last edited by
              #6

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

              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