Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:47:15+00:00 2026-06-01T13:47:15+00:00

I want my AbstracttableModel subclass data() method to return html i.e. PreText<b>Text</b>PostText And this

  • 0

I want my AbstracttableModel subclass data() method to return html i.e.

PreText<b>Text</b>PostText

And this text must be displayed int table as in html:
PreTextTextPostText

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:47:16+00:00Added an answer on June 1, 2026 at 1:47 pm

    You can create a delegate for the view that will display the html.

    class HtmlDelegate : public QItemDelegate {
    public:
        HtmlDelegate(QObject *parent = 0) : QItemDelegate(parent) {}
    
        // This function is only called to paint the text
        void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
                         const QRect &rect, const QString &text) const
        {
            QTextDocument doc;
    
            // Since the QTextDocument will do all the rendering, the color,
            // and the font have to be put back inside the doc
            QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                      ? QPalette::Normal : QPalette::Disabled;
            if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
                cg = QPalette::Inactive;
            QColor textColor = option.palette.color(cg, QPalette::Text);
            doc.setDefaultStyleSheet(QString("body { color: %1}")
                                     .arg(textColor.name()));
            doc.setDefaultFont(option.font);
            doc.setHtml(text);
            doc.setDocumentMargin(1); // the default is 4 which is too much
    
            painter->save();
            painter->translate(rect.topLeft());
            doc.drawContents(painter);
            painter->restore();
        }
    
        // bold and underlined characters take more space
        // so you have to redefine this function as well
        // (if you have a checkbox or an icon in the item, you will have
        // to include their size to the returned value)
        QSize sizeHint(const QStyleOptionViewItem &option,
                       const QModelIndex &index) const
        {
            QTextDocument doc;
            doc.setDefaultFont(option.font);
            doc.setHtml(index.data(Qt::DisplayRole).toString());
            doc.setDocumentMargin(1);
            return doc.size().toSize();
        }
    };
    

    Then assign it to a view:

    view->setItemDelegateForColumn(0, new HtmlDelegate(view));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subclass of javax.swing.table.AbstractTableModel that defines table column headers like this: protected
I have a Table: public class AppointmentTableModel extends AbstractTableModel { private int columns; private
I have this code in my Table model: public class DocumentProjectTableModel extends AbstractTableModel{ private
I have extended the AbstractTableModel to suit my requirements. Now this table can be
I am retriving data from database.TableName:Contacts. There are two fields of this table.First_Name and
Want the function to sort the table by HP but if duplicate HPs then
I have a custom data structure that I want to display in a PyQt
want to create a temporary table that has an auto_increment field plus a field
I want to implement a TableModel, that will enable my existing data structure to
I have a table model. I want to delete a row from a table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.