Select multiple QGraphicsItems without holding down key - c++

I'm using a QGraphincsView that holds several elements which inherit from QGraphicsItem. The whole thing works fine, I can select them as desired. And when I hold down the Ctrl-key I can select several of them.
Now I want to implement an optional multi-selection without the need to hold down Ctrl-key. I already tried to set the related modifier in mouse-press-event by calling
evt->setModifiers(Qt::ControlModifier);
before the event is handed over to it's base-class QGraphicsItem but this does not work.
So my question: what has to be done to get multiple selection functionality by default and without holding down a key?
Thanks!

This is controlled by the QGraphicsScene. You stated: -
I'm using a QGraphincsView that holds several elements which inherit from QGraphicsItem
This is not actually the case. A QGraphicsView is a window into an area of the scene; it is a QGraphicsScene which holds items derived from QGraphicsItem.
You can see in the documentation that the QGraphicsScene has functions such as selectedItems(), selectionArea() and setSelectionArea(). While a QGraphicsItem can be selected with QGraphicsItem::setSelected, the control of what happens when you click an item is governed by the QGraphicsScene, with the event having been passed from the QGraphicsView.
If you inherit from QGraphicsScene, you can override the mouse methods; mousePressEvent, mouseMoveEvent, mouseReleaseEvent. This will allow you to monitor when the user selects consecutive items by clicking on them and react by calling their QGraphicsItem::setSelected function.
Alternatively, depending upon your design, you can allow the user to draw an area on the scene and call QGraphicsScene::setSelectionArea, which will set all the items surrounded by the given QPainterPath.

Related

Qt QGraphicsScene dynamic GUI elements like containers

I'm using QGraphicsScene and I want to create gui elements.
How do I/should I create a list container i.e. inventory in a game context.
See HERE
For an example of what I mean. There is an inventory widget at the bottom that can be dynamically populated.
Are there any container classes like the standard list widgets but for QGraphicsScene instead?
Right now the only way I can see is to draw a 'rectangle' and manually draw and manage 'squares' on top of it... which wouldn't be a container.
Every QGraphicItem is effectively a container because every item can be optionally parented to a QGraphicsItem. When you do that, the parent affects all its children...if the parent is moved, the children move with it. If a transform, visibility, or opacity are set on the parent, those changes apply to the children as well.
Basically, your understanding is correct; you would create a parent QGraphicsItem that draws a container outline, and then you would have children that each draw their icons or whatever is appropriate.
Alternately, you can use widgets in a QGraphicsScene, so all of the widget-based containers are available. If that's the route you want to go, then look at the QGraphicsWidget class.
This is a complex subject, but hopefully this will get you started.

What is the proper way to make position restriction for several QGraphicsItem elements on scene?

I have custom scene and it is implemented as a class derived from QGraphicsScene.
Also there are some QGraphicsItem-based items on it. I want to make the restriction to the scene coordinates in what these items can be moved.
So I implement itemChange() mеthod in these classes derived from QGraphicsItem class. As a result when I select multiple items on scene and move them to the defined scene bounds every item stop moving near this bound and all moved items constrict in one point.
I want to move all selected items as one item so when one item is near the scene bounds moving of other items stops.
So I decided to use QGraphicsItemGroup approach. In my custom scene class I create group of all selected item in mouseMoveEvent, make group rect position traсking and in the mouseReleaseEvent I destroy the group.
But after that all my items become not movable.
How can I handle this?

qt create simple rectangular board

I want to create a rectangular board using QT. This board will be updated when a step is performed. For example on step x, the text in cell 5,6 updates from "not explored" to "explored".
I have looked through the QT documentation and found the class QGraphicsView. How can i use QGraphicsItem to simulate a cell where text can be written?
I am also open for alternatives.
Technically this could also be done by customizing a QTableView/Widget, but QGraphicsScene is more robust for custom graphics and performance.
From a high level view, you can either create a composite object representing a "Cell" item, or you can subclass a QGraphicsItem and do all the custom painting yourself.
When creating a composite object, that would just be a QGraphicsItem "Cell" subclass which contains maybe a QGraphicsRectItem and a QGraphicsTextItem as members, set to the cell instance as a parent. This will keep the child items translating with the parent cell item.
When creating a completely custom QGraphicsItem, you would define all the painting inside of the paint() method, which would draw a rectangle, and text taken from a value set on the instance.

