I am trying to make a simple program in which I have added a qgraphics scene and in this I have added a QGraphicsRectItem. I have implemented mouse press event, paint event, bounding rect. Now I have drawn a point on one side of rectangle because there can be multiple rectangle I can drop on screen so just to differentiate between them of different color. Now I can move my rectangle inside graphics seen and can increase the size of rectangle by moving it's one side at a time. The problem that I am facing is when I trying to draw point on one side of rectangle at the time of moving it, it leaves traces on graphics scene. can I remove the ghost lines?
This happens either because your boundingRect method isn't correct, or because you forgot to call prepareGeometryChange before making changes that affect the boundingRect result. Your boundingRect needs to include space for line widths, for example; that's a common mistake.
Related
I am looking for a way to draw a node on top of the neighbouring ones in a HBox. Default behaviour means it is drawn on top of the previous one, but that also means the next one is drawn on top of it. For other containers, one could use the Node.toFront(), but changing the position of the node in the list containing a HBox's children also changes the actual position in the HBox, which is unwanted behaviour in my case. I appreciate any help, thank you.
EDIT:
The overlapping occurs when applying a DropShadow effect on an Ellipse and wrapping them in a StackPane along with a Text. It looks like the effect has a weird interaction with the HBox, as it works as intended without it. After adding the effect, it allocates more horizontal space for the ellipse, but not enough to cover the margins of the effect. Also, when clicking anywhere in the whole right half of the black rectangle, the mouse click is dispatched to the stackPane event handler, not to the rectangle's.
This happens
In VBox and HBox, the Node.toFront() and Node.toBack() functions will change the layout, so they are not usable. If you are using JavaFX 9+,you can use the viewOrder commands to change the rendering order of the Node in its Parent:
Node.getViewOrder()
Node.setViewOrder()
The default value of viewOrder is 0, so setting it to -1 will render it above all others. You can customize this to get specific orders. It also has a CSS property -fx-view-order.
I am having an issue where my QGraphicsItem is not alway performing proper hit detection with the mouse. I have subclassed QGrahicsItem and have overridden the shape() method. My shape() method calculates a polygon that surrounds the line. my boudingRect() function calculates a box that completely encloses the line. I have attached two screenshots. The first show the line highlighted in blue, indicating the hover event was fired. The second show the mouse moved just slightly to the right, but still well within the shape() and boundingRect() but the hover event did not fire. Any ideas on what I'm doing wrong?
I should also note that if I move the endpoints of the line, the hit detection usually starts working normally. It seems somewhat random as to when it breaks.
Note: The polygon surrounding the line is the shape() and the rectangle around that is the boundingRect()
Update
It seems that this must be a problem with the boundingRect(), as i zoom in to a non-working/partially-working line. The line will suddently disappear as if it's boundingRect() is no longer in the viewport, however, this is very obviously not the case as just before it disappears the bounding rect is visible.
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
I have a QGraphicsRectItem over a scene. I intend to drag and drop this window over the scene. When the rect item reaches the left boundary end I have to show it appearing from the right end. Currently I am using two objects and hiding and showing them by calculating the boundary of scene which involves lot of calculations.
Is there any better way to achieve the same effect using just a single object?
Thank You
You could use a single item that spans the entire scene, and draw the rectangle (or 2 parts of it) in it's paint method.
But then you would lose the optimization of the BSP tree, your item would redraw even if some unrelated area repaints. If this is just 1 item, I guess it would not have much impact.
You would need to implement your own dragging with mousemove event and the like, though this is not much code, you just need to get it right.
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.