Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between drawing with QPainter and (QGraphicsView + QGraphicsScene)

Tags:

c++

qt

I am new to the QT libraries, and I wanted to know what the difference is between QPainter and QGraphicsScene.

From what I understand by seeing voidrealms videos with QPainter and with QGraphicsView + QGraphicsScene with both you can draw complex 2d objects.

The main difference being in the latter have an arbitrarily large scene which we view through a "window" (I believe Qt guys call it viewport?) and that this scene can be scrolled with side-bars. Also we can interact with drawings made in a GraphicsView widget, say with a mouse, but in a QPainter we cannot.

Is this correct?

In what use cases do we use QPainter and when do we use QGraphicsScene+QGraphicsView? I am primarily interested in making animations of some algorithms in computational geometry, so it seems I will be working with the latter.

I am using Qt5.6.

like image 528
smilingbuddha Avatar asked Apr 03 '16 18:04

smilingbuddha


2 Answers

QPainter is what lets you manipulate pixels on a widget. QGraphicsView uses it internally to render the contents of the scene. If you use any custom items in a scene, you'll have to write your own rendering code that uses QPainter to render them. The graphics scene framework lets you manage your geometry in a tree of items, and provides for easy manipulation of the items. No matter what you do, it's still always QPainter that does the drawing. If your geometry is simple or doesn't need to be interactive and/or hierarchical, use QPainter directly. Otherwise, use the graphics scene, or QML's scene geometry that's then rendered via OpenGL.

like image 198
Kuba hasn't forgotten Monica Avatar answered Oct 23 '22 10:10

Kuba hasn't forgotten Monica


QGraphicsItem itself uses QPainter for drawing, so your question is ill-formed.

Qt offers 3 different APIs for graphics - QWidget based, QGraphicsScene stack based and QtQuick based.

QWidget is for "typical" GUI rectangular elements, buttons, checkboxes, drop down menus and whatnot. Widgets are QObject derived so you get signals/slots and such. It is optimal for typical user interface items, not so much for custom graphics, although they are still completely possible to implement.

QGraphicsScene as the name implies, is a graphics scene, you can scroll, scale, rotate the scene, the scene and the view are separate objects, drawing itself is identical to widgets, but the paradigm is not the typical for widgets "draw a GUI", also QGraphicsItem itself is not QObject derived, so you don't have the signals/slots and such, although you can use QGraphicsObject if you need them. The regular graphics item is more lightweight, supports LOD drawing and some extra functionality not part of the widgets API. There are a few stock graphics items such as lines, rectangles and whatnot, similar to how there are stock widgets, for everything custom, you have to implement your own painting using QPainter, just like with widgets. Being more lightweight and supporting LODs, you can have a significantly higher object count than widgets.

QtQuick is the most recent graphics API, available since Qt5, it uses QML rather than C++, and is very easy and fast to develop and prototype in it. Animations and custom graphics elements are fastest and easiest to implement with QML. It can be extended with custom C++ types, including graphics items, either using QPainter or the QML scenegraph API, the latter of which is a little more complex to extend. You also have a Canvas element, which has an API similar to QPainter which you can use directly. This API is heavier than the graphics scene, even heavier than the widgets because of the extra functionality, but thanks to the scenegraph, graphics performance is usually much better than widgets or the graphics scene, even if you combine them with OpenGL.

like image 25
dtech Avatar answered Oct 23 '22 09:10

dtech