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. TableView delegate
Forum Updated to NodeBB v4.3 + New Features

TableView delegate

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 387 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.
  • B Offline
    B Offline
    Babs
    wrote on last edited by
    #1

    Hello.I'm new at QML. I'm building an api that display data from external source in tabular from. I created my on model and my class representing the data souce. The problem is i can't display my message for now. You'll find my code below. Anyone can help pls?

    //CanMessage.h
    #ifndef CANMESSAGE_H
    #define CANMESSAGE_H
    
    #include <QObject>
    
    
    class CanMessage:public QObject
    {
        Q_OBJECT
        Q_PROPERTY(quint32 identifier READ getID WRITE setID NOTIFY IDChanged)
        Q_PROPERTY(QString payload READ getData WRITE setData NOTIFY dataChanged)
        Q_PROPERTY(int length READ getLength WRITE setLength NOTIFY lengthChanged)
        Q_PROPERTY(qint64 time READ getTimeStamp WRITE setTimeStamp NOTIFY timeStampChanged)
    
    public:
        explicit CanMessage(QObject *parent=nullptr);
        CanMessage(quint32 ,QString ,int ,qint64 );
        quint32 getID() const ;
        QString  getData() const;
        int getLength() const ;
        qint64  getTimeStamp() const ;
        void setID(quint32 ID);
        void setLength(int length);
        void setData(const QString data);
        void setTimeStamp(qint64 time);
    signals:
        void IDChanged(quint32 identifier);
    
        void dataChanged(QString payload);
    
        void lengthChanged(int length);
    
        void timeStampChanged(qint64 time);
    
    public slots:
    
    private:
        quint32 m_identifier;
        QString m_payload;
        int m_lenght;
        qint64   m_time;
        quint32 m_identifer;
        int m_length;
    };
    #endif // CANMESSAGE_H
    
    
    //CanMessageModel.h
    #ifndef CANMESSAGEMODEL_H
    #define CANMESSAGEMODEL_H
    
    #include "cancontroller.h"
    
    class CanMessageModel : public QAbstractTableModel
    {
        Q_OBJECT
        Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource NOTIFY dataSourceChanged)
    
        enum CanMessageDetails{
            idRole=Qt::UserRole,
            dataRole=Qt::UserRole+1,
            sizeRole=Qt::UserRole+2,
            timeRole=Qt::UserRole+3
        };
    public:
        explicit CanMessageModel(QObject *parent=nullptr);
        int rowCount(const QModelIndex &index=QModelIndex()) const;
         int columnCount(const QModelIndex &index=QModelIndex()) const ;
        QVariant data(const QModelIndex &index,int role=Qt::DisplayRole) const;
        Qt::ItemFlags flags(const QModelIndex &index) const;
        bool setData(const QModelIndex &index, const QVariant &value,int role);
        void setDataSource(CanController * dataSource);
        QHash<int,QByteArray> roleNames() const;
        CanController *dataSource() const;
    
    signals:
    
        void dataSourceChanged(CanController* dataSource);
    
    private:
        // QList<CanMessage*> m_canMessages;
        CanController *m_dataSource;
        bool m_signalConnected;
    };
    //Q_DECLARE_METATYPE(CanMessageModel)
    #endif // CANMESSAGEMODEL_H
    
    
    //CanController.h (dataSource)
    #ifndef CANCONTROLLER_H
    #define CANCONTROLLER_H
    
    #include <QObject>
    #include <QVariantMap>
    #include <QtSerialBus/QCanBus>
    #include <QDebug>
    #include "canmessage.h"
    #include <QAbstractListModel>
    #include <QVector>
    
    class CanController:public QObject
    {
        Q_OBJECT
    
    public:
        explicit CanController(QObject *parent=nullptr);
    
    QMap<QString,CanMessage*> dataItems() const;
    void addMessage(CanMessage *msg);
    Q_INVOKABLE void addMessage( const quint32 &identifier,const QString &data,const int &length,const qint64 atime);
    
    signals:
        void preMessageAdded();
        void postMessageAdded();
        void preMessageRemoved();
        void postMessageRemoved();
        void messagesChanged();
        void modelChanged();
    
    public slots:
        Q_INVOKABLE void connectDevice();
        Q_INVOKABLE void processReceivedFrames();
        void disconnectCANDevice();
    
    private:
        QMap<QString,CanMessage*>m_messages;
        QCanBusDevice *m_device;
        //CanMessageModel *m_model;
    };
    #endif // CANCONTROLLER_H
    
    
    //main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml/QQmlContext>
    #include <QtQml/QQmlEngine>
    
    #include "cancontroller.h"
    #include "canmessagemodel.h"
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        CanMessageModel myModel;
        CanController *ds=new CanController(&myModel);
        //qmlRegisterType<CanMessageModel>("TableModel", 0, 1, "TableModel");
        engine.rootContext()->setContextProperty("canControl", ds);
        engine.rootContext()->setContextProperty("myModel", &myModel);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    
    //main.qml
    
    //import QtQuick 2.12
    //import QtQuick.Window 2.12
    //import QtQuick.Controls 2.12
    
    //Window{
    //    id: win
    //    visible: true
    //    height: 480
    //    width: 800
    //    title: qsTr("Can Sniffer")
    
    //    Item{
    //        id:_root
    //        anchors.right: parent.right
    //        anchors.left: parent.left
    //        anchors.top:parent.top
    //        anchors.bottomMargin: 0
    
    //        Item{
    //            id:controls
    //            height: 50
    //            anchors.top: parent.top
    //            anchors.topMargin: 0
    //            anchors.left: parent.left
    //            anchors.leftMargin: 0
    //            anchors.right: parent.right
    //            anchors.rightMargin: 0
    
    //            Button{
    //                id:connectButton
    //                anchors.leftMargin: 8
    //                anchors.verticalCenter: parent.verticalCenter
    //                text:qsTr("Connect Device")
    //                onClicked: {
    //                    canControl.connectDevice()
    //                }
    //            }
    //            Item{
    //                id:messageList
    //                width: parent.width
    //                anchors.top: controls.bottom
    //                anchors.bottom: parent.bottom
    //                anchors.left: parent.left
    //                anchors.topMargin: 0
    
    //                Item{
    //                    id:listHead
    //                    height: 20
    //                    width:parent.width
    //                    Text{
    //                        id: idHead
    //                        width: 60
    //                        text: "ID"
    //                        clip: false
    //                    }
    //                    Text{
    //                        id:payloadHead
    //                        text: "Payload"
    //                        anchors.rightMargin: 64
    //                        anchors.left: idHead.right
    //                        anchors.leftMargin: 64
    //                    }
    //                    Text{
    //                        id:length
    //                        text:"length"
    //                        anchors.left: payloadHead.right
    //                        //anchors.rightMargin: 16
    //                        anchors.leftMargin: 128
    //                    }
    //                    Text{
    //                        text: qsTr("time Stamp")
    //                        anchors.left: length.right
    //                        anchors.leftMargin: 128
    //                        anchors.right: parent.right
    //                    }
    //                }
    //            }
    //            ListView{
    //                clip: true
    //                anchors.right: parent.right
    //                anchors.bottom: parent.bottom
    //                anchors.left: parent.left
    //                anchors.topMargin: 8
    
    //                model:myModel
    
    //                delegate: Item{
    //                    id:item1
    //                    height: parent.height
    //                    width: parent.width
    //                    Text{
    //                        id:id
    //                        width: 60
    //                        text:model.identifier//Number(identifier).toLocaleString(Qt.locale("de_DE"))
    //                    }
    //                    Text {
    //                        id: data
    //                        text: model.payload
    //                        anchors.leftMargin: 64
    //                        anchors.left: id.right
    //                    }
    //                    Text {
    //                        id: lengthText
    //                        text: model.length//Number(length).toLocaleString(Qt.locale("de_DE"))
    //                        anchors.left: data.right
    //                        anchors.leftMargin: 128
    //                    }
    //                    Text {
    //                        id: timeStamp1
    //                        text:model.timeStamp//Number(timeStamp).toLocaleString(Qt.locale("de_DE"))
    //                        anchors.left: lengthText.right
    //                        anchors.leftMargin: 128
    //                    }
    //                }
    //            }
    //        }
    //    }
    
    //}
    
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    import TableModel 0.1
    
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Item{
            id:_root
            anchors.right: parent.right
            anchors.left: parent.left
            anchors.top:parent.top
            anchors.bottomMargin: 0
    
            Button{
                id:connectButton
                anchors.leftMargin: 8
                anchors.verticalCenter: parent.verticalCenter
                text:qsTr("Connect Device")
                onClicked: {
                    canControl.connectDevice()
                }
            }
    
            TableView {
                anchors.top: _root.bottom
                anchors.fill: parent
                columnSpacing: 1
                rowSpacing: 1
                clip: true
    
                model: TableModel{}
    
                delegate: Rectangle {
                    implicitWidth: 150
                    implicitHeight: 50
                    border.color: "black"
                    border.width: 2
                    //color: (heading==true)?"red":"green"
    
                    //TableView.onPooled: console.log(tabledata + " pooled")
                    //TableView.onReused: console.log(tabledata + " resused")
    
                    Text {
                        text:display
                        font.pointSize: 12
                        anchors.centerIn: parent
                    }
                }
            }
        }
    
    }
    
    
    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @Babs ,

      my best suggestion would be use QAbstractListModel instead of QObject as a base class.

      for example:- Class MyModel: public QAbstractListModel

      And then you expose it to QML as you are doing currently.
      qRegisterMetaType<MyModel*>();

      After that you can access the model in QML as follows:-

      readonly property QtObject _yourModel:  myModel
      TableView {
              anchors.top: _root.bottom
              anchors.fill: parent
              columnSpacing: 1
              rowSpacing: 1
              clip: true
      
              model: _yourModel
              ....
              ....
      }
      

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      1 Reply Last reply
      0

      • Login

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