Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5 - Display dynamic Data Model in QML TableView

Tags:

c++

qt

qt5

qml

I'm developing a trace window for a GUI. I'm using a TableView element on the QML side to display data that will be constantly updating. How can I populate this element with data? The numbers of elements changes, along with each element's data, every few milliseconds.

I'm thinking a signals/slots implementation would be ideal, when the data changes, produce a signal that triggers a slot function to update the values shown in the TableView? Something along those lines.

Thanks in advance!

main.qml

import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.1
import QtQuick 2.1

....
TableView {
                anchors.fill: parent
                id: traceTable
                //table data comes from a model
                model: traceTableModel
                //Component.onCompleted: classInstance.popAndDisplayMsg(classInstance)
                TableViewColumn { role: "index"; title: "Index";  width: 0.25 * mainWindow.width; }
                TableViewColumn { role: "type"; title: "Type"; width: 0.25 * mainWindow.width; }
                TableViewColumn { role: "uid"; title: "ID"; width: 0.25 * mainWindow.width; }
                TableViewColumn { role: "timestamp"; title: "Timestamp"; width: 0.25 * mainWindow.width; }


            }
....

main.cpp

#include "class_header.hpp"
#include <QtQuick/QQuickView>
#include <QGuiApplication>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView view;

    class_name instance;

    view.rootContext()->setContextProperty("classInstance", &instance);

    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl("qml/main.qml"));
    view.show();
    return app.exec();
}

class_header.hpp

#ifndef class_name_HPP
#define class_name_HPP

#include <QtQuick/QQuickItem>
#include <polysync_core.h>
#include <glib.h>
#include <QString>
#include <QDebug>


class class_name : public QQuickItem
{
    Q_OBJECT
    //Maybe some Q_Properties here?

    public:

        //constructor
        class_name(QQuickItem *parent = 0);
        //deconstructor
        ~class_name();

    signals:
        void dataChanged();

    public slots:
        int updateInfo(//pass some data);

};

#endif // class_name_HPP
like image 830
davepmiller Avatar asked Nov 01 '22 06:11

davepmiller


1 Answers

Your use of the model from the QML is weird. You don't want to use custom roles for every column. This makes no sense. Neither do you need to have custom QQuickItem classes.

The basic process is:

  1. Properly implement a class deriving from QAbstractListModel or QAbstractTableModel.

  2. Bind the instance of such a class to the model of a QML View.

Here are complete (as in compile-and-run) references for your perusal:

  • Qml 2.0 TableView with QAbstractItemModel and Context Menu

  • How to Use Models with QML?

  • Remove rows from QAbstractListModel

like image 166
Kuba hasn't forgotten Monica Avatar answered Nov 10 '22 01:11

Kuba hasn't forgotten Monica