Qt Widget with SVG image and ui elements - qt

In a C++ Qt6 desktop application I would like to display a schematic drawing of an industrial process (which I have in SVG format), overlayed with some ui widgets, like radio buttons and combo-boxes, that represent configurable parameters.
It looks like this can be achieved with QSvgRenderer, but there is a problem:
the rendered svg will not be theme-aware.
Is there any way to modify the colors used by this renderer, without parsing and manipulating the SVG myself to edit the color values?
We can assume that all SVG elements are the same color.
Alternatively, can I do something else with my SVG schematic, that does not involve manually drawing it again e.g. in QPainter, but still accomplishes my original goal?

Related

How can I set vector mask for clicking in Qt?

I need create clickable component with custom shape. Appearance is set by svg file. Clickable area must be constrained by svg shape. I find great example of what I need, but it use pixel mask or circle mask. Can you help me find solution?
Most probably you will need to create a pixel mask yourself from the SVG shape.
The question is how to approach this. Qt does not offer a simple way of doing it. However, in Qt you can render the SVG offscreen into an image that you initialize with transparent pixels or a color key. You can then use this image as a mask.
If the size of your viewpoint changes frequently, you might want to do the mask rendering in a higher resolution first and then scale it down accordingly for performance. Also note that if your SVG is animated, you would have to accomodate for that.
Or you might use a different library than Qt to obtain the mask. Also, if your SVG contains only a single polygon, you might go for a point-polygon test. But I doubt it, and such a test is also not trivial when the polygon is non-convex (you typically end up with a scanline algorithm anyways).

QML Canvas: problem with drawing of SVG image with lines containing arrows

In QML canvas, I am trying to draw an image which is in svg format and contains lines with arrow heads like below.
svg in not supported by stackoverflow, so I put the png of svg image above just for diplay.
original svg is here : https://svgshare.com/i/9u8.svg
And when I tried to draw this image in QML canvas, resulted image doesn't contain the arrow heads. The result look similar to below. Even in Qt creator, arrow heads are not shown.
I want to know is it problem with SVG generation or problem with drawing on canvas?
The reason this happens is that Qts SVG implementation is based on TinySVG - it only supports a subset of SVG and can't handle all features of the specification. Looking at the code of the SVG-file, it is propably the <marker> elements that are not supported.
A possible solution would be to either edit the image (with e.g. Inkscape) to convert these markers to normal paths or to create a bunch of PNGs of variing sizes and load them conditionally.

Custom shaped menu with shadow in Qt

I'd like to create a context menu looking similar to this one:
I read suggestions on the web that QWidget::setMask() should be used to create a shape. But how can it fit the variable number of items then? Moreover, the same menu item may take more or less screen space on different machines.
Another question is how to create a shadow around this custom shape? As far as I understand, the mask allows to crop the widget, but not to make it semi-transparent.
I don’t found an easy way to do that! But here goes a way!
Instead of using the Qt mask API, I've used a frame-less widget with transparency enabled!
To draw the shadow, I've used radial gradient!
You can change the size of the menu before opening it, however you can’t resize it after opened (for example resize with mouse).
It’s quite easy add or remove widgets, just respect the layout margin to not draw outside the bounds destined to widgets. To simplify your life I created an inherited class of QPushButton with colors you can easily customize with style sheet.
See the result:
You can browse the source
Hope that helps!

Is there a way to use CSS to specify colors on an HTML5 canvas?

I'm collaborating on a checkers program for which the first logical step is to draw the initial board. The implementation we are using has one philosophical issue. The squared are formed by calling fillRect with the color of the"dark" squares after initializing the board with the color of the light squares.
My concern is that I now have colors hard coded into the JS, and I would prefer to specify them in the associated CSS. Is there a good way to encode the colors used in drawing the canvas in CSS?
Pick one or the other, and stick to it. Obviously the game logic would still use JS.
If you want to make it easy to style each part of the board, use CSS and render the parts of the board using actual HTML elements with classes on them. This way you can easily make changes to sizes, colours, images, textures, etc., without explicitly re-rendering anything.
If you want/need it to be on a canvas, you can easily expose a method for setting styling options during or after game creation. This way you can include an inline or second script file as you would with CSS.

How to set QPalette ColorRole properties with stylesheets

I am using a pretty lightweight custom palette that just defines a few colors and properties. I am attempting to transform it into a stylesheet but I am running into problems. For example, what is the QPalette::Base equivalent in stylesheets? Do I have to change the background color for every widget individually?
I tried setting a few stylesheet properties for the QWidget but then I got undesired results for widgets with borders. Layouts were showing borders when I didn't want them to, so I found myself writing a lot of stylesheet definitions to add/remove borders.
The gist of the question is, is there a good way to convert a palette to a stylesheet?

Resources