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 8313201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:24:43+00:00 2026-06-08T20:24:43+00:00

i have a program that needs to paint some text with paintEvent(). i’ve tried

  • 0

i have a program that needs to paint some text with paintEvent().
i’ve tried this:

1. QPainter painter; // painter for the canvas
2. painter.drawText(QPoint location, QString canvasText);

where

3. QString canvasText = variablesText.append("< b >");
4. variablesText.append((*fieldIter).second.c_str());
5. variablesText.append(":< /b > ");
6. variablesText.append(someValue);
7. variablesText.append("\n");

I need the text to be formated, canvasText should look like:

Some bold text: some not bold text. (newLine)

Some bold text 2: some not bold text2. (newLine) and that goes on for a while.

The problem i’m having is that a QString can’t have HTML code in it, so the text is displayed like:

< b>Some bold text:< /b> some not bold text. < b>Some bold text 2: < /b>
some not bold text2.\n

Is there a way to use draw in paintEvent to show the text the way i need it? with a QString (or QLabel or something)

I’m using Qt4.

Thanks for the help =)

  • 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-08T20:24:44+00:00Added an answer on June 8, 2026 at 8:24 pm

    QTextDocument seems like a good fit for what you’re after, specifically setHtml(). A QTextDocument can format your text and paint the result to the screen via your widget’s QPainter. Something like the below is the simplest possible solution:

    void Test::paintEvent(QPaintEvent *)
    {
      QPainter painter(this);
    
      QTextDocument doc;
      doc.setHtml("<b>Title</b><p>Body Text</p>");
    
      doc.drawContents(&painter, rect());
    }
    

    Bear in mind, however, that this is likely to be very inefficient. You’ll probably want to drawContents() to a cached QPixmap only when your source html changes…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that needs to... In Activity A , do some jobs
I have a Java program that outputs some text into console. It uses print
I have a program that occasionally needs to scan some directories recursively (an improvement
I have a program that needs several third-party libraries, and at the moment it
I have a program that needs a lot of memory and want to set
I have a Flash program that needs to make url requests to send data
In an embedded program I have a screen object that needs to manage a
I have a program that creates multiple text files of rdf triples. I need
I have a program that looks like this. I need to consistently write something
Not a terribly good question, sorry. I have a program that needs to be

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.