Questions tagged [qgraphicsitem]

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.

It provides a light-weight foundation for writing your own custom items. This includes defining the item's geometry, collision detection, its painting implementation and item interaction through its event handlers. QGraphicsItem is part of The Graphics View Framework

For convenience, Qt provides a set of standard graphics items for the most common shapes: ellipse, line, path, pixmap, polygon, rectangle, text.

All of an item's geometric information is based on its local coordinate system. The item's position, pos(), is the only function that does not operate in local coordinates, as it returns a position in parent coordinates. The Graphics View Coordinate System describes the coordinate system in detail.

You can set whether an item should be visible (i.e., drawn, and accepting events), by calling setVisible(). Hiding an item will also hide its children. Similarly, you can enable or disable an item by calling setEnabled(). If you disable an item, all its children will also be disabled. By default, items are both visible and enabled. To toggle whether an item is selected or not, first enable selection by setting the ItemIsSelectable flag, and then call setSelected(). Normally, selection is toggled by the scene, as a result of user interaction.

Features

Transformations

QGraphicsItem supports projective transformations in addition to its base position, pos(). There are several ways to change an item's transformation. For simple transformations, you can call either of the convenience functions setRotation() or setScale(), or you can pass any transformation matrix to setTransform(). For advanced transformation control you also have the option of setting several combined transformations by calling setTransformations().

Painting

The paint() function is called by QGraphicsView to paint the item's contents. The item has no background or default fill of its own; whatever is behind the item will shine through all areas that are not explicitly painted in this function. You can call update() to schedule a repaint, optionally passing the rectangle that needs a repaint. Depending on whether or not the item is visible in a view, the item may or may not be repainted; there is no equivalent to QWidget::repaint() in QGraphicsItem.

Sorting

All items are drawn in a defined, stable order, and this same order decides which items will receive mouse input first when you click on the scene. Normally you don't have to worry about sorting, as the items follow a "natural order", following the logical structure of the scene.

Events

QGraphicsItem receives events from QGraphicsScene through the virtual function sceneEvent(). This function distributes the most common events to a set of convenience event handlers.

762 questions
9
votes
2 answers

Selection of items in QGraphicsScene

I am trying to understand how it is possible to redefine the way items are selected and transformed (once selected) in a QGraphicsScene. For example changing length of a line, move a line, change a polygon by moving one of its points. I have created…
Francesco
  • 481
  • 2
  • 4
  • 16
9
votes
2 answers

Qt: resize a QGraphicsItem (boundingRect()) into a QGraphicsScene with the mouse

I would like to resize the boundingRect() of my QGraphicsItem using the mouse. To build this I found this topic. So I managed to make it work for the right, the bottomright and the bottom of my boundingRect() following the idea of this the…
Jeanstackamort
  • 309
  • 2
  • 4
  • 16
8
votes
2 answers

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call

I created a simple program that demonstrates the runtime error I'm getting with my Qt application that uses multiple inheritance. The inheritance tree looks like: QGraphicsItem (abstract) \ QGraphicsLineItem MyInterface (abstract) …
Ben Burnett
  • 383
  • 2
  • 4
  • 11
8
votes
1 answer

Why does QGraphicsItem::scenePos() keep returning (0,0)

I have been toying with this piece of code: QGraphicsLineItem * anotherLine = this->addLine(50,50, 100, 100); qDebug() << anotherLine->scenePos(); QGraphicsLineItem * anotherLine2 = this->addLine(80,10, 300, 300); qDebug() <<…
Extrakun
  • 19,057
  • 21
  • 82
  • 129
7
votes
2 answers

QT: trouble with qobject_cast

I have derived QGraphicsItem and QGraphicsScene classes. I want the items to be able to call scene() and get a derviedGraphicsItem * instead of a QGraphicsItem *, so I reimplemented QGraphicsScene::itemAt to return a derived pointer. DerivedItem*…
weevilo
  • 81
  • 1
  • 1
  • 4
7
votes
2 answers

Forcing QGraphicsItem To Stay Put

I have a QGraphicsScene that is filled with QGraphicsItems, and the user is able to pan and zoom around to inspect all of the various QGraphicsItems. However, I would like to have a QGraphicsTextItem label that always stays put at the top left of…
rcv
  • 6,078
  • 9
  • 43
  • 63
7
votes
5 answers

Qt - is there a way to transform a graphic item into a pixmap?

I want to create a pixmap from a graphicObject, so i can set the pixmap as an icon I have a Block class derived from QGraphicsPathItem and i tried using: Block *block = new Block(); QRect rect = block->boundingRect().toRect(); QPixmap…
Gabriel_Br
  • 119
  • 1
  • 9
7
votes
3 answers

restrict movable area of qgraphicsitem

Is there a way to restrict the area where a QGraphicsItem like QRect can be moved when setFlag(ItemIsMovable) is set? I'm new to pyqt and trying to find a way to move an item with the mouse, and the restrict it to only vertically/horizontally.
quaddro
  • 71
  • 1
  • 2
7
votes
2 answers

Resizing and rotating a QGraphicsItem results in odd shape

I can't understand how scaling and rotation are applied to QGraphicsItem. I need to be able to apply rotation and scaling (not necessarily keeping aspect ratio) and I get fully unexpected results. Rotation must be around the item center. I seem to…
Thalia
  • 13,637
  • 22
  • 96
  • 190
7
votes
5 answers

Qt: having selected items appear on top in QGraphicsScene

So i have a QGraphicsScene with various items. Some of the can take the same coordinates in the scene. When I display the scene the ones displayed on top are the ones which were added last. Is there a way to make a certain item always appear on top…
Silnik
  • 328
  • 2
  • 4
  • 14
7
votes
1 answer

Python Qt: Interactive Re-Sizable QGraphicsItem, mouse hover area not resizing

I am trying to build a python class around QGraphicsRectItem (PySide or PyQt4) that provides mouse interaction through hover-overs, is movable, and re-sizable. I have pretty much everything working except: For some reason, it seems as if the mouse…
Onlyjus
  • 5,799
  • 5
  • 30
  • 42
7
votes
2 answers

In ScrollHandDrag mode of QGraphicsView, How to stop movement of QGraphicsItems on scene?

I have multiple QGraphicsItems in scene spread across different parts of scene. In application there are different modes in one of mode user can scroll the scene (palm drag mode). To achieve scrolling over scene I set dragMode of QGraphicsView to…
Anwar Shaikh
  • 1,591
  • 3
  • 22
  • 43
7
votes
1 answer

How to make a QGraphicsItem show the background in a QGraphicsScene?

In a QGraphicsScene, I have a background set with a few QGraphicsItems on top of it. These graphics items are of arbitrary shape. I'd like to make another QGraphicsItem, i.e. a circle, which when placed over these items will essentially show the…
JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
6
votes
4 answers

QGraphicsItem selection

Does a simple method exist to select part of a QGraphicsItem (like for a selection, with a dashed border for example), despite its position in the QGraphicsScene I've found QGraphicsItem::ItemIsSelectable but it doesn't help me much. Thx
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
6
votes
1 answer

Forcing QGraphicsItem to use z value and ignore the parent child relationship

I have problem using QGraphicsItem class. There is a base_parent, base_part, part_1, part_2. The base_parent is the parent of the base_part. part_1 and part_2 are children of the base_part. What I want to do is to set the Z value of this items in a…
Arwen
  • 415
  • 1
  • 5
  • 14
1
2
3
50 51