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 - get table item's current background color?
QtWS25 Last Chance

QStyledItemDelegate - get table item's current background color?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstyleditemdelepainter
5 Posts 2 Posters 2.7k 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.
  • L Offline
    L Offline
    Lobstw
    wrote on last edited by Lobstw
    #1

    Hello,

    Trying to implement a transparent selection by using a QStyledItemDelegate and overriding the paint method:

    def paint(self, painter, option, index):
        painter.save()
        if option.state & QtWidgets.QStyle.State_Selected:
            var = index.model()
            color = var.data(index, QtCore.Qt.BackgroundRole)
            text = var.data(index, QtCore.Qt.DisplayRole)
            painter.fillRect(option.rect, color.color())
            painter.drawText(option.rect, QtCore.Qt.AlignCenter, text)
        else:
            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
        painter.restore()
    

    My table item, from a TableWidget, already has a color set (blue). So how can I get the current background of the cell and set the selected color to that? Setting color.color() gives me a black background and just using the QBrush itself color gives me the default grey.

    (Text is slightly off too, I'd prefer to have it maintain its original settings)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean you don't want to paint anything special when the item is selected ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you mean you don't want to paint anything special when the item is selected ?

        L Offline
        L Offline
        Lobstw
        wrote on last edited by
        #3

        @SGaist Yep that's what I want, I know I can do something like this for that:

        option.state &= ~QtWidgets.QStyle.State_Selected in initStyleOption

        But I was planning on adding a border too so getting the background color itself would be ideal.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What style of border ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          0
          • SGaistS SGaist

            What style of border ?

            L Offline
            L Offline
            Lobstw
            wrote on last edited by
            #5

            @SGaist Just a simple lined border. At present, this border applies behind the background and isn't visible. There's also the issue of the existing dotted border that I'd want to remove/replace.

            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