QImage ImageView::renderSvg(const QSize &imageSize) const { QImage image(imageSize, QImage::Format_ARGB32); image.fill(Qt::transparent); QPainter painter; painter.begin(&image); #ifndef QT_NO_SVG QGraphicsSvgItem *svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem); QTC_ASSERT(svgItem, return image); svgItem->renderer()->render(&painter, QRectF(QPointF(), QSizeF(imageSize))); #endif painter.end(); return image; }
MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { // create a scene with items, // use the view to show the scene QGraphicsScene *scene = new QGraphicsScene(); QGraphicsView *gv = new QGraphicsView(this); gv->setScene(scene); if (gv->scene()) { printf("renc: scene is not null.\n"); // these two lines are at scene origin pos. scene->addLine(0, 0, 100, 0, QPen(Qt::red)); scene->addLine(0, 0, 0, 100, QPen(Qt::green)); /*QGraphicsRectItem *rect = */ scene->addRect(QRectF(0,0,50,50), QPen(Qt::red), QBrush(Qt::green)); // svg image QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(svgFile2); scene->addItem((svgItem)); svgItem->setPos(400, 0); bool rValid1 = svgItem->renderer()->isValid(); printf("--%d--\n", rValid1); // QWidget to scene QPushButton *btn = new QPushButton("button 1");// at (0,0) by default. QGraphicsProxyWidget *btnWidget = scene->addWidget(btn); btnWidget->setPos(0, 400); // { SvgPushButton *btn2 = new SvgPushButton(svgFile1); QGraphicsProxyWidget *btnWidget2 = scene->addWidget(btn2); btnWidget2->setPos(400, 400); } } else printf("renc: scene is null.\n"); //setCentralWidget(gv); setWindowTitle("Demo: graphics view");//QMainWindow setStyleSheet("background-color: rgb(100,120,50);"); }
void SymbolDataEditor::load(const QString & fileName) { loadSettings(); doc = QDomDocument("SVG"); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; if (!doc.setContent(&file)) { file.close(); return; } file.close(); //scale viewbox because it is necessary in SvgView::insertSymbol() QDomElement svgElement = doc.elementsByTagName("svg").item(0).toElement(); SvgView::scaleViewBox(svgElement); QByteArray scaledFile = doc.toString(0).replace(">\n<tspan", "><tspan").toUtf8(); QGraphicsSvgItem *symbolItem = new QGraphicsSvgItem(); symbolItem->setSharedRenderer(new QSvgRenderer(scaledFile)); QSizeF itemSize = symbolItem->renderer()->defaultSize(); clear(); //add changed item to the scene scene->setSceneRect(0, 0, itemSize.width() * sceneScale, itemSize.height() * sceneScale); symbolItem->setPos(scene->width() / 2 - itemSize.width() / 2.0, scene->height() / 2 - itemSize.height() / 2.0); scene->addItem(symbolItem); //autoscale graphicsview depending on items and views size limitScale(qMax(qreal(width()) / itemSize.width(), qreal(height()) / itemSize.height()) / currentScaleFactor); centerOn(symbolItem); setDragMode(QGraphicsView::ScrollHandDrag); }