Important: Please read the 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
        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;
        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 index.row() );
            return QVariant();
    Qt::ItemFlags IntModel::flags( const QModelIndex &index ) const
            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() == 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);


  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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