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. Why IntMode::data function is always called while the mouse is hover over the view?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
qabstractlistmo
4 Posts 2 Posters 614 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.
  • M Offline
    M Offline
    milan
    wrote on last edited by milan
    #1

    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

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

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

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

      M 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

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

        M Offline
        M Offline
        milan
        wrote on last edited by
        #3

        @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.

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

          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)

          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
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved