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

Detect when mouse enter QGraphicsItem with button down

I want to detect when the mouse cursor moves in over a QGraphicsItem while a mouse button is pressed, i.e. the button is pressed before the mouse enters the item. My first idea was to use hoverEnterEvent, but it doesn't seem to trigger when the left…
pafcu
  • 7,808
  • 12
  • 42
  • 55
6
votes
1 answer

QGraphicsItem leaves artifacts when changing boundingRect

My LineItem inheriting from QGraphicsLineItem can change its pen width. I have created a boundingRect that uses the QGraphicsLineItem::boundingRect adjusted by pads that get calculated based on pen width and arrows. It works. void…
Thalia
  • 13,637
  • 22
  • 96
  • 190
6
votes
2 answers

How can I wrap text in QGraphicsItem?

1) How can I wrap text in a QGraphicsTextItem to fit a fixed rectangle, with width and height ? Right now I am experimenting with creating a text, getting its bounding rectangle, and resizing it to fit the box - but I can't get wrapping. class TTT:…
Thalia
  • 13,637
  • 22
  • 96
  • 190
6
votes
2 answers

Drawing widgets (such as buttons) over QGraphicsView

How do I draw interactive widgets such as QButtons and Line Edits over a QGraphicsView? For ex, I have selected a region over an image in an image editing app which displays an image with QGraphicsView, and I want to annotate this region with a…
Aditya Bhatt
  • 63
  • 1
  • 1
  • 3
5
votes
3 answers

QGraphicsItem : emulating an item origin which is not the top left corner

My application is using Qt. I have a class which is inheriting QGraphicsPixmapItem. When applying transformations on these items (for instance, rotations), the origin of the item (or the pivot point) is always the top left corner. I'd like to change…
Jérôme
  • 26,567
  • 29
  • 98
  • 120
5
votes
1 answer

Qt/PyQt: QGraphicsItem vs. QGraphicsWidget geometry, position, mouse interaction

I am converting a larger program of QGraphicsItems to QGraphicsWidgets (let's call them item and widget for typing sake). Mouse hover fails now because the position and/or rect of the widgets are not the same as the old items. I've boiled down to…
user1034632
  • 61
  • 2
  • 5
5
votes
1 answer

Changing the color of a QGraphicsItem

I am working on a simple software, which has a GUI. (I'm using PyQt) I have 2 radiobuttons. If the first is selected, then by clicking on the Graphicsscene a GraphicsItem will be added to the scene. I would like to have a button, which would change…
Dàvid Nagy
  • 159
  • 3
  • 10
5
votes
2 answers

How to keep the size and position of QGraphicsItem when scaling the view?

I have some QGraphicsItems in the QGraphicsScene which should keep the same size and position when scaling. I've tried QGraphicsItem::ItemIgnoresTransformations but it turns out that the items get wrong positions. Below is a sample code: I have…
codeup
  • 83
  • 1
  • 7
5
votes
2 answers

QGraphicsItem rendering on Apple Retina Display

I'm adding support for the Apple Retina Display to my PyQt5 application. While I successfully managed to render high resolution icons (by adding the @2x suffix to all my .png files and setting the Qt.AA_UseHighDpiPixmaps in my QApplication), I'm…
Daniele Pantaleone
  • 2,657
  • 22
  • 33
5
votes
3 answers

How to add QLabel to QGraphicsItem

I have a QGraphicsItem that has text on it. I want this text to be editable, so that if the user double-clicks it, it will enter an edit mode. It seems like the easiest way to do this would be to change the text into a QLineEdit and let the user…
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
5
votes
1 answer

Fixed QGraphicsItem position, without changing behaviour of other QGraphicsItems in scene

This question is related to: Forcing QGraphicsItem To Stay Put I'd like to have a QGraphicsItem on a fixed location when moving around in the scene. The suggested solution is to override the void paintEvent(QPaintEvent*) of the sub-classed…
Man of One Way
  • 3,904
  • 1
  • 26
  • 41
5
votes
3 answers

How to remove border around QGraphicsItem when selected?

Pretty basic question but I couldn't find a solution through google. In QT when a graphics item is selected, there's a border around it. I was wondering how I can set this border to be invisible. Thanks.
JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
4
votes
1 answer

QGraphicsItem - Follow the mouse cursor

I'm stuck on how to approach this. I have a QGraphicsItem within a scene, and I'm passing a hover event from the scene to this child. While the move event occurs (I'm just using mouseMoveEvent with mouse tracking on), I want another QGraphicsItem to…
dajaffe
  • 855
  • 13
  • 34
4
votes
1 answer

Draw a multi-colored line in Qt

What I'm trying to achieve is the following: I have a QGraphicsScene with a QGraphicsPixmapItem shown in it. The pixmap has multiple colors and I need to draw a line across the pixmap that must be visible and recognizable in every single point. My…
Pietro Lorefice
  • 1,477
  • 15
  • 19
4
votes
1 answer

QGraphicsItem inserting order

I have project which use QGraphicsScene + QGraphicsView for QGraphicsItem organizing. All graphics items are movable and selectable. The problem is: when I insert new QGraphicsItem on the scene and this item bigger then previous item, I can't select…
Dmitry
  • 906
  • 1
  • 13
  • 32
1 2
3
50 51