热门标签 | 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 - 如何扩展输出


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 中科院学位论文排版指南
    随着毕业季的到来,许多即将毕业的学生开始撰写学位论文。本文介绍了使用LaTeX排版学位论文的方法,特别是针对中国科学院大学研究生学位论文撰写规范指导意见的最新要求。LaTeX以其精确的控制和美观的排版效果成为许多学者的首选。 ... [详细]
  • 本文详细介绍了macOS系统的核心组件,包括如何管理其安全特性——系统完整性保护(SIP),并探讨了不同版本的更新亮点。对于使用macOS系统的用户来说,了解这些信息有助于更好地管理和优化系统性能。 ... [详细]
  • 创建项目:Visual Studio Online 入门指南
    本文介绍如何使用微软的 Visual Studio Online(VSO)创建和管理开发项目。作为一款基于云计算的开发平台,VSO 提供了丰富的工具和服务,简化了项目的配置和部署流程。 ... [详细]
  • 本文介绍了在Java环境中使用PDFBox和XPDF工具从PDF文件中提取文本内容的方法。重点讨论了处理中文字符集及解决相关错误的技术细节,特别是针对某些特定格式的PDF文件(如网上填写的报名表和下载的论文)遇到的问题及解决方案。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 本文介绍了在使用Visual Studio 2015进行项目开发时,遇到类向导弹出“异常来自 HRESULT:0x8CE0000B”错误的解决方案。通过具体步骤和实践经验,帮助开发者快速排查并解决问题。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 使用Python在SAE上开发新浪微博应用的初步探索
    最近重新审视了新浪云平台(SAE)提供的服务,发现其已支持Python开发。本文将详细介绍如何利用Django框架构建一个简单的新浪微博应用,并分享开发过程中的关键步骤。 ... [详细]
  • 深入了解 Windows 窗体中的 SplitContainer 控件
    SplitContainer 控件是 Windows 窗体中的一种复合控件,由两个可调整大小的面板和一个可移动的拆分条组成。本文将详细介绍其功能、属性以及如何通过编程方式创建复杂的用户界面。 ... [详细]
  • 本文详细介绍了如何在 Windows 环境下使用 node-gyp 工具进行 Node.js 本地扩展的编译和配置,涵盖从环境搭建到代码实现的全过程。 ... [详细]
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社区 版权所有