Using C++ as Value of List Element in QML
-
Qt Quick Version : 2
Qt Version: 6.4Hi 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:
Any help or potential alternative approaches is greatly appreciated.
jweb1 -
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 useListModel
dynamically without declaring anyListElement
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