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. dataChange in QAbstractListModel doesn't work?
Forum Update on Monday, May 27th 2025

dataChange in QAbstractListModel doesn't work?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 1.1k 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by Alien
    #1

    Hello,
    I implement a kind of custom model with QAbstractListModel and assign it to the Repeater in QML unfortunately when i update the model QML side cannot be aware of that and my data couldn't update.

    class CTransparentModel: public QAbstractListModel
    {
        //    Q_OBJECT
    private:
        int m_length;
    public:
        /*quint16 due to value of UserRole which is 256(0x100)*/
        enum class ECTRANSPARENT_MODEL_ROLE : quint16
        {
            Role_Lenght = Qt::UserRole + 1,
            ECTRANSPARENT_MODEL_ROLE_LENGHT,
        };
        void updateHappened();
        CTransparentModel(QObject *parent = nullptr);
        bool setTransparentModelLenght(const int& newLength);
    
        // QAbstractItemModel interface
    public:
    
        int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
        QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
        bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE;
    };
    

    and implementation is:

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    CTransparentModel::CTransparentModel(QObject *parent):QAbstractListModel (parent)
    {
        m_length = 0;
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    void CTransparentModel::updateHappened()
    {
        QModelIndex br=createIndex(m_length-1,0);
        QModelIndex tl=createIndex(0,0);
        /*Does it have any problem*/
        emit dataChanged(tl,br);
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    bool CTransparentModel::setTransparentModelLenght(const int &newLength)
    {
        m_length = newLength;
        QModelIndex br=createIndex(m_length-1,0);
        QModelIndex tl=createIndex(0,0);
        /*Doesn't work*/
        emit dataChanged(tl,br);
        return true;
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    int CTransparentModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return m_length;
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    QVariant CTransparentModel::data(const QModelIndex &index, int role) const
    {
        if(index.row() < 0 || index.row() >= m_length)
        {
            return false;
        }
        if(role == static_cast<int>(ECTRANSPARENT_MODEL_ROLE::Role_Lenght))
        {
            return QVariant::fromValue(m_length);
        }
           return  QVariant();
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    bool CTransparentModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if(index.row() < 0 || index.row() >= m_length)
        {
            return false;
        }
    
        if(role == static_cast<int>(ECTRANSPARENT_MODEL_ROLE::Role_Lenght))
        {
            m_length = value.value<int>();
        }
        else
        {
            return  false;
        }
        emit dataChanged(index,index);
        return true;
    }
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    Qt::ItemFlags CTransparentModel::flags(const QModelIndex &index) const
    {
        if (!index.isValid())
            return Qt::ItemIsEnabled;
    
        return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }
    

    QML side :

    import QtQuick 2.9
    
    Item {
    
        Repeater
        {
            model:TRANS_MODEL
            Rectangle
            {
                x:0
                y:index*25
                width: 100
                height: 20
                color: "lime"
                Text
                {
                    id: name
                    text: CPM.keyName(0,index)
                    anchors.centerIn: parent
                }
            }
        }
    
    }
    
    

    I implement it in UBUNTU16.0.4LTS and QT Creator4.3.1 Based on QT5.9.1

    What am I missing?

    I appreciate any help.

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

      Hi,

      How are you creating that model ?
      Where does it get modified ?

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

      AlienA 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How are you creating that model ?
        Where does it get modified ?

        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by Alien
        #3

        Dear @SGaist,
        Indeed this code doesn't modify any data I have a kind of nested and a little bit complex data hierarchy and I try to introduce simple model to qml in order to make my model be updated.I want to use it as a trick to qml.
        The main problem is that I have keys and labels that they are inside menus they are inside pages and pages are inside a page manager now I want to update something like status (enable,visibility,...),name,etc of keys if I create keymodel that is derived from QAbstractListModel all of my classes need to change properly so I decided to introduce a simple model to qml (this model doesn't have any data but report any chages that happend in undelaying data) and register this class to my "page manager" class so when something change in my data,
        "page manager" class invoke "updateHappened" function of my model to virtually this simple model update itself therefore Qml update its data, with that I expect that Qml find changes and finally update itself but it is never happend till now and I don't know why?

        Sorry for long story :)

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

          Hence my questions:
          How are you creating and managing it ? From C++ ? From QML ?

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

          AlienA 1 Reply Last reply
          0
          • SGaistS SGaist

            Hence my questions:
            How are you creating and managing it ? From C++ ? From QML ?

            AlienA Offline
            AlienA Offline
            Alien
            wrote on last edited by
            #5

            Dear @SGaist ,
            From C++ I manage it and just show in QML

            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