繁体   English   中英

Qt5 - 在QML​​ TableView中显示动态数据模型

[英]Qt5 - Display dynamic Data Model in QML TableView

我正在为GUI开发一个跟踪窗口。 我在QML端使用TableView元素来显示将不断更新的数据。 如何用数据填充此元素? 元素数量以及每个元素的数据每隔几毫秒就会发生变化。

我认为信号/插槽实现是理想的,当数据发生变化时,会产生一个触发插槽函数的信号来更新TableView中显示的值? 沿着那条线的东西。

提前致谢!

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

您使用QML中的模型很奇怪。 您不希望为每列使用自定义角色。 这毫无意义。 您也不需要自定义QQuickItem类。

基本过程是:

  1. 正确实现从QAbstractListModelQAbstractTableModel派生的类。

  2. 将此类的实例绑定到QML视图的模型。

以下是完整的(如在编译和运行中)参考文献供您阅读:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM