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. Make selection background have similar background to item being selected

Make selection background have similar background to item being selected

Scheduled Pinned Locked Moved Unsolved General and Desktop
stylesheetselectioncolorbackground
10 Posts 2 Posters 6.1k 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 26 Nov 2015, 22:00 last edited by
    #1

    How can I make the background a blend of current cell background for when an item is selected ?

    The default item-selection background is blue and say if my cell is red, then I'd want it to be red with slightly less opacity:

    Example image of how it is

    Example image of how I'd like it to be

    I have tried setting the color to be transparent:

    setStyleSheet("selection-background-color: transparent")
    

    And also the rgba feature with opacity 1%:

    setStyleSheet("selection-background-color: rgba(255, 255, 255, 1)")
    

    But neither retain the original color.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Nov 2015, 22:16 last edited by
      #2

      Hi,

      One way would be to use a QStyledItemDelegate where you handle your special painting.

      Hope it helps

      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 26 Nov 2015, 22:34
      0
      • S SGaist
        26 Nov 2015, 22:16

        Hi,

        One way would be to use a QStyledItemDelegate where you handle your special painting.

        Hope it helps

        L Offline
        L Offline
        Lobstw
        wrote on 26 Nov 2015, 22:34 last edited by
        #3

        @SGaist Hello, thank you for the quick response. If you could, can you point me towards some resources on making delegates and how I'd use the paint option (I've not used delegates before). And are you saying that there's no other way of doing this? Thank you

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 26 Nov 2015, 22:40 last edited by
          #4

          The "extreme" example is the Star Delegate Example and shows the technique to draw the selection rectangle 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 2 Replies Last reply 27 Nov 2015, 00:14
          1
          • S SGaist
            26 Nov 2015, 22:40

            The "extreme" example is the Star Delegate Example and shows the technique to draw the selection rectangle when the item is selected.

            L Offline
            L Offline
            Lobstw
            wrote on 27 Nov 2015, 00:14 last edited by
            #5

            @SGaist I'll try and make this work for python, thank you

            1 Reply Last reply
            0
            • S SGaist
              26 Nov 2015, 22:40

              The "extreme" example is the Star Delegate Example and shows the technique to draw the selection rectangle when the item is selected.

              L Offline
              L Offline
              Lobstw
              wrote on 27 Nov 2015, 01:01 last edited by
              #6

              @SGaist I've implemented it in python and it seems that it overwrites the background set previously? I have a tablecell item that I've applied a gradient to so it's reddish.

              This is what I'm using:

              class SelectionHighlightDelegate(QtWidgets.QItemDelegate):
              
              def __init__(self, parent=None, *args):
                  QtWidgets.QItemDelegate.__init__(self, parent, *args)
              
              def paint(self, painter, option, index):
                  painter.save()
              
              # set background color
                  painter.setPen(QtGui.QPen())
                  if option.state & QtWidgets.QStyle.State_Selected:
                      painter.setBrush(#blue#)
                  else:
                      painter.setBrush(QtGui.QBrush(#white))
                      painter.drawRect(option.rect)
              
              # set text color
                  value = index.data(QtCore.Qt.DisplayRole)
                  if option.state & QtWidgets.QStyle.State_Selected:
                      painter.setPen(QtGui.QPen(#white#)
                  else:
                      painter.setPen(QtGui.QPen(#black#)
              
              # Left indent
                  painter.translate(3, 0)
              
                  painter.drawText(option.rect, QtCore.Qt.AlignLeft, value)
              
                  painter.restore()
              

              (Which is a pyqt5 version of this)

              In another class with the main application/window I have some table items with a custom background

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 27 Nov 2015, 21:53 last edited by
                #7

                You're making the background white no matter the state except for State_Selected, you should then rather use the options values

                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 28 Nov 2015, 14:58
                0
                • S SGaist
                  27 Nov 2015, 21:53

                  You're making the background white no matter the state except for State_Selected, you should then rather use the options values

                  L Offline
                  L Offline
                  Lobstw
                  wrote on 28 Nov 2015, 14:58 last edited by
                  #8

                  @SGaist I had that part commented out in my code but it still ignores the background set previously to the cell. I have two tables, one I set the custom delegate on and that one has the background colors reset. The other one is fine. What do you mean by options values?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 28 Nov 2015, 21:15 last edited by
                    #9

                    The option parameter gives you all the information about colors to use and the rest for painting so if you set a particular color you should use the information from the palette given by option to do the drawing.

                    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 28 Nov 2015, 22:11
                    0
                    • S SGaist
                      28 Nov 2015, 21:15

                      The option parameter gives you all the information about colors to use and the rest for painting so if you set a particular color you should use the information from the palette given by option to do the drawing.

                      L Offline
                      L Offline
                      Lobstw
                      wrote on 28 Nov 2015, 22:11 last edited by
                      #10

                      @SGaist Argh everything's too confusing. I think I'll just leave it for now haha

                      1 Reply Last reply
                      0

                      9/10

                      28 Nov 2015, 21:15

                      • Login

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