A custom widget to display and select among multiple graphic views

I have a collection of images, dynamically generated through QGraphicsView widgets, and i'd like my users to choose between them. For that purpose, i would display inside a custom widget available images in some kind of grid and have users click the one they are interested in.
Multiple questions arise :
is there an existing widget that already fits this purpose ?
should i find a way to disable all mouse event handling by QGraphicsView items, or could i add a transparent widget in front of graphic views which would intercept them ?
is there a performance issue displaying many QGraphicsView widgets (up to a few hundreds) ? Should i export them to plain images first ?
First off, no, there's no widget designed specifically for that purpose.
I don't think you are grasping what QGraphicsView is for. It's for displaying a QGraphicsScene, which is meant to hold many QGraphicsItems. Based on your post, I can't see why you would need multiple QGraphicsViews. You can simply have one QGraphicsView and display many images inside of its scene. For example, see QGraphicsPixmapItem.
You definitely should not have hundreds of QGraphicsViews. You probably just want one (although a few could be justified in certain circumstances), in which you display many QGraphicsItems in a QGraphicsScene. You can definitely have hundreds of QGraphicsItems visible at once. In your case, you probably want QGraphicsPixmapItems, which are a subclass of QGraphicsItem. You could even have multiple QGraphicsScenes, and display whichever one is relevant using QGraphicsView::setScene. If you want the user to be able to select an image from a grid, and then work with that image, I would look to the State Pattern.
I can't think of any reason to disable mouse handling in QGraphicsViews, QGraphicsScenes, or QGraphicsItems. Why should these not handle their own mouse events? You can (and should, where necessary) subclass them and reimplement mousePressEvent, mouseMoveEvent, mouseRelease event, etc. to obtain the functionality you want.
Good luck!

How to get mouse pressed events in the root widget of a hierarchy in Qt

I have the following problem with Qt (no answer on this site seemed to address exactly this problem so I create my own question).
I have an application with a MainWindow class which inherits from QWidget. At a certain point there is a table inside the main window and I want to catch all mouse pressed events outside that table.
My first solution was to reimplement the method
/* virtual */ void MainWindow::mousePressEvent(QMouseEvent *event)
In this method, I check the position of the event and check that it is not within the QRect of the table. Unfortunately, I realized that mousePressEvent() is not always called. I suspect that if I click on another child widget of MainWindow, that widget consumes the event and does not pass it through to the parent.
So the only alternative idea I had was to reimplement the mousePressEvent() method for all the widgets contained in MainWindow. This is of course not feasible, because:
There a lot of them: it would be very complex, time-consuming, error-prone, and difficult to maintain if one had to change all the widget classes that are instantiated inside MainWindow.
Some of the subwidgets are implemented in some library modules developed in a parallel project, so I cannot change those.
In other cases, the subwidgets use Qt classes directly.
Even if I defined custom subclasses for 2 and 3, I would have to make sure that these subclasses are used everywhere instead of the original classes. This might imply again falling back to case 2.
So this alternative solution seems unfeasible to me.
Summarizing: Do you know if there is a simple method to catch all mouse clicks on the main window from within the MainWindow class?
You could do this by installing an event filter in the main window. Take a look at QObject::installEventFilter() in the Qt docs.
You can set the attribute Qt::WA_TransparentForMouseEvents with QWidget::setAttribute to all the child widgets except the table to get the mouse events in the MainWindow (which will only work if the table is a direct child of MainWindow).
Or do the opposite, and add a transparent widget above your whole MainWindow with a hole at the position of the table. And you set/unset the Qt::WA_TransparentForMouseEvents to that widget when you want it to let the clicks pass or to catch them.
The hole can be created with QWidget::setMask() and QRegion::substracted().