Using C++ Models in QML {tutorial}

Qt Group · 144K views · 2017 · 49:49

Open on YouTube

Software (other)Web developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the worked example of a C++ list model in QML, then ask follow-up questions about the code it builds.

Recommended by 1 people across 1 different months, 2017 to 2022.

What it is

A 50-minute Qt Group tutorial that builds a to-do list app to show how to write a C++ QAbstractListModel and expose it to a QML ListView. It is split into ten chapters with matching branches in a public Git repository, so viewers can start from any point.

What people said

Recommends NobodySpecial · 2022-01 · Stack Overflow · on “Qml/Qt c++: How to automatically update items in Qml ListView when the underlying Qt c++ ListModel changes?”

Based on an example video from Qt (the ToDo example), I have created a ListView based Qt/Qml application. Data for the ListView in Qml comes from a c++ class based on QAbstractListModel. The c++ model class is populated with data from a database. This all works fine. I have added a pushbutton in the Qml file which invokes a method in the c++ code that fetches data from a remote source (i.e. makes an https request). The https response data is of course asynchronous with the method that makes the http request. Debug lines in the c++ https response handler confirm that the response is received okay. I save the response to the database. If I close the application and re-open it, the new data is

Cites LinG · 2017-12 · Stack Overflow · on “Why use QStringLiteral?”

I recently started with QML and I tried to follow this example [this video] . The video goes over how to create a C++ model that can be shown in your QML application. In the data member function of the model, a switch is used and the string that will be displayed in QML is returned in a QVariant but the tutorial also uses an additional QStringLiteral as you can see in the code below. QVariant ToDoModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); // FIXME: Implement me! switch (role) { case DoneRole: return QVariant(false); case DescriptionRole: return QVariant(QStringLiteral("Test description")); } return QVariant(); } I tried this same piece of code

Cites Dev · 2019-01 · Stack Overflow · on “Can't set text field to fill width in QML”

I am following this [this video] tutorial on YouTube and the person sets the TextField to fill the width of the RowLayout . However, it doesn't seem to work for me. I tried using Layout.fillWidth on the CheckBox and it seems to work perfectly fine but it doesn't seem to want to work on the TextField . Here is my code: main.qml: import QtQuick 2.9 import QtQuick.Controls 2.2 ApplicationWindow { visible: true; width: 640; height: 480; title: qsTr("Tabs"); ToDoList { anchors.centerIn: parent; } } ToDoList.qml: import QtQuick 2.9 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 Frame { ListView { // Using implicit width and height allows the frame to automatically scale to the size of the list

Cites chillpu · 2020-04 · Stack Overflow · on “Qt Quick / QML flag changes in C++ back to UI”

I'm familiar with Qt from some time back, but I could use a little guidence with QML/QTQuick interactions with C++. I feel I'm missing something simple here, but am a little stuck. I'm developing an embedded system displays the status of distributed switches communicating on a serial bus. The serial bus runs as a separate thread in C++ (has always been Qt) and automatically polls devices in a round robin fashion to get updates from the devices. Looking into this, I found a pretty simple example using QAbstractList model to share the status via QML properties from the backed C++. [this video] Initially the model looked great, I simply loaded a list with the information I needed and can see it from