Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Item of QAbstractListModel changes type after beeing updated

Item of QAbstractListModel changes type after beeing updated

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 471 Views
  • 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.
  • S Offline
    S Offline
    Slash200
    wrote on last edited by Slash200
    #1

    Hello,

    I found a strange behaviour that makes me problems:

    The item of a QAbstractListModel, (mine's called datasourceobject) changes it's type after being updatet several times.

    In my case the item is updated via this function from the model.

    bool DataSourceModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        DataSourceObject *dataSourceObject = m_DataSourceObjects[index.row()];
        if (data(index, role) != value) {
            if(role == idRole)
                dataSourceObject->setId(value.toInt());
            else if(role == nameRole)
                dataSourceObject->setName(value.toString());
            else if(role == unitRole)
                dataSourceObject->setUnit(value.toString());
            else if(role == valueRole)
                dataSourceObject->setValue(value.toDouble());
            emit dataChanged(index, index, QVector<int>() << role);
            return true;
        }
        return false;
    }
    

    Returning a pointer from the item with this function returns : DataSourceObject(0x1bbb43e0)

    DataSourceObject *DataSourceModel::dataPointer(const QModelIndex &index)
    {
        return m_DataSourceObjects[index.row()];
    }
    

    After updating it several times it changes to:

    QObject(0x1bbb43e0)
    

    and the adress stays the same.

    I need to access single items from the model with QML via pointer to get it automatically updated, and this stops working when the type changes to QObject. Any explanation why this happens?

    regards

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      How does DataSourceModel::setData() look like? Seems you're doing something wrong in there.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Slash200
        wrote on last edited by
        #3

        I changed the post, the function is now there

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

          Hi,

          Do you mean you have the same object in the whole model ?
          How big is that model ?

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

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Do you mean you have the same object in the whole model ?
            How big is that model ?

            S Offline
            S Offline
            Slash200
            wrote on last edited by
            #5

            @SGaist
            Hello,

            there are about 200 items, all of them are of the same type: "DataSourceObject"

            #ifndef DATASOURCEOBJECT_H
            #define DATASOURCEOBJECT_H
            
            #include <QString>
            #include <QVariantMap>
            
            class DataSourceObject : public QObject
            {
                Q_OBJECT
            
                Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged)
            
            public:
            
                explicit DataSourceObject(QObject *parent = nullptr);
                DataSourceObject(const QJsonObject &obj);
                int id() const;
                void setId(int id);
            
                QString name() const;
                void setName(const QString &name);
            
                QString unit() const;
                void setUnit(const QString &unit);
            
                Q_INVOKABLE double value() const;
                void setValue(double value);
                QVariantMap toMap() const;
            
            signals:
                void valueChanged();
            
            
            private:
                int m_id;
                QString m_name;
                QString m_unit;
                double m_value;
            };
            
            #endif // DATASOURCEOBJECT_H
            
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hence my question: do they all point to the same address ? Or are you visualising only one entry ?

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

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                Hence my question: do they all point to the same address ? Or are you visualising only one entry ?

                S Offline
                S Offline
                Slash200
                wrote on last edited by Slash200
                #7

                @SGaist No they don't point to the same address.
                I want to visualize a single item.

                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