Why IntMode::data function is always called while the mouse is hover over the view?
Solved
General and Desktop
-
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);
-
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.
-
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)