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. When using a custom delegate to paint the items of a QTableWidget, how can the delegate be configured to use the correct background colour for selected items?
Forum Updated to NodeBB v4.3 + New Features

When using a custom delegate to paint the items of a QTableWidget, how can the delegate be configured to use the correct background colour for selected items?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.9k 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.
  • B Offline
    B Offline
    Bastian
    wrote on last edited by
    #1

    Hi,

    Normally, when a QTableWidget item is selected, the background appears dark blue, but when I use a custom delegate to paint the items (taken from a Stack Overflow post, https://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt/1956781#1956781) the background appears light blue instead.

    What do I need to do to match the original behaviour when using the delegate?

    Thanks.

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

      Hi
      Add some extra code to the painting (set the brush)

      void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
      {
          QStyleOptionViewItemV4 optionV4 = option;
          initStyleOption(&optionV4, index);
      
          QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
      
          QTextDocument doc;
          doc.setHtml(optionV4.text);
      
          /// Painting item without text
          optionV4.text = QString();
      
          if (option.state & QStyle::State_Selected) { //////////// new
              painter->setPen(Qt::white);
              painter->setBrush(option.palette.highlightedText());
          }
      
          style->drawControl(QStyle::CE_ItemViewItem, &option, painter, option.widget); // slightly changed
      
          QAbstractTextDocumentLayout::PaintContext ctx;
      
          // Highlighting text if item is selected
          if (optionV4.state & QStyle::State_Selected) {
              ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active,
                                                                          QPalette::HighlightedText));
              ctx.palette.setColor(QPalette::Highlight, Qt::green);
              ctx.palette.setColor(QPalette::Base, Qt::green);
          }
      
          QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
          painter->save();
          painter->translate(textRect.topLeft());
          painter->setClipRect(textRect.translated(-textRect.topLeft()));
          doc.documentLayout()->draw(painter, ctx);
          painter->restore();
      }
      
      

      alt text

      B 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Add some extra code to the painting (set the brush)

        void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
        {
            QStyleOptionViewItemV4 optionV4 = option;
            initStyleOption(&optionV4, index);
        
            QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
        
            QTextDocument doc;
            doc.setHtml(optionV4.text);
        
            /// Painting item without text
            optionV4.text = QString();
        
            if (option.state & QStyle::State_Selected) { //////////// new
                painter->setPen(Qt::white);
                painter->setBrush(option.palette.highlightedText());
            }
        
            style->drawControl(QStyle::CE_ItemViewItem, &option, painter, option.widget); // slightly changed
        
            QAbstractTextDocumentLayout::PaintContext ctx;
        
            // Highlighting text if item is selected
            if (optionV4.state & QStyle::State_Selected) {
                ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active,
                                                                            QPalette::HighlightedText));
                ctx.palette.setColor(QPalette::Highlight, Qt::green);
                ctx.palette.setColor(QPalette::Base, Qt::green);
            }
        
            QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
            painter->save();
            painter->translate(textRect.topLeft());
            painter->setClipRect(textRect.translated(-textRect.topLeft()));
            doc.documentLayout()->draw(painter, ctx);
            painter->restore();
        }
        
        

        alt text

        B Offline
        B Offline
        Bastian
        wrote on last edited by
        #3

        @mrjj Thanks! That's fixed it.

        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