热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Qt图表在PDF上呈现问题-QtChartsrenderingproblemsonaPDF

ImusingQtchartsmoduletodrawapiechartdirectlyonaPDFfile.我正在使用Qt图表模块直接在PDF文件上绘制饼图。H

I'm using Qt charts module to draw a pie chart directly on a PDF file.

我正在使用Qt图表模块直接在PDF文件上绘制饼图。

Here's the problem:

这是问题所在:

  • For some unknown reason, the chart needs to be displayed with show() before it's rendered to the PDF for it's size to be OK (left image).

    由于某些未知原因,图表需要在显示为PDF之前以show()显示,因为它的大小正常(左图)。

  • On the other hand, I don't want to have to display every chart on the screen since my application generates a lot of them. However, if the chart is not displayed in a window with show(), then the drawing gets too small in the PDF (right image) even though the size of the chart is properly set with resize().

    另一方面,我不想在屏幕上显示每个图表,因为我的应用程序会生成很多图表。但是,如果图表未显示在带有show()的窗口中,则即使使用resize()正确设置图表的大小,PDF(右图)中的图形也会变得太小。

(black borders were added to these images to improve visualization)

(这些图像中添加了黑色边框以改善可视化)

Displaying all charts on a window before they are rendered to the PDF is not an option. The fact that the chart needs to execute show() for QPainter to draw it to the PDF correctly seems to indicate that without it, QPainter ignores the chart's dimension.

在窗口显示为PDF之前显示所有图表不是一种选择。图表需要执行show()以使QPainter正确地将其绘制到PDF这一事实似乎表明,如果没有它,QPainter将忽略图表的维度。

On a side note, show() opens the window but it takes several seconds for the chart to appear, so rendering is very very slow, another reason for me not to want to display the charts.

在旁注中,show()打开窗口但是图表显示需要几秒钟,因此渲染非常慢,这是我不想显示图表的另一个原因。

So here are my main questions:

所以这是我的主要问题:

  • Are these bugs or am I missing something?
  • 这些错误还是我错过了什么?
  • If not, what would be the proper way to specify the size and (x,y) position of the drawing (in the PDF)?
  • 如果不是,那么指定图纸的尺寸和(x,y)位置的正确方法是什么(在PDF中)?

Here is a Minimal, Complete, and Verifiable example...

这是一个最小,完整,可验证的例子......

main.cpp:

main.cpp中:

#include 
#include 
#include 
#include 
#include 
#include 


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QtCharts::QChartView* chartView = new QtCharts::QChartView();
    chartView->setRenderHint(QPainter::Antialiasing);
    chartView->resize(640, 480);

    QtCharts::QChart* chart = chartView->chart();
    chart->setTitle("Beautiful Pie Chart");
    chart->legend()->hide();

    QtCharts::QPieSeries* series = new QtCharts::QPieSeries();
    float hits = 73.0f, misses = 27.0f;
    series->append("Hits", hits);
    series->append("Misses", misses);

    QtCharts::QPieSlice* hit_slice = series->slices().at(0);
    hit_slice->setBrush(QColor(87, 147, 243));  // blue

    QtCharts::QPieSlice* miss_slice = series->slices().at(1);
    miss_slice->setBrush(QColor(221, 68, 68)); // red

    chart->addSeries(series);

    // Due to Qt bug, must show() the chart before render()
    // or it will be draw too tiny in the PDF by QPainter
    chartView->show();

    QPdfWriter writer("out.pdf");
    writer.setCreator("https://stackoverflow.com/users/176769/karlphillip");
    writer.setPageSize(QPagedPaintDevice::A4);
    QPainter painter(&writer);

    chartView->render(&painter);

    painter.end();

    return a.exec();
}

QtCharts_PDF.pro:

QtCharts_PDF.pro:

QT       += core gui charts

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QtCharts_PDF
TEMPLATE = app
SOURCES += main.cpp

2 个解决方案

#1


6  

One way to bypass this problem is to create a QPixmap from the QChartView and draw the pixmap into the PDF:

绕过此问题的一种方法是从QChartView创建QPixmap并将像素图绘制到PDF中:

    QPixmap pix = chartView->grab();
    int h = painter.window().height()*0.4;
    int w = h * 1.3;
    int x = (painter.window().width() / 2) - (w/2);
    int y = (painter.window().height() / 2) - (h/2);
    chartView->resize(w, h);
    painter.drawPixmap(x, y, w, h, pix);

This is kinda like taking a screenshot of the widget and rendering it to the file.

这有点像获取窗口小部件的屏幕截图并将其呈现给文件。

QPainter.drawPixmap() let's you specify the size and location of the drawing in the PDF. It's not ideal, but it will do for now. I know, it's a hack, it works, but I'm still looking for a better solution.

QPainter.drawPixmap()让您指定PDF中图形的大小和位置。这不是理想的,但它现在会做。我知道,这是一个黑客,它的工作原理,但我仍然在寻找更好的解决方案。

#2


2  

I think this could be a scaling issue as I encountered a similar problem with my output being printed much smaller than expected. QPdfWriter has a logical unit of a 'dot' where the default resolution is 1200 dots per inch. You need to decide how to map between the size of the QChartView and its printed appearance. The QPdfWriter will map a pixel to a dot by default. You want to set a scaling of 1200/pixelsPerInch

我认为这可能是一个扩展问题,因为我遇到了类似的问题,我的输出打印比预期的要小得多。 QPdfWriter的逻辑单元为'dot',默认分辨率为每英寸1200点。您需要决定如何在QChartView的大小和其打印外观之间进行映射。默认情况下,QPdfWriter会将像素映射到点。您想要设置1200 / pixelsPerInch的缩放比例

For sample code see my other answer here: QTextDocument, QPdfWriter - how to scale output

有关示例代码,请参阅我的其他答案:QTextDocument,QPdfWriter - 如何扩展输出


推荐阅读
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
  • IamnewtoAngularandFlot,butamexperiencedwithJqueryandJavascript.Iamabitconfusedabo ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • 本文介绍了解决mysql 5.1启动问题的方法,通过修改my.ini文件中的相关配置,包括innodb_data_home_dir和skip-innodb等,可以解决启动问题。同时还介绍了如何调整内存池来存储metadata信息。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Android获取app应用程序大小的方法
    Android获取app应用程序大小的方法-Android对这种方法进行了封装,我们没有权限去调用这个方法,所以我们只能通过AIDL,然后利用Java的反射机制去调用系统级的方法。 ... [详细]
  • 学习笔记17:Opencv处理调整图片亮度和对比度
    一、理论基础在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式:在图像像素中其中:参数f(x)表示源图像像素。参数g(x)表示输出图像像素。 ... [详细]
  • 如何在Google Chart中的工具提示中自定义内容 ... [详细]
author-avatar
手机用户2602883655
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有