Hey Guys,
basically i want to render multiple layered Svg on the Graphics card, in 20-50ms update rate.
For Testing and understanding this i wrote some Code.
It works, but not as i expexcted.
It looks not really antialized. Also it should be resizable at runtime.
Header of my Widgets i want to place inside the QGraphics View
#ifndef SVGWIDGET_H_
#define SVGWIDGET_H_
#include <qwidget.h>
#include <qsvgrenderer.h>
/*!
* @brief This class represents a little extension of the QWidget to display a svg as a backgroundimage.
*/
{
public:
/*!
*Constructor with given parent Widget
* @param parent is the parent widget
*/
~SvgWidget();
/*!
* Sets the background to be displayed and resizes the widget to the
* background's size.
* repaints the widget if background is changed
* @param backgroundPath the path to the svg for the background
*/
void
setUpBackgroundSvg
(QString backgroundPath
);
protected:
/*!
* Paints the background of the widget.
* @param pe
*/
void
private:
/*!
* Renderer for the background
*/
};
#endif /* SVGWIDGET_H_ */
#ifndef SVGWIDGET_H_
#define SVGWIDGET_H_
#include <qwidget.h>
#include <qsvgrenderer.h>
/*!
* @brief This class represents a little extension of the QWidget to display a svg as a backgroundimage.
*/
class SvgWidget : public QWidget
{
public:
/*!
*Constructor with given parent Widget
* @param parent is the parent widget
*/
SvgWidget(QWidget * parent);
~SvgWidget();
/*!
* Sets the background to be displayed and resizes the widget to the
* background's size.
* repaints the widget if background is changed
* @param backgroundPath the path to the svg for the background
*/
void
setUpBackgroundSvg(QString backgroundPath);
protected:
/*!
* Paints the background of the widget.
* @param pe
*/
void
paintEvent(QPaintEvent *pe);
private:
/*!
* Renderer for the background
*/
QSvgRenderer background_;
};
#endif /* SVGWIDGET_H_ */
To copy to clipboard, switch view to plain text mode
source file of my little SvgWidget
#include <SvgWidget.h>
#include <qpainter.h>
#include <qlayout.h>
SvgWidget
::SvgWidget(QWidget * parent
) :{
}
SvgWidget::~SvgWidget()
{
}
void
SvgWidget
::setUpBackgroundSvg(QString backgroundPath
){
background_.load(backgroundPath);
this->resize(background_.defaultSize());
repaint();
}
void
{
if (background_.isValid())
{
background_.render(&painter);
}
}
#include <SvgWidget.h>
#include <qpainter.h>
#include <qlayout.h>
SvgWidget::SvgWidget(QWidget * parent) :
QWidget(parent),background_(this)
{
}
SvgWidget::~SvgWidget()
{
}
void
SvgWidget::setUpBackgroundSvg(QString backgroundPath)
{
background_.load(backgroundPath);
this->resize(background_.defaultSize());
repaint();
}
void
SvgWidget::paintEvent(QPaintEvent *pe)
{
if (background_.isValid())
{
QPainter painter(this);
background_.render(&painter);
}
}
To copy to clipboard, switch view to plain text mode
#ifndef MYVIEW_H_
#define MYVIEW_H_
#include <qgraphicsview.h>
#include "SvgWidget.h"
{
Q_OBJECT
SvgWidget* widget;
public:
virtual
~MyView();
};
#endif /* MYVIEW_H_ */
#ifndef MYVIEW_H_
#define MYVIEW_H_
#include <qgraphicsview.h>
#include "SvgWidget.h"
class MyView : public QGraphicsView
{
Q_OBJECT
SvgWidget* widget;
QTimer* timer_;
public:
MyView(QWidget* parent = 0);
virtual
~MyView();
};
#endif /* MYVIEW_H_ */
To copy to clipboard, switch view to plain text mode
#include "MyView.h"
#include <QGLWidget>
#include <qtimer.h>
{
widget = new SvgWidget(0);
widget->setUpBackgroundSvg(":/images/some.svg");
//some more layers will be set up here
scene()->addWidget(widget); //from here
setViewport(
update(); //to here
// if the code above is disabled, it looks better but is not rendered on the cpu right?
timer_->setInterval(20);
connect(timer_, SIGNAL(timeout()), this, SLOT(repaint()));
timer_->start();
}
MyView::~MyView()
{
}
#include "MyView.h"
#include <QGLWidget>
#include <qtimer.h>
MyView::MyView(QWidget* parent) :
QGraphicsView(parent)
{
setScene(new QGraphicsScene(this));
widget = new SvgWidget(0);
widget->setUpBackgroundSvg(":/images/some.svg");
//some more layers will be set up here
scene()->addWidget(widget); //from here
setViewport(
new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::DirectRendering)));
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
update(); //to here
// if the code above is disabled, it looks better but is not rendered on the cpu right?
timer_ = new QTimer(this);
timer_->setInterval(20);
connect(timer_, SIGNAL(timeout()), this, SLOT(repaint()));
timer_->start();
}
MyView::~MyView()
{
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <qapplication.h>
#include "MyView.h"
int
main(int argc, char *argv[])
{
MyView myView(0);
myView.repaint();
myView.show();
return app.exec();
}
#include <qapplication.h>
#include "MyView.h"
int
main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyView myView(0);
myView.repaint();
myView.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I'm very Happy for all suggestions how to draw a Svg on the GPU.
Thank You!
Bookmarks