vendredi 20 février 2015

Qt: Unable to display QGraphicsSvgItem


class MyView : public QGraphicsView
{
Q_OBJECT

public:
MyView(QWidget *parent = 0);
private:
QList<QGraphicsSvgItem *> m_svgItems;
};

MyView::MyView(QWidget *parent)
: QGraphicsView(parent)
{
QGraphicsScene *s = new QGraphicsScene(this);
setScene(s);
s->setSceneRect(0, 0, 280, 140);

scene()->addEllipse(10,10,25,45); // I can see it

// Trying to add svg items
m_svgItems = QList<QGraphicsSvgItem *>();
for (unsigned int i = 0; i < 4; i++)
{
QString fileName = QString(":/svg_files/svg%1.svg").arg(i+1);
QFile file(fileName);
m_svgItems.append(new QGraphicsSvgItem(file.fileName()));
scene()->addItem(m_svgItems.at(i)); // NOTHING HAPPENS
}
}

Window::Window(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window)
{
ui->setupUi(this); // with a QGraphicsView area promoted to MyView
}


Do I have to do anything else to make the svg show up ? I am not familiar with them but got simple ones from w3schools and they seem to be parsed correctly. Example (to show that the svg should fit within the bounding rectangle):



<!DOCTYPE html>
<html>
<body>

<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>


I am trying to make the svg show up in the simplest way, just so I learn how to work with them (I need multiple svgs). I have looked at the examples/painting/svggenerator and svgviewer, and can't figure out what I am missing still.


I have also tried



for (unsigned int i = 0; i < 4; i++)
{
QString fileName = QString(":/svg_files/svg%1.svg").arg(i+1);
QSvgRenderer *renderer = new QSvgRenderer(fileName);
m_svgItems.append(new QGraphicsSvgItem());
m_svgItems.at(i)->setSharedRenderer(renderer);
scene()->addItem(m_svgItems.at(i)); // NOTHING HAPPENS
}


Reading through other questions, I tried to change the svg:



<!DOCTYPE html>
<xml>

<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Sorry, your browser does not support inline SVG.
</svg>

</xml>


Still nothing gets displayed


Aucun commentaire:

Enregistrer un commentaire