Results 1 to 1 of 1

Thread: Adding HTML code into QTableWidget cells

  1. #1
    Join Date
    Dec 2013
    Posts
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Adding HTML code into QTableWidget cells

    Hi,
    I have a QTableWidget of 4 columns and many rows.
    The contents of cells is loaded form a XML file that contains HTML code.
    To do so, i wrote a delegate class :
    Qt Code:
    1. class HtmlDelegate : public QStyledItemDelegate
    2. {
    3. protected:
    4. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    5. QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    6. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItem optionV4 = option;
    4. initStyleOption(&optionV4, index);
    5.  
    6. QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
    7.  
    8. doc.setHtml(optionV4.text);
    9.  
    10. QAbstractTextDocumentLayout::PaintContext ctx;
    11.  
    12. QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
    13. painter->save();
    14. painter->translate(textRect.topLeft());
    15. painter->setClipRect(textRect.translated(-textRect.topLeft()));
    16.  
    17. doc.documentLayout()->draw(painter, ctx);
    18. painter->restore();
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItem optionV4 = option;
    4. initStyleOption(&optionV4, index);
    5.  
    6. doc.setHtml(optionV4.text);
    7. doc.setTextWidth(optionV4.rect.width());
    8. return QSize(doc.idealWidth(), doc.size().height());
    9. }
    To copy to clipboard, switch view to plain text mode 

    It works well, but the last column which is normaly stretched when i use text plain without the delegate fonction
    Qt Code:
    1. qtbl->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 
    lose the stretched parameter and the cell's height doesn't fit the multiline content.

    Thanks for you help.

  2. The following 2 users say thank you to a5633Oy for this useful post:


Similar Threads

  1. QTableWidget exclusive cells
    By bwnicewo in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2012, 00:25
  2. How to display cut cells in QTableWidget
    By danblanks in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 21:42
  3. read QTableWidget cells
    By navid in forum Newbie
    Replies: 8
    Last Post: 4th April 2010, 10:40
  4. Merging cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 1
    Last Post: 22nd January 2010, 19:15
  5. Focus of cells in a QTableWidget
    By SailinShoes in forum Qt Programming
    Replies: 4
    Last Post: 9th June 2008, 08:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.