Qt5 Tutorial QGraphicsView and QGraphicsScene - 2020
In this tutorial, we will learn QGraphicsView and QGraphicsScene.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
QGraphicsView visualizes the contents of a QGraphicsScene in a scrollable viewport. QGraphicsView is part of the Graphics View Framework.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
The class serves as a container for QGraphicsItems. It is used together with QGraphicsView for visualizing graphical items, such as lines, rectangles, text, or even custom items, on a 2D surface. QGraphicsScene is part of the Graphics View Framework.
Qt->Qt Gui Application:
We need to put GraphicsView into our dialog box:
If we compile and run at this point, we get:
We see nothing but a view. That's the GraphicsView. We'll create a scene and the scene will be displayed with the items we're going to add. In other words, the QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Here is the modified dialog.h:
// dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QGraphicsScene> #include <QGraphicsView> #include <QGraphicsItem> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private: Ui::Dialog *ui; QGraphicsScene *scene; QGraphicsEllipseItem *ellipse; QGraphicsRectItem *rectangle; QGraphicsTextItem *text; }; #endif // DIALOG_H
Here is the implementation file, dialog.cpp:
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QBrush greenBrush(Qt::green); QBrush blueBrush(Qt::blue); QPen outlinePen(Qt::black); outlinePen.setWidth(2); rectangle = scene->addRect(100, 0, 80, 100, outlinePen, blueBrush); // addEllipse(x,y,w,h,pen,brush) ellipse = scene->addEllipse(0, -100, 300, 60, outlinePen, greenBrush); text = scene->addText("bogotobogo.com", QFont("Arial", 20) ); // movable text text->setFlag(QGraphicsItem::ItemIsMovable); } Dialog::~Dialog() { delete ui; }
- We created a QGraphicsScene object, scene:
scene = new QGraphicsScene(this);
- Then we put the scene to graphicsView:
ui->graphicsView->setScene(scene);
- Items were added to the scene, for example
scene->addText();
If we run the code, we get:
Note that the text is movable by setting like this:
text->setFlag(QGraphicsItem::ItemIsMovable);
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization