我使用Qt5显示QwtPlot3D的数据图。这些图是在几次子类QGLWidget上呈现的。有问题的地块看起来与此相似:
我使用以下代码按照其他来源的指示编写文件,方法是将QGLWidget渲染到QPainter中,当我调用QPainter::begin
时,QPainter接收QSvgGenerator。类似的过程适用于编写光栅图像,但SVG文件似乎是空的(但它们会写入)。
请注意,currentPlot变量属于Plot类,它继承了SurfacePlot,后者继承了继承QGLWidget的Plot3D。
void PlotWindow::writeCurrentPlotToSVG(const QString& directory,
const QString& filename)
{
QSvgGenerator generator;
QString ext = ".svg";
QString filepath = buildPath(directory, filename, ext);
generator.setFileName(filepath);
generator.setSize(currentPlot->size());
generator.setViewBox(currentPlot->rect());
generator.setTitle(currentPlot->title());
generator.setDescription("description");
QPainter painter;
painter.begin(&generator);
currentPlot->render(&painter);
painter.end();
}
生成的文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="225.778mm" height="169.333mm"
viewBox="0 0 640 480"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
<title>Plot_0</title>
<desc>description</desc>
<defs>
</defs>
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
</g>
</svg>
在Internet Explorer和Mozilla Firefox中查看文件不显示任何内容。我已经检查过显示矩形和传递给生成器的大小值是否正确。在这一点上,我不知道还能做什么。
提前感谢您的帮助。
答案 0 :(得分:0)
SVG输出矢量图形,OpenGL将位图数据输出到帧缓冲区,因此不能直接从QGLWidget获取可缩放的矢量图形。我希望QSvgGenerator也能获取OpenGL内容(至少作为位图),但它似乎没有这样做......
我在渲染屏幕外的QGLWidget时遇到了类似的问题。我的解决方法是首先渲染()整个小部件(你不会得到任何OpenGL内容),然后使用一个单独的FBO渲染QGLWidget,然后在第一个渲染的顶部绘制我从FBO获得的图像。 />
这些方面的东西:
//find your QGL-sub-widget
qglWidget = ...
//render parent widget into the svg
QPainter painter(&svgGenerator);
widget->render(&painter);
//create/resize FBO. make the OpenGL context current, so the FBO is created in the correct context
qglChild->makeCurrent();
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setSamples(4);
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
fbo = new QOpenGLFramebufferObject(newWindowRect.size(), fboFormat);
//get the position and size of the QGLWidget within the widget
QRect newWindowRect = GetChildRectInParent(widget, qglChild);
//the position is reliably off by one pixel in x and y, so adjust it
newWindowRect.adjust(-1, -1, -1, -1);
//make the OpenGL context current, so the FBO is created in the correct context
qglChild->makeCurrent();
//bind FBO for rendering to it
fbo->bind();
//Painting to an FBO using OpenGL content in Qt is only possible with a QOpenGLPaintDevice
QOpenGLPaintDevice fboPaintDev(newWindowRect.size());
QPainter fboPainter(&fboPaintDev);
fboPainter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);
//now render the QGraphicsView
qglChild->render(&fboPainter);
fboPainter.end();
fbo->release();
//grab image from FBO and paint it at the correct position in the pixmap we grabbed from the parent widget
QPixmap openGLPixmap = QPixmap::fromImage(fbo->toImage());
painter.drawPixmap(newWindowRect, openGLPixmap);
painter.end();
delete fbo;