作者:手机用户2602883655 | 来源:互联网 | 2023-05-17 18:34
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 个解决方案