Layers on QGraphicsView?

Hi, I am creating an application that retrieves data from WFS and then displays these data layers in a QGraphicsView in widgets. Currently, all layers are rendered and added to the same view, which means that if I want to rotate the layer, it means redisplaying all this except this layer.

At the moment, adding a QGraphicsScene scene to the text of the graphic with ellipse elements and polygon elements. I am wondering if it is possible to add multiple scenes to a graphical representation or layers to a scene or something that would allow me to simply hide / show certain points / polygons from a check box or something that just hides a layer?

I know this is a little vague, but I would appreciate any help.

Thanks.

+4
source share
3 answers

Another way: QGraphicsItemGroup

Sort of:

 // Group all selected items together QGraphicsItemGroup *group = scene->createItemGroup(scene->selecteditems()); ... // Destroy the group, and delete the group item scene->destroyItemGroup(group); 

Thus, you can consider a group as a layer, and since the group also QGraphicsItem has all the functions, such as show () / hide (), etc.

UPDATE: changing Z-val for a group will allow you to implement things like "move layer up / down"

+5
source

You only need one QGraphicsScene , but the key here is that all QGraphicsItem and QGraphicsObject can be parent.

If you create a single QGraphicsItem or QGraphicsObject as the parent object, it does not need to draw anything, but it can be used as the root for the layer elements.

Therefore, a subclass of QGraphicsItem creates the QGraphicsItemLayer class, which does not display anything or add all the ellipses, polygons, etc. that are required on the same layer as the children of this QGraphicsItemLayer .

If you want to hide the layer, just hide the parent QGraphicsItemLayer object and all its children will be hidden too.

-------- Edited --------------

Inherit from QGraphicsItem : -

 class QGraphicsItemLayer : public QGraphicsItem { public: virtual QRectF boundingRect() { return QRectF(0,0,0,0); } virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) { } }; 

Create a layer element:

 QGraphicsItemLayer* pLayer = new QGraphicsItemLayer; 

Add the objects you want to the layer, note that pLayer is passed as the parent

 QGraphicsEllipseItem = new QGraphicsEllipseItem(pLayer); 

Assuming you created a QGraphicsScene with a pointer to it named pScene : -

 pScene->addItem(pLayer); 

Then when you want to hide the layer

 pLayer->hide(); 

Or display a layer: -

 pLayer->show(); 
+6
source

I think you could try to split your objects according to the z value: see setZValue . Then enter a mapping between the layer ID and indexing. A simple QStringList could do.

Of course, there are many details and variations that need to be considered for a practical solution.

+2
source

Source: https://habr.com/ru/post/1495475/


All Articles