Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to draw row selection if item delegate has been subclassed

How to draw row selection if item delegate has been subclassed

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 919 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.
  • J Offline
    J Offline
    judethedude
    wrote on last edited by
    #1

    Hey everyone,

    I've built a custom QStyledItemDelegate with a paint function so I can display some columns in a different way. However, this has eliminated any ability to select anything in the table, regardless of the QTableView settings (ie SingleSelection, SelectRows). This is the code for my delegate:

    class RefractOverUnderDelegate(QStyledItemDelegate):
        def paint(self, painter: QPainter, option, index: QModelIndex):
            combine_columns_pairs = {
                2:None, 3:5, 7:8, 24:36, 25:37, 26:38, 27:39, 28:40, 29:41, 30:42, 31:43, 32:44, 33:45, 34:46, 35:47, 65:66
                }
            if index.column() in combine_columns_pairs.keys():
                row = index.row()
                top_col = index.column()
                bot_col = combine_columns_pairs[top_col]
                
                if combine_columns_pairs[top_col] == None:
                    data_bot = ""
                else:
                    data_bot = index.model().data(index.sibling(row, bot_col), 0)
                
                data_top = index.model().data(index.sibling(row, top_col), 0)
                
                data = str(data_top) + "\n" + str(data_bot)
                painter.drawText(option.rect, Qt.AlignCenter, data)
            else:
                super().paint(painter, option, index)
    

    and this produces this kind of table:
    alt text
    I realize my delegate is kinda janky but I couldn't figure out a better way to do it, and I need to move on with the project.

    Any recommendations for doing row selection?

    Regards,
    Jude

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

      Hi,

      From the looks of it, your main issue is that you are not painting the selection rectangle in your delegate.

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

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        From the looks of it, your main issue is that you are not painting the selection rectangle in your delegate.

        J Offline
        J Offline
        judethedude
        wrote on last edited by judethedude
        #3

        @SGaist thanks for the reply. How would I go about painting the selection?

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

          As silly as it may sound: paint a transparent rectangle after you are done painting the rest of your cell.

          Use the [state from your QStyleOptionViewItem to know whether you need to raw the focus rectangle.

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

          J 1 Reply Last reply
          1
          • SGaistS SGaist

            As silly as it may sound: paint a transparent rectangle after you are done painting the rest of your cell.

            Use the [state from your QStyleOptionViewItem to know whether you need to raw the focus rectangle.

            J Offline
            J Offline
            judethedude
            wrote on last edited by
            #5

            @SGaist thanks that worked!

            For anyone that finds this thread, this is the code I used (courtesy of chatGPT actually):

            class RefractOverUnderDelegate(QStyledItemDelegate):
                def paint(self, painter: QPainter, option, index: QModelIndex):
                    if option.state & QStyle.State_Selected:
                        colour = option.palette.highlight().color()
                        painter.fillRect(option.rect, colour)
            

            I found I needed to highlight (fillRect) the cell and then paint the text over the highlight.

            JonBJ 1 Reply Last reply
            0
            • J judethedude

              @SGaist thanks that worked!

              For anyone that finds this thread, this is the code I used (courtesy of chatGPT actually):

              class RefractOverUnderDelegate(QStyledItemDelegate):
                  def paint(self, painter: QPainter, option, index: QModelIndex):
                      if option.state & QStyle.State_Selected:
                          colour = option.palette.highlight().color()
                          painter.fillRect(option.rect, colour)
              

              I found I needed to highlight (fillRect) the cell and then paint the text over the highlight.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @judethedude said in How to draw row selection if item delegate has been subclassed:

              (courtesy of chatGPT actually)

              OOI, do you know exactly how you phrased your question to ChatGPT?

              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