QPainter彩色文本(语法着色)

发布于 2024-07-22 20:43:56 字数 1576 浏览 10 评论 0原文

我有一个自定义的 Qt 小部件,用于显示反汇编,我希望为其添加语法着色。

目前,我只需将 QPen 设置为纯色,构造我想要显示的文本,并将其渲染到 QPainter 的适当坐标处。

问题是,添加语法着色的最佳方法是什么? 我想到了一些:

  1. 我可以简单地将颜色划分为逻辑块,每个逻辑块之前都将 QPen 设置为所需的颜色。
  2. 我可以使用特殊的转义字符来表示调色板的变化,并一次渲染 1 个字符。
  3. 我可以对 #1 进行修改并创建一个 std::pair 列表,然后我可以简单地迭代列表设置颜色并在弹出项目时绘制文本列表的前面。
  4. 完全不同的东西吗?

我知道我列出的 3 种方法在技术上都是可行的,但我正在寻找一种非常有效的解决方案。 这段代码会被很多人调用。 由于这是一个交互式调试器,如果此代码很慢,快速单步执行或跟踪的人会看到明显的减慢。

编辑:我知道QSyntaxHighlighterQTextDocument。 主要问题是这些通常不太适合我的目的。 我有几根立柱,它们都有分隔板,可以前后滑动。 为了给您提供一个想法,这里有一个指向我的调试器的屏幕截图的链接。 正如您所看到的,它根本不像文本文档。 事实上它更接近于列表或表格。 但是已经有一堆自定义绘图正在进行,使得普通的 QTextDocument 有点不切实际。

编辑:我错了,QTextDocument似乎可以直接渲染到QPainter。 看起来像我需要的!

编辑:目前尚不清楚如何控制QTextDocumentQTextLayoutQPainter上绘制的位置和方式。 我尝试使用它们但没有成功。 因此,如果有人可以提供一个基本的例子,那将会非常有帮助。

编辑:我最终能够使用这样的东西得到我想要的东西:

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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-07-29 20:43:56

我会使用 QTextEdit 或直接使用其下划线引擎 QTextDocument

I'd use either QTextEdit or directlay its underlining engine QTextDocument.

挽清梦 2024-07-29 20:43:56

Qt 提供了一个 QSyntaxHighlighter 这可能正是你要。 QSyntaxHighlighter 使用 QTextDocument 用特定状态标记每个代码块,该状态可以与特定演示文稿关联 格式

QSyntaxHighlighter 上的文档提供了一个示例,演示了如何实现这一点并做了一些好事:

  • 将模型与表示分离
  • 将格式分离为不同的可重用类(如果这样实现)
  • 支持状态 设计模式(如果对您的语言有用)

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文