How to draw rounded corners with QPainter::drawPolyLine - qt

I'm trying to create a custom container widget at the moment using QGroupBox as a base and drawing the new frame in the paint event, which is all working fine using drawPolyLine to create it, but I'd like to draw the frame with rounded corners. Has anyone come across a way to do it with drawPolyLine or would I need to rewrite my code to implement them?

When it comes to custom flexible shapes, QPainterPath is the most powerful class of them all. You could for example use QPainterPath::arcTo() in order to draw single rounded corners, though painting the full shape might require some math.
Another possibility is defining single shapes and merging them using intersected() or subtracted(), as already suggested by cbamber85 in the comments.

QPen has a "Cap Style" option of Qt::RoundCap which could result in rounded corners depending on pen width.

Related

Overlay grid onto QFrame

So, I have a QFrame with its layout set as a QGridLayout.
Within this layout I have tiles in rows of 16 which represent something of a palette.
I want this grid of tiles to be separated by lines, like a grid should be. I can do this easily with the tiles' paintEvents.
However, the obvious problem is that between the tiles, the lines are doubled up. When I scale this up for other applications, the difference becomes even more noticeable.
So, is there a way to create a gridline overlay for my QFrame? I have considered converting the whole thing to a view/scene solution, and using drawForeground, however this seems like a completely inappropriate use of the paradigm.
Thanks for any assistance!
Put the QFrame into a QGridLayout, then put a custom QWidget with transparent background and paintEvent that paints the grid on top of it (same QGridLayout position).
Or since you already have a QGridLayout, just put the custom QWidget in that, above the tiles, filling the entire grid.
A side note, are you sure you want QFrame there, or if just QWidget would do? Just saying, because with QFrame you get that 1990's look into your UI... If you do want that then go ahead, just saying.

QPainter drawing lines - configuring line softness (horizontal opacity gradient)

I am looking for a way to apply a horizontal opacity gradient when painting QLine elements using QPainter. Put simply, I want to be able to have the line opacity decrease the further away from the line center it is being painted. The effect I want to achieve corresponds to what a lot of image editing tools comonly describe as hardness of a brush.
Here is a sample image that compares a line using a hard brush to a soft one:
This would be a minimum example for painting a regular QLine:
QPainter p;
p.setPen(QPen(Qt::black, 12, Qt::SolidLine, Qt::RoundCap));
p.drawLine(QPointF(0,0), QPointF(1024,1024));
How and where would I configure the line hardness I am describing? Is there something like a fall-off property when painting QLine elements?
In the docs I could only find examples for how to apply linear gradients between set points, which is not what I am looking for.
That's not QPen painting, that's brush painting, like in say photoshop, and Qt doesn't really support such functionality out of the box.
But it is quite easy to implement, you need a brush stencil pixmap, and you simply draw that pixmap on your target paint device along a line at a given step.
The line interpolation part is already answered here.
It is recommended that the brush stencil is an 8 bit grayscale QImage, then you can easily get a colorized version of that by using the greyscale value as an alpha value for the solid color of choice. QImage is preferable, as it offers individual pixel access. This allows to have any type of brush aside from hard and soft, including certain artistic brushes.
Naturally, if all you need is a soft brush, you can generate that directly in the desired color by utilizing Qt's existing gradients and skip the stencil colorizing part. You can use QPainter to procedurally draw colorizable or full color stencils to use as a brush.

QPushbutton background image from 3 images

I have three background images for a button - left.png, center.png and right.png. The left and right ones have rounded edges and the center one is a single line which need to extend based on the size of the button. How do I create such a button? I have considered the option of constructing the image on the fly and apply it to the button in the resize event, but am looking to see if this is possible through stylesheets. Is this possible?
You can't do it with background-image alone. But it may be done with the help of border-image: http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#border-image
Use background-repeat: repeat-x to fill the button and border-image to round out the edges.
You can do it with QPainter and constructing the image, but honestly for the complexity level and for a button class you might be better off learning how to do it by constructing a paintEvent in full using the standard primitives. It'll give you better fine grain control, just involves a bit of leg work.

QT QSlider Margins

I'm making a widget that consists of a QGridLayout which maps out a QSlider and a few QPushButtons. In my widget, I'm able to position the buttons against the rightmost side of the container widget. The slider, however, is a bit more problematic.
I want the slider to be positioned such that the groove and handle extend all the way over to the point where the handle can be positioned flush against the widget boundary. I've tried a few things including changing the stylesheet margin properties for the groove and messing with the functions provided by QGridLayout, but I can't seem to figure out how to accomplish this.
Does anyone have any suggestions?
I discovered that there is no easy, reliable method of ensuring that a QSlider be dynamically sizeable within a QGridLayout.
The best way of implementing a widget like this that I've found was to place the labels using QWidget::move(x,y) and using coordinates relative to the slider's size/position.

Flex: Points to pixels and back again

So I have extended the PlotChart that comes with Flex to have the ability to draw trend-lines. To do this, I have to get pixel positions. How can I convert (100px,100px) in pixels to a point on the graph, such as (0.7,1.0)?
You can draw on charts using the CartesianChartCanvas, here is the link to the LiveDoc: Drawing on Chart Controls
To answer your question more specifically, CartesianChartCanvas supports drawing by pixels.
You probably want to create a subclass of ChartElement, and add it to annotationElements for your chart. The docs for Flex 2 show an example: Livedocs
PlotSeries::dataToLocal(...) ended up working fine.

Resources