Qt: How to create/render Arrow Graphics Item on QGraphicsscene - qt

How do we create a Arrow graphics item which gets displayed on a graphics scene?
My requirement is that I drag and drop a QGraphicsLineItem from one scene to another. Once the Line item is dropped on a scene, a Arrow graphics Item should display perpendicular to the line item dropped on the scene. I should be able to display the Arrow Item on either side of the line. Currently I am able to drop a Line item onto the scene. I need source code for creation of an Arrow item.
Can somebody please help me with this scenario?

There are two possible options here. One is to have an image that you load into a QGraphicsPixmapItem and position and rotate it as desired.
The better method would be to create a class inherited from QGraphicsItem, and draw the arrow in its paint method, with the calls to drawLine.
When you inherit from QGraphicsItem, make sure that you overload the boundingRect() function, as well as the paint() function.

Related

JavaFX - how to override component's drawing behaviour?

I have a TableView and one column contains buttons. When I press the button a line is created and I drag the mouse to draw the line to another button.
The line starts and ends at the center of each button, however, I need the line to display BEHIND the buttons, yet over the table.
In Swing I would simply override the paint method of any component and write any custom graphics drawing before actually painting the button.
How can I achieve the same thing using JavaFX?
Try to make use of Node.toFront() and Node.toBack().

JavaFX: Rectangle with custom bounding rect and paint method

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

QTextEdit: How to add fixed position text on top of an image

I am using QTextEdit to implement an editor. One of the requirements I have is to add fixed position text on top of an image.
For example:
I have an image of dimensions: 300x300. I need to add text beginning at the location (20, 20) of the image and ensure that the text does not flow beyond the width of the image.
Something like below:
I am thinking that if I can add a QGraphicView, I can add the image and position text appropriately. Is this possible? Is there a way to introduce a graphic element into a QTextedit? If not, what is the right approach?
Is there a way to introduce a graphic element into a QTextEdit? If not, what is the right approach?
You could look at this the other way and add the QTextEdit to a QGraphicsScene. The graphics scene provides a QGraphicsProxyWidget to add standard Qt widgets.
Therefore, you could create a QGraphicsScene and QGraphicsView. Add a QGraphicsPixmapItem for the image and add the QTextEdit item with a call to QGraphicsScene::addWidget, which returns a QGraphicsProxyWidget, allowing you to position, scale and resize the widget.
Alternatively, you could start with a QGraphicsItem, inherit from that and create your own object which encapsulates the image and proxy object of the QTextEdit.
There are other ways of tackling this too, but I'd probably go for the custom QGraphicsItem. It also depends on your specification, but you can add text items in a graphics scene, without the QTextEdit, though you'd probably have to implement the editing feature, if this is required.

QGraphicsView not drawing line items when zoomed in or moved?

I'm developing a small Qt application similar to the DiagramScene example. I have subclassed QGraphicsView instead of QGraphicsScene. My view is zooming in and out with the mouseWheel, I can drag it with the mouse and I can add Nodes and Links with clicking.
I click on one Node, (the first end of the line item is set), then move the mouse (the second end of the line is following the mouse cursor), then I click on the second Node and anchor the second end of the line item to this second node.
The problem is, when the view is zoomed in, or I have moved the view, when I click on a Node and move the mouse, the preview of the link is not visible. When I click on the second Node - the link is still not visible. The link between the two Nodes becomes visible only after I zoom out or drag the view to some point and it intersects with the sides of the view.
Any ideas how to fix this?
Thank you so much in advance.
I finally fixed it. I was wrong to use data members for the coordinates (also the bounding rect and paint method) of my custom Graphics items. I changed the code using the setPos() function which gives the right coordinates to my items. #Merlin069 thank you, actually your last question got me thinking whether I set the coordinates correctly.

QGraphics Scene Zooming in and out

Here I am again worrying about how to Zoom in and out a QGraphicsPixmapItem in a graphics scene. I looked for a direct method for this but could not find any in graphics scene or in pix map. Can someone help me with this. Do I have to extend QGraphicsPixmapItem and implement methods for this.
Thanks again for help and I really do appreciate it.
~Tharanga
setScale() changes the size of the item, not the view scale of the scene. For a one item scene, it's effectively the same. But if you have more than one item in the scene, it changes the relation between items.
QGraphicsView::setTransform() should be used if you want to keep the item's relationship to the scene and other items.
QGraphicsPixmapItem inherits from QGraphicsItem, so it has all of that class's methods.
In particular, there's setScale that will change the item's scale factor (i.e. "zoom" it).
Look at the Transformations Example page for how this is done, and other transformations you can do.

Resources