Python GUI – Pyside2 QGraphicsView & QGraphicsScene

In this Python GUI article we are going to learn Pyside2 QGraphicsView & QGraphicsScene,

so the QGraphicsScene class provides a surface for managing a large number of 2D graphical

items.

 

 

 

Also you can check more Python GUI Development tutorials with different libraries.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: wxPython GUI Development Tutorials 

4: Kivy GUI Development Tutorials

 

 

 

 

What is QGraphicsScene ? 

According to Qt For Python Documentation , the QGraphicsScene 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 also

provides functionality that lets you efficiently determine both the location of items, and for

determining what items are visible within an arbitrary area on the scene. With the QGraphicsView

widget, you can either visualize the whole scene, or zoom in and view only parts of the scene.

 

 

Note: QGraphicsScene has no visual appearance of its own; it only manages the items. You

need to create a QGaphicsView widget to visualize the scene.

 

 

 

 

 

Complete source code for Python GUI – Pyside2 QGraphicsView & QGraphicsScene 

 

 

 

 

 

So these are the import that we need for this article, you can see that we have imported QGraphicsView and QGraphicsScene classes.

 

 

 

 

 

When you are going to develop GUI applications, you need to give title for the window and

also you need to set the x and y position of the window with the width and height of the window.

 

 

 

 

These are the two QPushButtons that we are going to use,  also you can see that we have

connected the clicked signal of the buttons with the methods or slots that we will create.

 

 

 

 

In here we have created the object of QGraphicsScene. 

 

 

 

 

These are the brush colors and the pen style that we will use.

 

 

 

 

 

As i have said that QGraphicsScene is used for adding items, in here we are adding three

items to our scene, an ellipse, a rectangle and a text.

 

 

 

 

 

If you want to move your items by clicking, you can use ItemIsMovable flag, as we want to

make movable our rectangle and ellipse.

 

 

 

 

 

As i have said that for visualizing QGraphicsScene you need to add your scene in the

QGraphicsView. in here we have created the object of QGraphicsView, also we have set the

geometry (x,y,width, height) for our view.

 

 

 

 

 

These are the methods that we have already connected this with the clicked signal of

our QPushButtons, these methods are just for rotating of the view.

 

 

 

 

 

For every Pyside2 application we need to create the object of QApplication.

 

 

 

 

And this is the starting point of the loop.

 

 

 

 

 

Run the complete code and this will be the result.

Python GUI - Pyside2 QGraphicsView & QGraphicsScene
Python GUI – Pyside2 QGraphicsView & QGraphicsScene

 

 

 

 

 

 

Also you can watch the complete video for this article.

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×