Painting on the margins of QScrollArea - qt

I want to draw something like x\y axes scales like rules in Photoshop but with QScrollArea
There is good property for that:
QAbstractScrollArea::setViewportMargins(20,20,0,0)
and it works fine - there white space at the top and left sides and putting point to (0,0) draw it with specified offset.
But I cant draw at this place.
I tried
painter.drawLine(-10,0,-10,height())
but it do nothing. So how can I draw something on this margin space?

You're painting on the viewport, and this won't work. You need to put a widget in the margin area and paint within that widget's paintEvent.

Related

Drawing node on top of the others in HBox

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.

Qtransform scale and width of pen set in Qpaint of qgraphicsitem

Hello friends I am resizing and rotating QGraphicsItem using setTransform-method of QGraphicsItem, I am drawing QGraphicsItem of any shape in its paint-method, when I scale item using setTransform method border of QGraphicsItem also gets thicker and I am setting name to QGraphicsItem, it also gets bigger as per scaling factor. So is there any method by which I could draw the border of item and name given using drawtext in paint method remains of same pen width that I am specifying.
You need to make a pen cosmetic with pen.setCosmetic(true) and use this pen to draw border. Cosmetic pen's width doesn't depend on applied transformations.
I recommend using QGraphicsSimpleTextItem or QGraphicsTextItem to draw text in the scene. You can disable text transformation by calling textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);. It may be convinient to make main graphics item the parent of the text item, so that text item is positioned relative to the main item.
Note that reimplementing QGraphicsItem::paint is not necessary in most cases because built-in QGraphicsItem subclasses satisfy variety of needs.

Shifting a QGraphicsItem's "graphic"

I have a QGraphicsItem with a custom graphic as shown below in the top half of the figure, where the red circle is the shape() of the item:
Is there a way to preserve the graphic but just shift it like shown in the bottom half of the figure?
I don't think, you can move QGraphicsItem without shape. Shape is bounded to QGraphicsItem.
It changes its co-ordinates according to QGraphicsItem.
If you want to achieve something like your second half of the fig. You need to have two QGraphicsItems (one is your red outer circle and second your inner graphics item).

Qt - What is meant by this sentence

At the following web page: http://web.mit.edu/qt-dynamic/www/tutorials-tutorial-t3.html
The following sentence is mentioned:
A widget is clipped by its parent and by the widgets in front of it.
What do we mean by such sentence?
Thanks.
Qt has the concept of parent/child widgets. The parent widget is the container of the child widget, all the way up to the main window widget. So this is just saying that a widget will be clipped by its parent (container) widget. This means it won't extend past its parent's boundaries, but will be cut off if it goes beyond. Likewise, a widget is clipped by any widgets in front of it.
First off: the second part of the sentence is no longer true for Qt >= 4.1 where a parent may paint behind its children.
In graphics, clipping describes the limiting of the painting to a given area. E.g. if you drew a line from (0,0) to (100,100) with a clipping rectangle of (50x50)#(0,0), you'd effectively draw a line only from (0,0) to (50,50), ie. all pixels that would have been painted, but lay outside the clipping region, were discarded.
In Qt, painting can optionally be clipped using QPainter methods, but the painting is always implicitly clipped by the QPaintDevice you're operating on. QWidget is a QPaintDevice, and as such, painting outside of its QWidget::rect() will have no effect (= it will be clipped to rect()).
Now, about the second part of the sentence: In older Qt versions, the child widgets would first fill their area with the background color/image, thus effectively clipping the parent's painting to outside the union of all children's geometries. Since Qt 4.1, this behaviour can be disabled by setting the QWidget::autoFillBackground property to false. Indeed, this happens to be the new default, too.
In the autoFillBackground == false case, the child widgets do no longer erase the parent's drawing acting as their background, except where they actually paint. Take a QLabel as an example: with autoFillBackground == false, it merely paints its text, leaving the parent's drawing to shine through as the label's background.

Flex - Placement of Legend for a chart

I would like to be able to specify the placement of a legend for a linechart. Currently, it continues to appear to the right of the chart. I have tried playing with the width/height of the chart to no avail... Putting the legend before the linechart in the mxml causes it to appear to the left. I can't seem to get it appear at the bottom though. I can't seem to find any good examples for this. They don't seem to specify anything but the legend usually shows up below the chart, I can't seem to do it. Optionally, it would be okay to somehow minimize the legend..
what container are your line chart and legend in? its sounds like you are using either an HBox or an application with the layout="horizontal". To move the legend below use either a VBox or application layout of vertical. Or you can use a canvas and use constraints (left, right, top, bottom) or x and y coordinates

Resources