How do I get the opaque version of a colour in QML? - qt

I'm building a custom control that fakes partially-rounded rectangles by drawing several rectangles on top of each other. I want to use a colour in this control that is sourced dynamically from elsewhere -- and sometimes that colour has an alpha component (semi-transparent). Of course, that doesn't work at all for me, since the multiple rectangles situation means that the alpha will show the rectangles layering.
Is there a function I can use to set the alpha part of a colour to full opacity and leave the rest of the colour alone?

You can access and set individual color components, like this:
someColor.a = 1 // set alpha to 1 for opaque
If you are asking about getting a pixel's value after the renderer has blended everything, there is no efficient way to do it.
If you know all colors that are blended, you can mix them manually.

You can define a function to do that in javascript (or same in C++):
function makeTransparent(color, alphaPercent) {
return color.a *= alphaPercent;
}
and use it as:
Rectangle {
property color myColor: "#33000000" //<-- e.g. semi-transparent black
color: makeTransparent(mycolor, 1.2);
}
Note: my function makes color transparent in percent as Qt.darker() and Qt.lighter()

Related

Qt3D transparency in offscreen renderer

I'm using Qt3D with a combination of this offscreen renderer and modified the framegraph to include a background image, like here.
Unfortunately, adding transparency to the objects drawn over the background image using QPhongAlphaMaterial only works unsatisfactorily.
This is th result:
What you can't see here is that the whole circle part is actually transparent, i.e. the renderer wrote the transparency value of the object for the whole pixel instead of adding it transparently on top of the background.
This is what the rendered object looks like wihtout transparency:
And this is the background:
The framegraph has two branches: one for the backgroun image, which is processed first, and one for the objects. I added a QRenderStateSet for the objects that contains a QBlendEquation with the blend function set to add and a QBlendEquationArguments with source RGB and alpha set to 1, and destination RGB and alpha set to 1 minus source alpha.
Any ideas how to fix this problem?
(For anyone wondering, I took the images from the T-Less dataset and wrote a program to create ground-truth data for 6D pose estimation)
Similarly to this question, the format of the texture that is being rendered to needs to be set to RGB8_UNorm and not RGBA8_UNorm, i.e. without the alpha channel.

Setting background color of system background for widgets, on a QGraphicsScene

In my QGraphicsScene, I would like to set the background brush to the default widget background - but I can't get it.
Kinda like, for my QGraphicsView,
setBackgroundRole(QPalette::Window);
or
setBackgroundBrush(palette().background().color());
(but setting this I see nothing happening)... I also see nothing happening if I set the view color to a bright red).
So I thought I must set color directly on the QGraphicsScene.
For the QGraphicsScene I am trying all sort of combinations like
setBackgroundBrush(QPalette::color(QPalette::Background));
Nothing will even build, seems I require an object (? a widget ?) - but my scene may not have a widget parent... and all I want is a default palette, I thought there would be a generic way to get that color without having a widget ?
On the scene, this will work...
setBackgroundBrush(Qt::red);
No clue why the view won't show color (even if I set on the view, red brush and on the scene transparent).
You may retrieve the QApplication's current style using the static method style(). From there, you may access the QStyle's standard palette using standardPalette(). Use QPalette's brush method to get a brush for a given ColorRole. Putting it all together you get...
QApplication::style()->standardPalette().brush(QPalette::Background)
This may not be the color you are expecting. Check out the documentation on http://doc.qt.io/qt-4.8/qpalette.html, and try different ColorRole values until you find what you're looking for.
Create a temporary widget instance just to access its palette and get the background color:
QColor bgColor = QWidget().palette().background().color();
But I think you should set the background color in the QGraphicsView widget. You can do that by changing its stylesheet. Something like:
QColor bg = ui->graphicsView->palette().background().color();
ui->graphicsView->setStyleSheet(QString("background-color:") + bg.name(QColor::HexArgb));
Setting a transparent background also works.

How can I achieve a "Karaoke Lyric" like effect with qt (QLabel)

So this is the effect what I want to get. Left and right parts of a label should have different filling/stroke colors (actually stroke size is also changed). And the proportion may change dynamically, and can be some position inside a letter.
I know I should override the paintEvent but still have no idea of how to do it.
Any ideas?

How can I set the backgroundColor of a WKInterfaceController programmatically?

I couldn't find any way of changing the overall background color of the Watch app interface programmatically.
I therefore tried creating a fullscreen WKInterfaceGroup and changing the backgroundColor of that.
centralGroup.setBackgroundColor(UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
However, I still have unfilled rounded corners and lines on borders.
Is there any way to achieve my goal, of an edge-to-edge background color that fills the screen?
While you can set a background color for a WKInterfaceController in the storyboard, there is no API that lets you programmatically set it in the present Xcode 6.2 beta 3.
However, you can create a full screen WKInterfaceGroup and set the color of that programmatically. If you also setCornerRadius: to 0 you get the following result:
Clearly, there are still black lines at the edges here. The other possibility was that setting a solid color image as the background might perhaps scale to fill the full screen with one of the scaling options, but I tested this and the result is identical to the above.
However, it is important to consider the hardware this will be running on. Apple have said more than once in their public statements about the Watch that the intersection between the laminated screen and the edges is so seamless that you essentially can't tell where the screen ends and the edges begin. Taking this at face value, as long as you set the background color of the WKInterfaceController to clear or black in interface builder, so that these edges are black to match the screen surround, then these black lines should be invisible to the end user and are something you can safely ignore.
This is how I done it:
Add WKInterfaceGroup and setCornerRadius to 0
Go to WKInterfaceController Attribute Inspector and change Left/Right insets to 0 (Fig.1) (This will remove the extra black line on the left & right side)
Change the color of the WKInterfaceGroup to whatever you want :)
Fig.1
Change insets of Your WKInterfaceController to custom and set all to 0. Also, change corner radius of group to 0 too. Enjoy.
Try changing the corner radius of the WKInterfaceGroup to 0. You can use setCornerRadius: to change the corner radius.

mschart: Different background colors for different y-value ranges?

Is there any way I can create a line chart using mschart where the chart area's background color changes for different ranges of y values?
For instance, a line chart would have a green background for y values 0 - 10, a yellow background for y values 10 - 20 and a red background for y values 20 - 30.
A gradient will not work, the colors must be solid.
I might have to use a background image, but I think there might be a better way. Unfortunately, web searches haven't turned anything up.
I've decided the only way to do this is to use a background image. Where needed I can use GDI+ to create the image at runtime with the proper height and width.
you can also use custom labels feature of MSchart.
int element = Chart1.ChartAreas["Default"].AxisY.CustomLabels.Add(0, 10,"Low");
element = Chart1.ChartAreas["Default"].AxisY.CustomLabels.Add(10, 20, "Medium");
element = Chart1.ChartAreas["Default"].AxisY.CustomLabels.Add(20,30,"High");

Resources