4

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.

4

2 回答 2

3

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

于 2010-09-09T19:49:33.453 回答
1

设置 QPrinter:

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

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

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 英寸宽 :)

于 2010-09-10T02:08:10.510 回答