如何打印包含文本和图形的 QGraphicsScene

发布于 2024-09-18 10:35:43 字数 650 浏览 2 评论 0原文

我有一个 QGraphicsScene,上面绘制有图形和文本。当我尝试打印时,图形很好,但文本使用以磅为单位定义的字体大小,因此当我将 QPainter 传递给它时 scene->render() > 使用 QPrinter 初始化,具有非常大的文本。

我该如何打印带有文本的 QGraphicsScene

编辑:

这是我当前的打印代码,其中 scene_ 是我的 QGraphicsScene 的自定义子类:

  QPrinter printer(QPrinter::HighResolution);
  QPrintDialog dialog(&printer, this);
  dialog.exec();
  std::cout << printer.resolution() << std::endl;
  QPainter painter(&printer);
  scene_->render(&painter);

std:cout 行似乎没有任何区别。打印机仍然认为文本很大,因此对于每个文本项,仅打印第一个字母的一小部分。

I have a QGraphicsScene that has graphics as well as text drawn on it. When I try to print, the graphics are fine, but the text is using a font size defined in points, so scene->render() when I pass it a QPainter initialized with a QPrinter, has VERY large text.

How am I supposed to print a QGraphicsScene that has text on it?

edit:

Here is my current printing code, where scene_ is my custom subclass of QGraphicsScene:

  QPrinter printer(QPrinter::HighResolution);
  QPrintDialog dialog(&printer, this);
  dialog.exec();
  std::cout << printer.resolution() << std::endl;
  QPainter painter(&printer);
  scene_->render(&painter);

The std:cout line doesn't appear to make any difference. The printer still thinks the text is huge, so for each text item only a tiny part of the first letter is printed.

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

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

发布评论

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

评论(2

奶茶白久 2024-09-25 10:35:43

QPrinter 文档 听起来像您必须指定字体大小(以像素为单位)才能使文本和图形匹配。请注意,QFont 有一个 setPixelSize 方法。

From the QPrinter docs it sounds like you have to specify font sizes in pixels to get text and graphics to match up. Note that QFont has a setPixelSize method.

絕版丫頭 2024-09-25 10:35:43

设置 QPrinter:

默认情况下,QPrinter 对象会初始化为屏幕分辨率(通常为 96 DPI),除非您在中指定 QPrinter::HighResolution然后构造函数将使用正在使用的打印机的分辨率。

如果您使用QPrintDialog 设置QPrinter 对象,则代码应如下所示:

QPrinter printer(QPrinter::HighResolution);
QPrintDialog dialog(&printer, this);
dialog.exec();
std::cout << printer.resolution() << std::endl;

此后,程序应输出所选打印机的DPI。在我的例子中,它打印出 600。

如果您不使用 QPrintDialog,则应使用 QPrinter 构造函数(如上所示),然后调用 setResolution(DPI) 使用打印机的已知 DPI。

这应该会导致字体正确呈现。

更新:

现在周末到了,我终于有时间好好考虑这个问题了:)
尽管从技术上来说,设置 QPrinter 是正确的,但上述解决方案对于包含以点大小指定的文本的图形场景来说并不实用。由于所有图形项都是以像素坐标指定的,因此只有以像素为单位指定字体大小才有意义,以确保字体在与其他图形基元混合时完全按照预期显示。

无需担心不同显示器上的文本大小,因为图形项目本身与分辨率无关。视图可以指定比例转换来处理不同的分辨率和 DPI 监视器。

打印时,默认情况下,QPrinter 会进行缩放以使整个场景适合页面。这是有道理的,因为 600 DPI 打印机上的 100 x 100 正方形在纸张上的宽度应该是 1/6 英寸:)

Setting up the QPrinter:

By default, a QPrinter object is initialized to screen resolution (usually 96 DPI) unless you specify QPrinter::HighResolution in the constructor which will then use the resolution of the printer in use.

If you are setting up the QPrinter object using a QPrintDialog then the code should be something like this:

QPrinter printer(QPrinter::HighResolution);
QPrintDialog dialog(&printer, this);
dialog.exec();
std::cout << printer.resolution() << std::endl;

After this, the program should output the DPI of the selected printer. In my case it prints out 600.

If you aren't using the QPrintDialog, you should use the QPrinter constructor as shown above and then call setResolution(DPI) with the known DPI of your printer.

This should result in fonts that are rendered correctly.

Update:

Now that the weekend is here, I finally had time to properly consider this issue :)
Although technically correct for setting up a QPrinter, the above solution is not practical for Graphics scenes that include text specified in point sizes. Since all graphic items are specified in pixel coordinates, it only makes sense to specify font sizes in pixels as well to ensure that fonts appear exactly as expected when mixed with other graphic primitives.

There is no need to be concerned about the size of the text on different monitors as the graphics items themselves are not resolution independent. The view can specify scale translations to deal with different resolution and DPI monitors.

When printing, by default, the QPrinter scales to fit the entire scene to the page. Which makes sense since a 100 x 100 square on a 600 DPI printer is goint to be 1/6th of an inch wide on your paper :)

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