{"id":555,"date":"2015-01-12T15:02:19","date_gmt":"2015-01-12T09:32:19","guid":{"rendered":"http:\/\/www.e-consystems.com\/blog\/linux-android\/?p=555"},"modified":"2023-08-15T12:34:12","modified_gmt":"2023-08-15T07:04:12","slug":"qt-graphics-view-framework","status":"publish","type":"post","link":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/qt-graphics-view-framework\/","title":{"rendered":"Qt Graphics View Framework"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Graphics View provides a surface for managing and interacting with a large number of custom-made graphical items, and a view widget for visualizing the items, with support for zooming and rotation.<\/p>\n<p>The Graphics View Framework comprises of three basic elements or classes namely QGraphicsScene, QGraphicsView and QGraphicswItem which helps the developer to use and understand the Graphics View Framework.<\/p>\n<ul>\n<li><strong>QGraphicsScene<\/strong><\/li>\n<\/ul>\n<p>Class for storing the widgets, handling event propagation(input from keyboard and touches) and managing item states. It represents a scene with items in it. QGraphicsScene also manages the state of item like selection, focus and acts as a container to different items placed on the scene.<\/p>\n<p>A QGraphicsScene object is flexible enough to include any number of QGraphicsItem objects and still maintain the efficiency in retrieving them. The basic idea here is to manage the events of each item you create and redraw them as needed. This way, you can make your own custom widgets with more visually appealing features.<\/p>\n<ul type=\"disc\">\n<li><strong>QGraphicsView<\/strong><\/li>\n<\/ul>\n<p>Displays the widgets of a scene, it basically visualizes content of a scene. The view widget is a scroll area, and provides scroll bars for navigating through large scenes. The view receives input events from keys and touches, and translates these to scene events (converting the coordinates used to scene coordinates where appropriate), before sending the events to the visualized scene.<\/p>\n<p>Graphics View provides an item-based approach to model-view programming, much like classes QTableView, QTreeView and QListView. Several views can observe a single scene, and the scene contains items of varying geometric shapes.<\/p>\n<ul>\n<li><strong>QGraphicsItem<\/strong><\/li>\n<\/ul>\n<p>A basic class for the graphical items on the scene. It can also represent a group of items. Graphics View provides several standard items for typical shapes, such as rectangles (QGraphicsRectItem), ellipses (QGraphicsEllipseItem) and text items (QGraphicsTextItem), but the most powerful QGraphicsItem features are available when one writes a custom item. QGraphicsItem is responsible for keyboard input, focus, drag and drop.<\/p>\n<p>Items live in a local coordinate system, and like QGraphicsView, it also provides many functions for mapping coordinates between the item and the scene, and from item to item. Also, like QGraphicsView, it can transform its coordinate system using a matrix: QGraphicsItem::transform(). This is useful for rotating and scaling individual items.<\/p>\n<p><strong>Sample Application:<\/strong><\/p>\n<p>Let us see how to draw a simple image using QT Graphics View Framework using simple images with a small example.<\/p>\n<p>The below example uses: QgraphicsItem for drawing the custom item, QGraphicsScene for managing the custom items and QGaphicsView for displaying the contents of the QGraphicsScene.<br \/>\n<strong>m_graphicsview<\/strong> visualizes the contents of the <strong>m_graphicsscene<\/strong> in a scrollable viewport.<\/p>\n<p>The scene in the view is set by using setScene() by passing the address of m_graphicsscene object.<br \/>\nOnce the scene and the view are created, the items can be added to the scene by calling addItem().<\/p>\n<p>The custom image is subclassed from QGraphicsItem and the image is drawn in the paint event of\u00a0\u00a0 SpinnerGraphics.<\/p>\n<p>\/\/ Class SpinnerGraphics is the custom item to be displayed on the screen<\/p>\n<p><strong>class SpinnerGraphics:public QObject,public QGraphicsItem<\/strong><br \/>\n<strong>{<\/strong><br \/>\nQ_OBJECT<\/p>\n<p>public:<br \/>\n&#8230;..<\/p>\n<p>protected:<br \/>\nvoid paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);<br \/>\nQRectF boundingRect() const;<\/p>\n<p><strong>};<\/strong><\/p>\n<p><strong>\/\/ Class SpinnerControl is the class containing the graphics scene and the graphics view<\/strong><br \/>\n<strong>class SpinnerControl: public QFrame<\/strong><br \/>\n<strong>{<\/strong><br \/>\nQ_OBJECT<\/p>\n<p>QGraphicsScene\u00a0 *m_graphicsscene;<br \/>\nQGraphicsView\u00a0\u00a0 *m_graphicsview;<\/p>\n<p>QBrush\u00a0 *m_brush;<br \/>\npublic:<br \/>\n&#8230;<br \/>\nSpinnerGraphics\u00a0*m_spinnerGraphics;<\/p>\n<p><strong>};<\/strong><\/p>\n<p><strong>SpinnerControl::SpinnerControl(QWidget *parent) :<\/strong><br \/>\n<strong>QFrame(parent)<\/strong><br \/>\n<strong>{<\/strong><br \/>\nm_graphicsscene = new QGraphicsScene();<br \/>\nm_graphicsview\u00a0 = new QgraphicsView(this);<br \/>\n\/\/ Set the current scene pointing to the created scene<br \/>\nm_graphicsview-&gt;setScene(m_graphicsscene);<\/p>\n<p>\/\/ set the area of the scene visualized by this view<br \/>\nm_graphicsscene-&gt;setSceneRect(0,0,311,261);<\/p>\n<p>m_spinnerGraphics = new SpinnerGraphics();<br \/>\nm_graphicsscene-&gt;addItem(m_spinnerGraphics);<\/p>\n<p>m_brush\u00a0 = new QBrush();<br \/>\nm_brush-&gt;setStyle(Qt::SolidPattern);<br \/>\nm_brush-&gt;setColor(QColor(15,15,15));<br \/>\nm_graphicsscene-&gt;setBackgroundBrush(*m_brush);<\/p>\n<p>\/\/the custom spinner<br \/>\nm_spinnerGraphics=new SpinnerGraphics();<br \/>\n\/\/add the custom item in the scene<br \/>\nm_graphicsscene-&gt;addItem(m_spinnerGraphics);<br \/>\n&#8230;<br \/>\n<strong>}<\/strong><\/p>\n<p><strong>void SpinnerGraphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)<\/strong><br \/>\n<strong>{<\/strong><br \/>\n\/\/images for custom spinners<br \/>\nQImage Compass(&#8220;:\/images\/course-bg.png&#8221;);<br \/>\nQImage grayArrow(&#8220;:\/images\/gray_arrow.png&#8221;);<br \/>\nQImage redArrow(&#8220;:\/images\/red_arrow.png&#8221;);<br \/>\npainter-&gt;drawImage(&#8230;);<br \/>\n&#8230;<br \/>\n<strong>}<\/strong><\/p>\n<p><strong>QRectF SpinnerGraphics::boundingRect() const<\/strong><br \/>\n<strong>{<\/strong><br \/>\nreturn QRectF(0,0,310,310);<br \/>\n<strong>}<\/strong><\/p>\n<p>By implementing mousePressEvent, mouseReleaseEvent and mouseMoveEvent we can even implement a way to change the position of the spinner (As shown in Fig).<\/p>\n<p><a href=\"https:\/\/www.e-consystems.com\/blog\/linux-android\/wp-content\/uploads\/2015\/01\/Qt-Graphics-View-Framework.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-559\" src=\"https:\/\/www.e-consystems.com\/blog\/linux-android\/wp-content\/uploads\/2015\/01\/Qt-Graphics-View-Framework-300x116.jpg\" alt=\"Qt-Graphics-View-Framework\" width=\"300\" height=\"116\" srcset=\"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-content\/uploads\/2015\/01\/Qt-Graphics-View-Framework-300x116.jpg 300w, https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-content\/uploads\/2015\/01\/Qt-Graphics-View-Framework-658x255.jpg 658w, https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-content\/uploads\/2015\/01\/Qt-Graphics-View-Framework.jpg 684w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>References<\/h2>\n<p>1. <a href=\"http:\/\/doc.qt.io\/qt-4.8\/graphicsview.html\" rel=\"nofollow\">http:\/\/doc.qt.io\/qt-4.8\/graphicsview.html<\/a><\/p>\n<p>For any technical support or queries feel free to write to us at <a href=\"mailto:sales@e-consystems.com\" target=\"_blank\" rel=\"noopener noreferrer\">sales@e-consystems.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Graphics View provides a surface for managing and interacting with a large number of&#8230;<\/p>\n","protected":false},"author":17,"featured_media":561,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[110,109,108,207,129,111],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/555"}],"collection":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":15,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":2874,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/555\/revisions\/2874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}