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. Item background color from selection with custom paint() on ItemDelegate issue
Forum Updated to NodeBB v4.3 + New Features

Item background color from selection with custom paint() on ItemDelegate issue

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

    Hello,

    I noticed recently that the items on my QTreeView with custom QStyledItemDelegate paint do not get the proper background item color on select. They do on Hover/View item colors. But I need to call the QStyledItemDelegate::paint before drawing my custom data for the select color to display correctly, but it feels odd that select doesn't draw properly while the hover does. Its especially not great because if there is some data on that item, it gets painted twice, once by the QSyledItemDelegate::paint() and the second time by my own custom paint code. Is there a way to fix this? Or first draw a quad in my custom paint code with the correct color?

    Christian EhrlicherC 1 Reply Last reply
    0
    • E esmolikowski

      Hello,

      I noticed recently that the items on my QTreeView with custom QStyledItemDelegate paint do not get the proper background item color on select. They do on Hover/View item colors. But I need to call the QStyledItemDelegate::paint before drawing my custom data for the select color to display correctly, but it feels odd that select doesn't draw properly while the hover does. Its especially not great because if there is some data on that item, it gets painted twice, once by the QSyledItemDelegate::paint() and the second time by my own custom paint code. Is there a way to fix this? Or first draw a quad in my custom paint code with the correct color?

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

      @esmolikowski said in Item background color from selection with custom paint() on ItemDelegate issue:

      by the QSyledItemDelegate::paint() and the second time by my own custom paint code.

      How should this happen? Please show your painting within your ItemDelegate::paint() function.

      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
      0
      • E Offline
        E Offline
        esmolikowski
        wrote on last edited by
        #3

        My paint code looks like this:

        def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionViewItem, index: Union[QtCore.QModelIndex, QtCore.QPersistentModelIndex]) -> None:
        painter.save()

            model = self.parentObject.model()
            sourceModel = model.sourceModel()
            
            sourceIndex = model.mapToSource(index)
            item = sourceModel.itemFromIndex(sourceIndex)
            
            label = self.getLabel(item)
            assert label
        
            opt = QtWidgets.QStyleOptionButton()
            opt.initFrom(label)
        
            opt.rect = option.rect.adjusted(self.padding, self.padding, -self.padding, -self.padding)
            opt.icon = label.icon()
            opt.text = label.text()
            opt.iconSize = label.iconSize()
        
            option.widget.style().drawControl(QtWidgets.QStyle.ControlElement.CE_PushButton, opt, painter, label)
        
            painter.restore()
        

        Shouldn't the background color for the item already be painted with the correct color? The alternateColor and hover color for the item are properly painted because I get into this function. Only the select color is not painted properly.

        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