Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why IntMode::data function is always called while the mouse is hover over the view?



  • I do not understand why Data function is called while the mouse is hover over the view? The data function is overriden function for QAbstractListModel.

    #ifndef INTMODEL_H
    #define INTMODEL_H
    
    #include <QAbstractListModel>
    #include <QDebug>
    
    class IntModel : public QAbstractListModel
    {
        Q_OBJECT
    
    public:
        explicit IntModel(int count, QObject *parent = nullptr);
    
        // Basic functionality:
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    
        Qt::ItemFlags flags(const QModelIndex &index) const override;
        bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    private:
        QList<int> m_values;
    };
    
    #endif // INTMODEL_H
    
    
    #include "intmodel.h"
    
    IntModel::IntModel(int count, QObject *parent) : QAbstractListModel(parent)
    {
        for (int i=0; i < count; ++i)
            m_values << i+1;
    }
    
    int IntModel::rowCount(const QModelIndex &parent) const
    {
        // For list models only the root node (an invalid parent) should return the list's size. For all
        // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
        if (parent.isValid())
            return 0;
        return  m_values.count();
    }
    
    QVariant IntModel::data(const QModelIndex &index, int role) const
    {
        qDebug() << "IntModel data called.....";
    
        if (!index.isValid())
            return QVariant();
    
        if(role != Qt::DisplayRole )// && role != Qt::EditRole )
            return QVariant();
    
        if( index.column() == 0 && index.row() < m_values.count() )
            return m_values.at( index.row() );
        else
            return QVariant();
    }
    
    Qt::ItemFlags IntModel::flags( const QModelIndex &index ) const
    {
        if(!index.isValid())
            return Qt::ItemIsEnabled;
        return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
    }
    
    bool IntModel::setData( const QModelIndex &index, const QVariant &value, int role )
    {
        if(role != Qt::EditRole || index.column() != 0 || index.row() >= m_values.count())
            return false;
        if(value.toInt() == m_values.at( index.row()))
            return false;
        m_values[index.row()] = value.toInt();
        emit dataChanged(index, index);
        return true;
    }
    
    
        schemListView = new QListView;
        intModel = new IntModel(25);
        schemListView ->setModel(intModel);
    

    0_1538747556872_2a5fb5d2-e85b-43dc-87fb-4bb99a1d9c0e-image.png


  • Qt Champions 2019

    When you also output the role I would guess it's the ToolTip role.



  • @Christian-Ehrlicher . No it is not tool-tip role. It is first statement inside IntModel::data function. It is debug statment. But this means on every hover of list view elements, this data function is continously called. I do not understand why this function is called.


  • Qt Champions 2019

    Once again: When you add the role to your debug output you will for sure notice that the data for Qt::ToolTipRole is requested (and maybe Qt::BackgroundRole / TextRole because the item is repainted)


Log in to reply