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. Using C++ as Value of List Element in QML
Forum Updated to NodeBB v4.3 + New Features

Using C++ as Value of List Element in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 350 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jweb1
    wrote on last edited by
    #1

    Qt Quick Version : 2
    Qt Version: 6.4

    Hi all,
    I'm wanting to display many CANbus signals that come from a C++ class in a list format on a QML screen.

    I'm using a ListView with a ListModel that is populated with the value from the C++ class. When using text the output is what I want, however, when I replace with the C++ class value, I get the following error:
    3725643e-3dfc-4f1a-a37c-2b2133986763-image.png
    79ee2359-e2b7-4341-99ec-13ee15b10c7a-image.png
    ab1acda6-f280-41c4-826a-01f4f7e53db4-image.png

    Any help or potential alternative approaches is greatly appreciated.
    jweb1

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anumas
      wrote on last edited by
      #2

      Hi, looks like you haven't read the documentation yet: https://doc.qt.io/qt-6/qml-qtqml-models-listelement.html

      It clearly says what type of values ListElement accept: Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

      From 5.11 ListElement accepts callable roles: Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions.

      What you should do is to create a class subclassing from QAbstractListModel to fetch your data and then pass it to QML. Or use ListModel dynamically without declaring any ListElement to manipulate it through its API.

      https://doc.qt.io/qt-6/qabstractlistmodel.html
      https://doc.qt.io/qt-6/qml-qtqml-models-listmodel.html

      Say hello to a bright day.-

      Anumas.

      1 Reply Last reply
      1

      • Login

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