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

  • SEARCH
  • Home
  • 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 229453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:46:01+00:00 2026-05-11T19:46:01+00:00

I have a custom Qt widget which I used to display disassembly and I

  • 0

I have a custom Qt widget which I used to display disassembly and I am looking to add syntax coloring to it.

Currently, I simply set the QPen to a solid color, construct the text I want to display, and render it to the QPainter at the appropriate coordinates.

The question is, what is the best approach to adding syntax coloring? I’ve thought of a few:

  1. I could simply divide the coloring into logical blocks, each preceded by setting the QPen to the desired color.
  2. I could have special escape characters which represent a change in the color palette, and render 1 character at a time.
  3. I could do a modification of #1 and create a list of std::pair<QColor, QString>, then I could simply iterate the list setting the color and drawing the text as I pop items off the front of the list.
  4. Something entirely different?

I know that each of the 3 approaches I’ve listed will technically work, but I’m looking for a very efficient solution. This code will be called a lot. And since this is an interactive debugger, if this code is slow, someone rapidly stepping or tracing will see a visible slowdown.

EDIT: I’m aware of QSyntaxHighlighter and QTextDocument. The main issue is that these don’t generally suite my purposes very well. I have several columns which all have dividers and can be slid back and forth. To give you an idea, Here’s a link to a screenshot of my debugger. As you can see it isn’t really like a text document at all. In fact it is closer to a list or table. But there is already a bunch of custom drawing going on making a normal QTextDocument somewhat impractical.

EDIT: I was incorrect, It seems that QTextDocument can render directly to a QPainter. Looks like what I need!

EDIT: It is unclear how to control where and how QTextDocument or QTextLayout will draw on a QPainter. I’ve attempted to use them to no avail. So if someone could provide a rudimentary example, that would be very helpful.

EDIT: I was eventually able to get what I wanted using something like this:

painter.setPen(default_color);
QTextDocument doc;
doc.setDefaultFont(font());
doc.setDocumentMargin(0);
doc.setPlainText(text);
highlighter_->setDocument(&doc);

painter.save();
painter.translate(x, y);
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter.pen().color());
doc.draw(&painter, context);
painter.restore();
  • 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-05-11T19:46:02+00:00Added an answer on May 11, 2026 at 7:46 pm

    Qt provides a QSyntaxHighlighter that is probably exactly what you want. QSyntaxHighlighter uses a QTextDocument to mark each block of code with a specific state which can be associated with a specific presentation format.

    The documentation on QSyntaxHighlighter provides a sample demonstrating how this may be accomplished and does some nice things:

    • Separates the model from presentation
    • Separates the formatting into different reusable classes (if implemented as such)
    • Supports the State design pattern if useful to your language
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a MyWidget which contains a MySubWidget , e.g. a custom widget
lets say i have a custom widget which has a ClickHandler. Here's the example:
I have a custom GTK+ widget which I am compiling in a C++ file
i'm having difficulties implementing custom widget rendering in a QListView . I currently have
I have a custom ScrollView (extended android.widget.ScrollView) which I use in my layout. I
I have created a custom widget in gwt which extends the composite.I am using
I have a widget which I am trying to use to display information from
I have created a custom dijit widget which contains a grid and some buttons.
Basically I have written a custom widget, which is going to be displayed 5
I have created a custom widget which has a link to some ajax functionality.

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.