Using Qt, I am working on a tool that displays items in a QGraphicsScene
. Items are both polygons and QGraphicsTextItems
. The latter can contain hyperlinks.
Problem: I am trying to export the diagrams and I would like to keep the hyperlinks active.
Using QImage
renderers produce nice PNG but (obviously) no hyperlinks.
QSvgRenderer
produce SVG that also do not embed the hyperlinks.
QPrinter
allows to generate PDF that do the job (more or less) - hyperlinks are clickable, and that is what I need in principle. Except that I want to publish the diagram on the web, so PDF is not really an option for me.
I could not find any way render the QGraphicsScene
in an HTML form. Did I miss something? Is there a way to do that?
Thanks
EDIT
A code example:
from PySide import QtGui, QtCore
app = QtGui.QApplication([])
scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView(scene)
item = QtGui.QGraphicsTextItem()
item.setOpenExternalLinks(True)
item.setHtml('<a href="http://stackoverflow.com">Hello</a>')
item.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)
scene.addItem(item)
view.show()
app.exec_()
This example displays a scene with a clickable hyperlink (that opens the link with the default system application). In my application the scene also contains some graphical item but I think this is sufficient to get the idea: I would like to have this scene rendered (or printed or exported) in HTML so that the links would remain clickable.
The following code does the export to PDF:
pdf_printer = QPrinter()
pdf_printer.setOutputFormat(QPrinter.PdfFormat)
pdf_printer.setPaperSize(rect.size().toSize(), QPrinter.Point)
pdf_printer.setFullPage(True)
pdf_printer.setOutputFileName(filename)
pdf_painter = QPainter()
pdf_painter.begin(pdf_printer)
scene.render(pdf_painter, source=rect)
pdf_painter.end()
And the resulting PDF does contains the active hyperlinks - this is what makes me thinks that all the mechanics is there. I just cannot find a way to export in HTML instead of PDF.