I am selecting items using the default QRubberBand enabled by setting QGraphicsView.setDragMode() to rubber band selection. This works fine, however when an item is selected it appears with a grey dotted line around it's bounding rect and I was wondering if there is any way to prevent this?
It appears the only way to do this is to override QGraphicsItem::paint().
The default implementation of paint() for standard items will change the pen if the item is selected or not. But since standard items likely consist of 1 line paint() calls changing this is trivial.
For example in a QGraphicsLineItem it would just be painter.drawLine( line() );
Related
I have a QGraphicsScene with many QGraphicsItem like circle, rectangle, polylines, arc etc.
Every QGraphicsItem has a name. And I have a Search feature which highlights searched items using that name. When scene contains fewer items, highlighted items are seen properly. But when scene contains many items ( 100 + ) then identifying the highlighted item becomes difficult. For that I need to scroll up - down or left - right to check where is that highlighted item is.
So I want my highlighted item to be easily recognizable.
For that I was suggested, some approach like
Take searched item in front
Or
Add some marker
But I do not know, how to implement it , which Qt class should I use etc.
QGraphicsScene displays items depending on their stacking, i.e., when you call .items(), the first item in the QList is the topmost item on the scene.
The ordering of the items is depending on their respective Z-values which you can set for the QGraphicsItem by calling .setZValue()
You can read more about this on the following link: https://doc.qt.io/qt-5/qgraphicsitem.html#sorting
As for your question on how to put them in front. If you implemented your own children classes of QGraphicsItem, you can add a method or a slot to toggle them "on-top". E.g., all of the items usually have a Z-order of 0, so your method could just set it to 1 when the item is highlighted and revert it to 0 when it's not.
In graphics view m setting scene in that adding objects (by dropping ), i can move these items by mouse, when i moved one object on another object moved object should be transparent. how can i make it?
I don't believe you actually want full transparency since it will make it impossible to visually recognize the transparent object later on. Reduced opacity - yes.
As for your question: each item inside your scene has a bounding rectangle (or other type of bounding area). You can easily get it by calling boundingRect() of your item. The returned QRectF has (just like QRect) has the bool QRect::intersects(const QRect &rectangle) const function, which takes another rectangle and checks if a collision is present.
Whenever you move your mouse while dragging an item you need to iterate either through all or just a subset of all items in your scene (by subset I mean just the items in a specific region to increase the performance) and check for collision. If a collision is detected, you can alter either the item you are dragging or the item underneath it.
Of course to make sure that one item covers another one you also need to check the Z value. The easiest way to do that is if you keep all currently not being dragged items at the same Z level and then, whenever you drag one, increase it's Z level by one so that it is "above" the others.
I have a QCombobox, consists of 3 or 4 items.
Whenever,I try to select an item from my QCombobox, the color of the item changes to black(is not at all visible clearly).When I move my cursor away from the Dialog UI, then the color of the item changes to white (and is now clearly visible).
I think the problem lies with my QCombobox focus, but still not sure if I need to take care of it while adding the items inside my QCombobox.
Can someone explain the method, so that text of the items inside my QCombobox are clearly visible always?
Look for any stylesheets added for the combobox.
Also you can look for "setItemdata" function and anything is done with respect colors (backgroundrole).
Or if it is tough to figure out and wasting time, after adding your combobox items, change the background color of items to your convenience (Red in below code is just an example).
ui->comboBox->addItems({"text1","text2","text3","text4"});
ui->comboBox->setStyleSheet("QAbstractItemView { selection-background-color: red; }");
I have custom graphic item that draws some figure consisting of lines and polygons.
It has reimplemented method
hoverMoveEvent( QGraphicsSceneHoverEvent* event )
that indicates when need to highlight the figure (when mouse crosses the internal line or polygon ).
There are situations when items are drawn one above other, but hover event is accepted only by top item.
I tried to ignore event inside the method but it don't help.
To solve this problem need to reimplement method
QPainterPath QGraphicsItem::shape() const
in the custom item.
This shape should be exactly as a shape of "internal" painted items. Without empty spaces inside the bounding rectangle.
What I want to do:
In PySide, one could override the paintEvent() method of a QWidget to draw a custom widget. The bounding rect can be customized by overriding the boundingRect() method.
How does one do this in JavaFX? My goal is to create a custom Rectangle object, that draws itself smaller than it's bounding rect.
In context:
I'm creating an MS Paint clone in JavaFX. I'm working on the selection box that you use to select/move pixels around. I want the cursor to change to the appropriate resizing cursor when it is near the outside of the selection box.
However, the bounding rect is the same size as the selection box drawn on the screen, so the cursor only changes when it is on top of the box, but not when near. My solution is to set the bounding rect to larger than the actual selection box is, so the cursor change will occur. Then, override it's paintEvent() equivalent to draw a smaller selection box.
Thanks for your help.
I have a similar use case to you, and asked a similiar question here : Drawing transform independent layout bounds in JavaFX.
The JavaFX API is much higher level though than Java2D or PySide (I am assuming from your snippets, because I actually never heard of it ;) ), it does not allow you to override painting of Nodes, nor can you stop a Node from inheriting its parents transform.
This means that you need a seperate Group parallel to your content where you create the selection box and update it according to your needs (content changes etc.).
Example SceneGraph:
Scene
contentGroup
someShapeFromUser
selectionBoxGroup
selectionBoxOfSomeShapeFromUser