c++ - QPainter 中的文本比 QPainterPath 中的文本好得多

标签 c++ qt qt5 qpainter

我想使用 QPainter 绘制文本,我想首先使用 QPainterPath(因为最终我想以各种方式旋转文本)。但是,我发现 QPainterPath 生成的文本比 QPainter 生成的文本要丑得多。

以下代码:

void MyWidget::paintEvent(QPaintEvent* /*event*/) {

     QFont font;
     font.setStyleHint(QFont::Times, QFont::PreferAntialias);
     font.setPointSize(30);

     QPainter painter;
     painter.begin(this);
     painter.setRenderHint(QPainter::Antialiasing);
     painter.setBrush(Qt::black);
     painter.setFont(font);
     painter.drawText(10, 40, "Hello World");

     QPainterPath textPath;
     textPath.addText(10, 100, font, "Hello world");
     painter.drawPath(textPath);

     painter.end();
}

产生以下结果:

enter image description here

前者显然更干净、更好看,尤其是在较小的字体中。我应该怎么做才能从 QPainterPath 获得相同的结果?

我在装有 Qt 5.0 的 Windows 7 计算机上生成上述结果。

最佳答案

根据用于向 QPainterPath 添加文本的 Qt 文档:-

Adds the given text to this path as a set of closed subpaths created from the font supplied.

所以这里发生了转换,这就是它看起来不一样的原因。如果您需要旋转文本,您可以尝试在渲染之前旋转 QPainter,然后再将其恢复。或者,如果您可以使用 QGraphicsView 和 QGraphicsDisplay 而不是仅仅渲染到小部件上,那么 QGraphicsTextItem 类可能会有所帮助。

但总的来说,导致不同文本质量输出的是封闭子路径集的转换。

关于c++ - QPainter 中的文本比 QPainterPath 中的文本好得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832783/

相关文章:

c++ - 命名空间中的主要内容

c++ - 适用于 C++ 的 Dropbox 同步 API

python - 挂起当前函数直到对话框关闭

c++ - QJson数据序列化顺序

python - PyQt5 中的 devicePixelRatio 损坏

python - 通过 QBuffer 到 QGeometry 的 Numpy 数组

qt5 - 如何避免带有 borderimage 的 QPushButton 的文本剪切

c++ - 对 CUDA 代码使用 C++ header 结构

c++ - 如何在android项目中使用NDK?

c++ - Qt Quick - 如何使用 setContextProperties()