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. Show HTML content in QTableView column while keeping BackgroundColorRole works

Show HTML content in QTableView column while keeping BackgroundColorRole works

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.7qtableviewqitemdelegateqstyleditemdeleqstandarditemmo
2 Posts 2 Posters 1.8k 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.
  • A Offline
    A Offline
    alizadeh91
    wrote on 19 Jul 2016, 13:04 last edited by
    #1

    I've created a QStyledItemDelegate subclass in order to show HTML content in a column of QTableView. I had also set backgroundColors of QTableView' rows dynamically by setting data of the model this way:

    _model->setData(index, color, Qt::BackgroundColorRole);
    

    If I set delegate for the column, then, It will lost backgroundColor since it will be ignored by the delegate paint() method. So, How can I modify or implement the paint() method of the delegate class in order to keep both html content enabled and keep backGroundColorRole data?

    Here is my paint() method of the delegate:

    void THTMLDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
    {
     QStyleOptionViewItem optionV4 = option;
     initStyleOption(&optionV4, index);
    
     QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
    
     QTextDocument doc;
     doc.setHtml(optionV4.text);
    
     QAbstractTextDocumentLayout::PaintContext ctx;
    
     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();
     }
    
    R 1 Reply Last reply 19 Jul 2016, 13:20
    0
    • A alizadeh91
      19 Jul 2016, 13:04

      I've created a QStyledItemDelegate subclass in order to show HTML content in a column of QTableView. I had also set backgroundColors of QTableView' rows dynamically by setting data of the model this way:

      _model->setData(index, color, Qt::BackgroundColorRole);
      

      If I set delegate for the column, then, It will lost backgroundColor since it will be ignored by the delegate paint() method. So, How can I modify or implement the paint() method of the delegate class in order to keep both html content enabled and keep backGroundColorRole data?

      Here is my paint() method of the delegate:

      void THTMLDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                            const QModelIndex &index) const
      {
       QStyleOptionViewItem optionV4 = option;
       initStyleOption(&optionV4, index);
      
       QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
      
       QTextDocument doc;
       doc.setHtml(optionV4.text);
      
       QAbstractTextDocumentLayout::PaintContext ctx;
      
       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();
       }
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 19 Jul 2016, 13:20 last edited by raven-worx
      #2

      @alizadeh91
      Either you paint it yourself since you already have the QModelIndex at hand:

      QColor color = index.data( Qt::BackgroundColorRole ).toColor();
      

      or by letting the style paint the stuff. Note that this also paints the selected background.

      style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, widget );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      2/2

      19 Jul 2016, 13:20

      • Login

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