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
23
votes
2 answers

QGraphicsItem doesn't receive mouse hover events

I have a class derived from QGraphicsView, which contains QGraphicsItem-derived elements. I want these elements to change color whenever the mouse cursor hovers over them, so I implemented hoverEnterEvent (and hoverLeaveEvent): void…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
18
votes
3 answers

Can I get mouse events in a QGraphicsItem?

I have subclassed QGraphicsRectItem, and it's not receiving any mouse events. I've seen other questions similar to this say I need to enable mouse tracking, but setMouseTracking is in QWidget, and QGraphicsItem does not appear to be a QWidget. I've…
Almo
  • 15,538
  • 13
  • 67
  • 95
16
votes
2 answers

How to optimize QGraphicsView's performance?

I'm developing a CAD application using Qt 5.6.2 that is required to run in cheap computers at the same time that it needs to handle thousands of items in the same scene. Because of that, I had to make a lot of experimentations in order to get the…
Adriel Jr
  • 2,451
  • 19
  • 25
15
votes
3 answers

Find screen position of a QGraphicsItem

Use case: This should be a fairly common problem. In a normal QMainWindow with QMdiArea lives an mdiChild with a QGraphicsView. This view displays a QGraphicsScene with QGraphicsItems inside. A right-click at one of these items selects (focusses)…
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
15
votes
1 answer

How does QGraphicsItem::prepareGeometryChange() work?

According to the Qt docs, it "Prepares the item for a geometry change. Call this function before changing the bounding rect of an item to keep QGraphicsScene's index up to date. prepareGeometryChange() will call update() if this is necessary." What…
Anthony
  • 8,570
  • 3
  • 38
  • 46
14
votes
3 answers

Prevent QGraphicsItem from moving outside of QGraphicsScene

I have a scene which has fixed dimensions from (0;0) to (481;270): scene->setSceneRect(0, 0, 481, 270); Inside of it, I have a custom GraphicsItem and I can move it thanks to the flag ItemisMovable, but I would like it to stay within the Scene; I…
Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32
12
votes
1 answer

Draw an item in a static location relative to the QGraphicsView

I would like to draw a notification that always appears in the upper right corner of my QGraphicsView. However, QGraphicsItems positions are specified in scene coordinates, so if the user panned/zoomed to view a different part of the scene, this…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
12
votes
1 answer

what is the qtransform in QGraphicsScene::itemAt()

I create a custom QGraphicsItem. And overwrite the boundingRect() and paint(). QRectF myTile::boundingRect() const { return QRectF(xPos*10, yPos*10, 10, 10); } void myTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget…
lancellx
  • 285
  • 1
  • 5
  • 15
12
votes
2 answers

How to get notifications from QGraphicsScene on addItem or itemChange?

In my project I'm using a QGraphicsScene and adding / removing items all over the code. Now I want to get notified whenever a QGraphicsItem gets added or removed. Many Qt classes have notification signals or at least virtual functions that get…
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
11
votes
2 answers

Alternatives to static and global in C++?

I have a class instance that needs to be accessed by some other classes. It would be quite cumbersome to pass the instance always down the construction chain. I tried to avoid global variable, since people tend to advise against this. I thought I…
Sharky Bamboozle
  • 1,066
  • 16
  • 28
11
votes
1 answer

Adjusting QPen thickness when scaling QGraphicsView?

One amazing feature of QGraphicsView is its ability to scale itself with its scene's content (every QGraphicsItem inserted in the scene actually). The QPixmap that I have inserted scales correctly, meaning that if I provide a scale factor of 4x4…
Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32
10
votes
3 answers

How to stretch scene to fit graphics view?

I am using Qt and C++ as my environment. I draw a QGraphicsView in my UI, then create a scene and add lines to that it. I run through an array of 5000 points and draw lines connecting each point. QGraphicsScene *scene = new QGraphicsScene(); QPen…
JohnStudio
  • 221
  • 1
  • 7
  • 13
10
votes
4 answers

Customizing shape of bounding rect

I am drawing a line using mouse clicks. The line is drawn using paint function as: painter->drawLine(start_p, end_p); The bounding rect of line is defined as: QRectF Line::boundingRect() const { // bounding rectangle for line return…
Kamalpreet Grewal
  • 822
  • 1
  • 13
  • 28
10
votes
1 answer

Why is it more efficient to remove a QGraphicsItem from its scene before destroying it?

According to the QGraphicsItem destructor documentation, "It is more efficient to remove the item from the QGraphicsScene before destroying the item." Why is that? I can't think of how it could make a difference. And if it did make a difference,…
Anthony
  • 8,570
  • 3
  • 38
  • 46
10
votes
2 answers

What method is called when a QGraphicsItem get selected

Probably a trick question, but I can't find the answer. I need to know when a QGraphicsItem gets selected. There must be a method that's called. I know QGraphicsItem::itemChange() but it's called too often. Is there a better method ? thx edit :…
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
1
2 3
50 51