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. QMap and data model.
Forum Updated to NodeBB v4.3 + New Features

QMap and data model.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
20 Posts 3 Posters 3.0k Views 2 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.
  • AlienA Alien

    Hey,
    Could you please explain more about your problem?
    You can have your own setdata and getdata to set and get data that's enough to emit the right signals to refresh new data.

    B Offline
    B Offline
    Babs
    wrote on last edited by
    #3

    @Alien I'm trying to create a model of data based on a QMap. The problem is i have difficulties to acess the index of an objects in the QMap. Therefore i have difficulties implementing my data and setData functions below:

    //QVariant CanMessageModel::data(const QModelIndex &index, int role) const
    {
        if (index.row() < 0 || index.row() >= m_dataSource->dataItems().size())
            return QVariant();
        CanMessage *message= m_dataSource->dataItems().at(index.row());
        if(role == idRole)
            return message->getID();
        else if(role == dataRole)
            return message->getData();
        else if(role == sizeRole)
            return message->getLength();
        else if(role == timeRole)
            return message->getTimeStamp();
        return QVariant();
    }
    
    1 Reply Last reply
    0
    • AlienA Offline
      AlienA Offline
      Alien
      wrote on last edited by Alien
      #4

      @Babs you've done that; what's the difficulty?
      And i don't know why you define
      Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource NOTIFY dataSourceChanged)

      CanController should be a qvariant is that works fine? In the other hand why you need Q_PROPERTY?

      B 1 Reply Last reply
      0
      • AlienA Alien

        @Babs you've done that; what's the difficulty?
        And i don't know why you define
        Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource NOTIFY dataSourceChanged)

        CanController should be a qvariant is that works fine? In the other hand why you need Q_PROPERTY?

        B Offline
        B Offline
        Babs
        wrote on last edited by
        #5

        @Alien this is not working at all this is the function i wrote for a QList<CanMessage*>. Now i'm trying to do it with a QMap

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Hi,

          Unless you are mapping an int to your CanMessage*, I don't see how you expect to use a QMap in a list model.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Unless you are mapping an int to your CanMessage*, I don't see how you expect to use a QMap in a list model.

            B Offline
            B Offline
            Babs
            wrote on last edited by
            #7

            @SGaist My CanMessage class contain a lot of information like the ID of my data. I don't want to add to my Model message wich ID is already present. I want to refresh it. That's the reason why i try to use QMap::insert() in order to not add messages with existing identifiers

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              What type is ID ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              B 1 Reply Last reply
              0
              • SGaistS SGaist

                What type is ID ?

                B Offline
                B Offline
                Babs
                wrote on last edited by
                #9

                @SGaist quint32. I use QCanBusFrame to get my data

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  And you would like to show them ordered by that ID ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  B 2 Replies Last reply
                  0
                  • SGaistS SGaist

                    And you would like to show them ordered by that ID ?

                    B Offline
                    B Offline
                    Babs
                    wrote on last edited by
                    #11

                    @SGaist not necessary i just want to replace messages with existing ID with new one. I don't want to add them to my list

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      And you would like to show them ordered by that ID ?

                      B Offline
                      B Offline
                      Babs
                      wrote on last edited by
                      #12

                      @SGaist I created my list model based on Qmap. The problem know is when my data update i get the error: Unable to assign [undefined] to QString.
                      Here is what i get:
                      0_1551347754150_qmap.PNG

                      When a new message with existing ID is received the data should be modified in my display.
                      The problem is the first received message is displayed and when messages are updated is says:
                      Unable to assign [undefined] to QString.

                      For example in first line i have 2ff as ID, and 2A as data. If i receive a new message with the ID 2ff and data 2B, 2A should be replaced with 2B in that line

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        Can you show how you implemented setData ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        B 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Can you show how you implemented setData ?

                          B Offline
                          B Offline
                          Babs
                          wrote on last edited by
                          #14

                          @SGaist of course

                          //your code here
                          bool CanMessageModel::setData(const QModelIndex &index, const QVariant &value, int role)
                          {
                              CanMessage *message= m_dataSource->dataItems().values().at(index.row());
                              //    CanMessage *message= m_dataSource->dataItems().at(index.row());
                              //CanMessage *message= (m_dataSource->dataItems().constBegin()+index.row()).value();
                          
                          
                              bool somethingChanged=false;
                          
                              switch (role) {
                              case idRole:{
                                  if(message->getID()!=value.toString()){
                                      message->setID(value.toString());
                                      somethingChanged=true;
                          
                                  }
                              }
                                  break;
                              case dataRole:{
                                  if(message->getData()!=value.toString()){
                                      message->setData(value.toString());
                                      somethingChanged=true;
                          
                                  }
                              }
                                  break;
                              case sizeRole:{
                                  if(message->getLength()!=value.toString()){
                                      message->setLength(value.toString());
                                      somethingChanged=true;
                          
                                  }
                              }
                                  break;
                              case timeRole:{
                                  if(message->getTimeStamp()!=value.toUInt()){
                                      message->setTimeStamp(value.toUInt());
                                      somethingChanged=true;
                          
                                  }
                              }
                                  break;
                              case periodRole:{
                                  if(message->getPeriod()!=value.toUInt())
                                      message->setPeriod(value.toUInt());
                                  somethingChanged=true;
                              }
                          
                          
                              }
                              if(somethingChanged){
                                  emit dataChanged(index,index,QVector<int>()<<role);
                                  return true;
                              }
                              return false;
                          
                          }
                          
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            Something is a bit strange here...

                            You want to order your message by idea, yet there's no real sign of a list to order. You seem to have a pretty convoluted setup right now.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            B 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Something is a bit strange here...

                              You want to order your message by idea, yet there's no real sign of a list to order. You seem to have a pretty convoluted setup right now.

                              B Offline
                              B Offline
                              Babs
                              wrote on last edited by
                              #16

                              @SGaist thank you for your answers. I successed in creating my model like i wanted. I used a QAbstractTable model and stored my data in a QMap.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                So everything is working now ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                B 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  So everything is working now ?

                                  B Offline
                                  B Offline
                                  Babs
                                  wrote on last edited by
                                  #18

                                  @SGaist Yes

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    What did you do to fix your issue ?

                                    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    B 1 Reply Last reply
                                    0
                                    • SGaistS SGaist

                                      What did you do to fix your issue ?

                                      Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)

                                      B Offline
                                      B Offline
                                      Babs
                                      wrote on last edited by
                                      #20

                                      @SGaist here is my code
                                      I created a QAbstractListModel based on tableView

                                      //your code here
                                      #ifndef CANMESSAGEMODEL_H
                                      #define CANMESSAGEMODEL_H
                                      
                                      #include "cancontroller.h"
                                      #include "canmessage.h"
                                      
                                      class CanMessageModel : public QAbstractListModel
                                      {
                                          Q_OBJECT
                                          Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource )
                                      
                                          enum CanMessageDetails{
                                              idRole=Qt::UserRole,
                                              dataRole=Qt::UserRole+1,
                                              sizeRole=Qt::UserRole+2,
                                              timeRole=Qt::UserRole+3,
                                              periodRole=Qt::UserRole+4,
                                              nodeIDRole=Qt::UserRole+5,
                                              fctTypeRole=Qt::UserRole+6
                                          };
                                      public:
                                          explicit CanMessageModel(QObject *parent=nullptr);
                                          int rowCount(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);
                                          Q_INVOKABLE void setDataSource(CanController * dataSource);
                                          QHash<int,QByteArray> roleNames() const;
                                          CanController *dataSource() const;
                                      
                                      signals:
                                      
                                      
                                      private:
                                          // QList<CanMessage*> m_canMessages;
                                          CanController *m_dataSource;
                                          bool m_signalConnected;
                                      };
                                      //Q_DECLARE_METATYPE(CanMessageModel)
                                      #endif // CANMESSAGEMODEL_H
                                      
                                      
                                      //model.cpp
                                      #include <QDebug>
                                      #include "canmessagemodel.h"
                                      
                                      CanMessageModel::CanMessageModel(QObject *parent):QAbstractListModel (parent),m_signalConnected(false)
                                      {
                                          setDataSource(new CanController(this));
                                      }
                                      
                                      int CanMessageModel::rowCount(const QModelIndex &index) const
                                      {
                                          Q_UNUSED(index)
                                          return m_dataSource->dataItems().size();
                                      }
                                      
                                      
                                      QVariant CanMessageModel::data(const QModelIndex &index, int role) const
                                      {
                                          Q_UNUSED(role)
                                          if (index.row() < 0 || index.row() >= m_dataSource->dataItems().size())
                                              return QVariant();
                                          CanMessage *message= m_dataSource->dataItems().values().at(index.row());
                                      
                                          if(role == idRole)
                                              return message->getID();
                                          else if(role == dataRole)
                                              return message->getData();
                                          else if(role == sizeRole)
                                              return message->getLength();
                                          else if(role == timeRole)
                                              return message->getTimeStamp();
                                          else if(role == periodRole)
                                              return message->getPeriod();
                                          else if(role ==nodeIDRole)
                                              return message->nodeId();
                                          else if(role ==fctTypeRole)
                                              return message->fct_Type();
                                          return QVariant();
                                      }
                                      
                                      
                                      
                                      Qt::ItemFlags CanMessageModel::flags(const QModelIndex &index) const
                                      {
                                          if (!index.isValid())
                                              return Qt::NoItemFlags;
                                          return Qt::ItemIsEditable;
                                      }
                                      
                                      QHash<int, QByteArray> CanMessageModel::roleNames() const
                                      {
                                      
                                          QHash<int, QByteArray> roles;
                                          roles[idRole] = "identifier";
                                          roles[dataRole] = "payload";
                                          roles[sizeRole] = "size";
                                          roles[timeRole] = "timeStamp";
                                          roles[periodRole]="period";
                                          roles[nodeIDRole]="nodeID";
                                          roles[fctTypeRole]="functionType";
                                          return roles;
                                      }
                                      
                                      bool CanMessageModel::setData(const QModelIndex &index, const QVariant &value, int role)
                                      {
                                          CanMessage *message= m_dataSource->dataItems().values().at(index.row());
                                      
                                          bool somethingChanged=false;
                                      
                                          switch (role) {
                                          case idRole:{
                                              if(message->getID()!=value.toString()){
                                                  message->setID(value.toString());
                                                  somethingChanged=true;
                                      
                                              }
                                          }
                                              break;
                                          case dataRole:{
                                              if(message->getData()!=value.toString()){
                                                  message->setData(value.toString());
                                                  somethingChanged=true;
                                      
                                              }
                                          }
                                              break;
                                          case sizeRole:{
                                              if(message->getLength()!=value.toString()){
                                                  message->setLength(value.toString());
                                                  somethingChanged=true;
                                      
                                              }
                                          }
                                              break;
                                          case timeRole:{
                                              if(message->getTimeStamp()!=value.toString()){
                                                  message->setTimeStamp(value.toString());
                                                  somethingChanged=true;
                                      
                                              }
                                          }
                                              break;
                                          case periodRole:{
                                              if(message->getPeriod()!=value.toString()){
                                                  message->setPeriod(value.toString());
                                                  somethingChanged=true;
                                      
                                              }
                                          }
                                              break;
                                          case nodeIDRole:{
                                              if(message->nodeId()!=value.toString()){
                                                  message->setnodeId(value.toString());
                                                  somethingChanged=true;
                                              }
                                          }
                                              break;
                                          case fctTypeRole:{
                                              if(message->fct_Type()!=value.toString()){
                                                  message->setFct_Type(value.toString());
                                                  somethingChanged=true;
                                              }
                                          }
                                      
                                          }
                                          if(somethingChanged){
                                              emit dataChanged(index,index,QVector<int>()<<role);
                                              return true;
                                          }
                                          return false;
                                      
                                      }
                                      
                                      CanController *CanMessageModel::dataSource() const
                                      {
                                          return m_dataSource;
                                      }
                                      
                                      
                                      
                                      void CanMessageModel::setDataSource(CanController *dataSource)
                                      {
                                          beginResetModel();
                                          if(m_dataSource && m_signalConnected)
                                              m_dataSource->disconnect(this);
                                          m_dataSource=dataSource;
                                      
                                          connect(m_dataSource,&CanController::preMessageAdded,this,[=](){
                                              const int index=m_dataSource->dataItems().size();
                                              beginInsertRows(QModelIndex(),index,index);
                                          });
                                          connect(m_dataSource,&CanController::postMessageAdded,this,[=](){
                                              endInsertRows();
                                          });
                                          connect(m_dataSource,&CanController::preMessageSent,this,[=](){
                                              const int index=m_dataSource->dataItems().size();
                                              beginInsertRows(QModelIndex(),index,index);
                                          });
                                          connect(m_dataSource,&CanController::postMessageSent,this,[=](){
                                              endInsertRows();
                                          });
                                          m_signalConnected=true;
                                          endResetModel();
                                      }
                                      
                                      
                                      

                                      And it qml i create my Tableview with following delegate

                                      //your code here
                                      import QtQuick 2.12
                                      import QtQuick.Controls 2.12
                                      
                                      
                                      
                                      
                                      
                                      Column{
                                      
                                          TableView{
                                              implicitHeight: 200
                                              id : mListView
                                              width: parent.width
                                              height: parent.height
                                      
                                              model : myModel
                                              delegate: Item {
                                                  height: parent.height
                                      
                                                  width: parent.width
                                                  Row{
                                                      //spacing: 0
                                                      anchors.fill: parent
                                                      //anchors.margins:0
                                                      TextField{
                                                          focus: true
                                                          text: model.identifier;
                                                          //wrapMode: Text.WordWrap
                                                          //canUndo: true
                                      
                                                      }
                                      
                                                      TextField{
                                      
                                                          focus: true
                                                          id : m_payload
                                                          text: model.payload
                                      
                                                          // canUndo:  true
                                                      }
                                                      TextField{
                                                          focus: true
                                                          id:m_length
                                                          text: model.size
                                                      }
                                                      TextField{
                                                          focus: true
                                                          text: model.timeStamp
                                                      }
                                                      TextField{
                                                          focus:true
                                                          text: model.period
                                                      }
                                                      TextField{
                                                          focus:true
                                                          text: model.nodeID
                                                      }
                                                      TextField{
                                                          focus:true
                                                          text: model.functionType
                                                      }
                                      
                                                  }
                                      
                                              }
                                      
                                          }
                                      
                                      }
                                      
                                      
                                      1 Reply Last reply
                                      1

                                      • Login

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