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. QStyledItemDelegate does not highlight with the rest of the row on mouse-over
Forum Updated to NodeBB v4.3 + New Features

QStyledItemDelegate does not highlight with the rest of the row on mouse-over

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 311 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.
  • qwasder85Q Offline
    qwasder85Q Offline
    qwasder85
    wrote on last edited by qwasder85
    #1

    I set up a delegate for a column in a QTableView and I'd like it to draw an icon when the mouse hovers somewhere over its row.

    Having it react to a mouse-over on the delegate itself is easy:

    void MyDelegate::paint(QPainter *in_p_painter,  const QStyleOptionViewItem &in_option, const QModelIndex &in_index) const
    {
        QStyledItemDelegate::paint(in_p_painter, in_option, in_index);
               
        if (in_option.state & QStyle::State_MouseOver)
        {
            // Draw the icon
            m_add_icon.paint(in_p_painter, in_option.rect, Qt::AlignCenter, QIcon::Normal);
        }
    }
    

    By trying this, I figured it would be better to show the icon when the user hovers anywhere on the row, not just the cell with the delegate.
    Unfortunately, the delegate does not highlight with the rest of the row on mouse-over, so I can't poll any QStyle flags noticing me of a highlighting:

    alt text

    I see two solutions for this:
    a) Make sure the column with the delegate highlights with the rest of the row and poll the highlight flags
    b) Find the highlighting-state of a neighboring cell in the same row and react to that

    I'd prefer solution a), obviously. But maybe there's an even better way. Any ideas?

    Christian EhrlicherC 1 Reply Last reply
    0
    • qwasder85Q qwasder85

      I set up a delegate for a column in a QTableView and I'd like it to draw an icon when the mouse hovers somewhere over its row.

      Having it react to a mouse-over on the delegate itself is easy:

      void MyDelegate::paint(QPainter *in_p_painter,  const QStyleOptionViewItem &in_option, const QModelIndex &in_index) const
      {
          QStyledItemDelegate::paint(in_p_painter, in_option, in_index);
                 
          if (in_option.state & QStyle::State_MouseOver)
          {
              // Draw the icon
              m_add_icon.paint(in_p_painter, in_option.rect, Qt::AlignCenter, QIcon::Normal);
          }
      }
      

      By trying this, I figured it would be better to show the icon when the user hovers anywhere on the row, not just the cell with the delegate.
      Unfortunately, the delegate does not highlight with the rest of the row on mouse-over, so I can't poll any QStyle flags noticing me of a highlighting:

      alt text

      I see two solutions for this:
      a) Make sure the column with the delegate highlights with the rest of the row and poll the highlight flags
      b) Find the highlighting-state of a neighboring cell in the same row and react to that

      I'd prefer solution a), obviously. But maybe there's an even better way. Any ideas?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Are you sure that the cell is enabled? Otherwise I don't see why the delegate should not be called when the whole line is hovered.

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

      qwasder85Q 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Are you sure that the cell is enabled? Otherwise I don't see why the delegate should not be called when the whole line is hovered.

        qwasder85Q Offline
        qwasder85Q Offline
        qwasder85
        wrote on last edited by
        #3

        @Christian-Ehrlicher Yes, I don't know why it shouldn't be enabled and it also reacts to the editor event.

        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