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. How to access C++ Model data directly from QML without any ListView/Repeater/etc ?
Qt 6.11 is out! See what's new in the release blog

How to access C++ Model data directly from QML without any ListView/Repeater/etc ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.1k 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.
  • R Offline
    R Offline
    rnayabed
    wrote on last edited by
    #1

    Hello everyone!
    I am a beginner in Qt and QML. So far I am able to easily access my C++ Models (subclasses of QAbstractListModel) easily from QML via Controls like ListView, Repeater, ComboBox, etc.
    However, I have reached a point where I would like to access data of my model via JavaScript in a QML function. Let's say, I would want to call the data method of my model directly from QML, and pass in the row information. How do i do that?

    Currently, I am having a Q_INVOKABLE method in my C++ code which accepts row number, and returns the appropriate value by invoking data within it.

    Is there a proper way to do this other than this? Initially I used QList instead of models, after reading up on QML Performance Optimisation guide, it looks like that is very performance heavy since it involves copying all at once when dealing with larger custom classes.

    Thank you, please excuse and correct if I said anything wrong.

    GrecKoG 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      What you are doing(Q_INVOKABLE) is the only way to access the model data

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      R 1 Reply Last reply
      0
      • R rnayabed

        Hello everyone!
        I am a beginner in Qt and QML. So far I am able to easily access my C++ Models (subclasses of QAbstractListModel) easily from QML via Controls like ListView, Repeater, ComboBox, etc.
        However, I have reached a point where I would like to access data of my model via JavaScript in a QML function. Let's say, I would want to call the data method of my model directly from QML, and pass in the row information. How do i do that?

        Currently, I am having a Q_INVOKABLE method in my C++ code which accepts row number, and returns the appropriate value by invoking data within it.

        Is there a proper way to do this other than this? Initially I used QList instead of models, after reading up on QML Performance Optimisation guide, it looks like that is very performance heavy since it involves copying all at once when dealing with larger custom classes.

        Thank you, please excuse and correct if I said anything wrong.

        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        QAbstactItemModel::data is already Q_INVOKABLE.

        1 Reply Last reply
        0
        • dheerendraD dheerendra

          What you are doing(Q_INVOKABLE) is the only way to access the model data

          R Offline
          R Offline
          rnayabed
          wrote on last edited by
          #4

          @dheerendra @GrecKo Thank you, I did not know that.

          1 Reply Last reply
          0
          • R rnayabed has marked this topic as solved on

          • Login

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