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. Change QAbstractItemView cell's decoration upon hover

Change QAbstractItemView cell's decoration upon hover

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    Hello, all.

    Currently, it is possible to change any visual aspects of a QAbstractItemView's cell using QSS, that is, modify the style of the QAbstractItemView::item subcontrol. I've found no way to customize the decoration of the latter, but only its text color, background, border, etc. The requirement is to change the icon / decoration upon hover. Tried using QIcon's QIcon::Normal and QIcon::Active modes but it didn't work. The brute force approach to hook to the view's notifications where the user is hovering and to provide this information to the model is known but not preferred. If you know about any integrated or better approach, please share it.

    raven-worxR 1 Reply Last reply
    0
    • napajejenunedk0N napajejenunedk0

      Hello, all.

      Currently, it is possible to change any visual aspects of a QAbstractItemView's cell using QSS, that is, modify the style of the QAbstractItemView::item subcontrol. I've found no way to customize the decoration of the latter, but only its text color, background, border, etc. The requirement is to change the icon / decoration upon hover. Tried using QIcon's QIcon::Normal and QIcon::Active modes but it didn't work. The brute force approach to hook to the view's notifications where the user is hovering and to provide this information to the model is known but not preferred. If you know about any integrated or better approach, please share it.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @napajejenunedk0
      the cleanest solution IMO is (subclassing QStyledItemDelegate and reimplement the paint method):

      void MyStyledItemDelegate::paint(QPainter *painter,  const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
          Q_ASSERT(index.isValid());
      
          QStyleOptionViewItem opt = option;
          initStyleOption(&opt, index);
      
          if( opt.state & QStyle::State_MouseOver ) // additional checks here?
               opt.icon = QIcon(...);
      
          const QWidget* widget = opt.widget;
          QStyle *style = widget ? widget->style() : QApplication::style();
          style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
      }
      

      alternatively (if you dont want to change the paint() method) only override initStyleOption():

      void MyStyledItemDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex & index) const
      {
           QStyledItemDelegate::initStyleOption(option, index);
           if( option->state & QStyle::State_MouseOver ) // additional checks here?
               option->icon = QIcon(...); //e.g. index.data(MyModel::AlternativeDecorationRole).value<QIcon>();
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Jus a small note, option->icon might not be enough if the icons are of different sizes or you have no icon at all when not hovering. You probably also need decorationPosition

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • napajejenunedk0N Offline
          napajejenunedk0N Offline
          napajejenunedk0
          wrote on last edited by
          #4

          @raven-worx, @VRonin , thank you. Now, I have an issue drawing the icon for the focused state. Now, the latter is the one being stylized instead of the mouse hover one. The problem is that the icon/decoration of the focused item is drawn using some color overlay. In my case it is a dark cyan color overlay:
          0_1542984731725_22d00f2c-bb1d-47a4-9b43-953088cc7374-image.png
          See the icon displayed on the left of the "High" list item which is the actual decoration that is affected by some mysterious color overlay and compare it to what it should actually look like on the right side of "High". This problem is reproducible only for the item is focused. Could any QSS affect the icon as well e.g. interpret another style preferences of the focused state of the cell as a tint for its decoration?

          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