Results 1 to 1 of 1

Thread: QGraphicsView QSvgRenderer and OpenGl

  1. #1
    Join Date
    Mar 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post QGraphicsView QSvgRenderer and OpenGl

    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
    Qt Code:
    1. #ifndef SVGWIDGET_H_
    2. #define SVGWIDGET_H_
    3.  
    4. #include <qwidget.h>
    5. #include <qsvgrenderer.h>
    6.  
    7. /*!
    8.  * @brief This class represents a little extension of the QWidget to display a svg as a backgroundimage.
    9.  */
    10. class SvgWidget : public QWidget
    11. {
    12. public:
    13. /*!
    14.   *Constructor with given parent Widget
    15.   * @param parent is the parent widget
    16.   */
    17. SvgWidget(QWidget * parent);
    18. ~SvgWidget();
    19.  
    20. /*!
    21.   * Sets the background to be displayed and resizes the widget to the
    22.   * background's size.
    23.   * repaints the widget if background is changed
    24.   * @param backgroundPath the path to the svg for the background
    25.   */
    26. void
    27. setUpBackgroundSvg(QString backgroundPath);
    28.  
    29. protected:
    30. /*!
    31.   * Paints the background of the widget.
    32.   * @param pe
    33.   */
    34. void
    35. paintEvent(QPaintEvent *pe);
    36.  
    37. private:
    38. /*!
    39.   * Renderer for the background
    40.   */
    41. QSvgRenderer background_;
    42. };
    43.  
    44. #endif /* SVGWIDGET_H_ */
    To copy to clipboard, switch view to plain text mode 


    source file of my little SvgWidget
    Qt Code:
    1. #include <SvgWidget.h>
    2. #include <qpainter.h>
    3. #include <qlayout.h>
    4.  
    5. SvgWidget::SvgWidget(QWidget * parent) :
    6. QWidget(parent),background_(this)
    7. {
    8. }
    9.  
    10. SvgWidget::~SvgWidget()
    11. {
    12. }
    13.  
    14. void
    15. SvgWidget::setUpBackgroundSvg(QString backgroundPath)
    16. {
    17. background_.load(backgroundPath);
    18.  
    19. this->resize(background_.defaultSize());
    20. repaint();
    21. }
    22.  
    23. void
    24. SvgWidget::paintEvent(QPaintEvent *pe)
    25. {
    26. if (background_.isValid())
    27. {
    28. QPainter painter(this);
    29. background_.render(&painter);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MYVIEW_H_
    2. #define MYVIEW_H_
    3.  
    4. #include <qgraphicsview.h>
    5. #include "SvgWidget.h"
    6.  
    7. class MyView : public QGraphicsView
    8. {
    9. Q_OBJECT
    10. SvgWidget* widget;
    11. QTimer* timer_;
    12. public:
    13. MyView(QWidget* parent = 0);
    14. virtual
    15. ~MyView();
    16. };
    17.  
    18. #endif /* MYVIEW_H_ */
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MyView.h"
    2. #include <QGLWidget>
    3. #include <qtimer.h>
    4.  
    5. MyView::MyView(QWidget* parent) :
    6. QGraphicsView(parent)
    7. {
    8. setScene(new QGraphicsScene(this));
    9.  
    10. widget = new SvgWidget(0);
    11. widget->setUpBackgroundSvg(":/images/some.svg");
    12. //some more layers will be set up here
    13. scene()->addWidget(widget); //from here
    14. setViewport(
    15. new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::DirectRendering)));
    16. setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    17. update(); //to here
    18. // if the code above is disabled, it looks better but is not rendered on the cpu right?
    19. timer_ = new QTimer(this);
    20. timer_->setInterval(20);
    21. connect(timer_, SIGNAL(timeout()), this, SLOT(repaint()));
    22. timer_->start();
    23. }
    24.  
    25. MyView::~MyView()
    26. {
    27. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include "MyView.h"
    3.  
    4.  
    5. int
    6. main(int argc, char *argv[])
    7. {
    8.  
    9. QApplication app(argc, argv);
    10. MyView myView(0);
    11. myView.repaint();
    12. myView.show();
    13. return app.exec();
    14.  
    15. }
    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!
    Last edited by HauckThalheim; 10th March 2014 at 17:59. Reason: updated contents

Similar Threads

  1. Slow OpenGL + QGraphicsView
    By ivigasin in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2011, 01:17
  2. QGraphicsView + opengl + Vsync
    By medved6 in forum Qt Programming
    Replies: 3
    Last Post: 11th March 2011, 17:20
  3. QGraphicsView and Opengl
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2010, 00:26
  4. Using OpenGL on QGraphicsView
    By strateng in forum Newbie
    Replies: 0
    Last Post: 30th March 2010, 02:55
  5. QGraphicsView, OpenGL and repainting/updating
    By Amargedon in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 13:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.