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

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.

Related

How to make a Qt stylesheet palette() option respect widget's local palette

I am developing a project where the user is able to redesign parts of the ui and replace them (create configuration packages) but the problem is I want my app to provide color information (changing based on the state of the app) which the user can incorporate into their ui's.
What I have tried so far is using palette() option in a stylesheet and replacing some of the color roles in the widgets palette programmatically. I have found out however that palette() only respects the QApplication palette and not the widget's own palette for some reason.
For an example test case I created two buttons, one with no stylesheet and one having:
background-color: palette(button);
which should theoretically always have the same color.
However setting their parent's QWidget palette via:
QPalette p = ui->centralwidget->palette();
p.setColor(QPalette::Button, Qt::white);
ui->centralwidget->setPalette(p);
only changes the color of the button with no stylesheet while
changing the QApplication's palette via
QPalette p = ui->centralwidget->palette();
p.setColor(QPalette::Button, Qt::white);
QApplication::setPalette(p);
changes the color of both buttons
So my question is whether there in a way to make the palette() option is a stylesheet respect the widget's own palette instead of the QApplication's one or whether I should go for a different solution

How to get widget background QColor

I am trying to find out the background color of a QWidget or QGLWidget so that I can use it with qglClearColor() to make the OpenGL part appear natively within the widget (without for example a black background).
I figured I could fetch backgroundRole() of my widget but I am having problems converting it to a QColor. There is QPalette::color(QColorRole) but it isn't static and I have no idea how I would have to create an instance of QPalette to do the transformation.
Use QWidget::palette().color(QWidget::backgroundRole())to receive the color of the background color role for that widget. Obviously, this should also work with any class that inherits QWidget.

Highlighting text in JavaFx Label

I am trying to set the text background of the JavaFx label text as green using the following CSS
label.setStyle("-fx-background-color:rgba(85, 255, 68,0.7););
And the unhighlight using the following
label.setStyle("-fx-background-color:rgba(0,0,255,0);");
However these does not work most of the times when it has to be done back to back.
Is there any way to set the style without using CSS i.e. using JavaFx API itself. I found label.textFill(Paint p) for text color but nothing for background colour i.e. the color of the label itself.
Is there any way to set the style without using CSS i.e. using JavaFx API itself.
For some styles (such as the text fill) yes. For background colors, background images, borders, etc API methods will not be available until JavaFX 8 is released (see Public API for Region backgrounds and borders in the JavaFX issue tracker for more information - anybody can sign up for access).
these does not work most of the times when it has to be done back to back.
If you just highlight a label and then unhighlight it again without using something like a PauseTransition to give the user some time to see the highlighted label, then, from the user's perspective nothing is going to happen as all the user will see is an unhighlighted label.
Not sure of your use case, but if you only want to highlight part of the text in a label or let the user highlight the text with a mouse, then you can use a TextField with editable set to false.
Possible Workaround
If the Java 8 preview does not work for you and you are experiencing errors due do bugs in the JavaFX CSS processing, then try placing a Pane then a label inside a StackPane. Set the background color of the Pane to label.setStyle("-fx-background-color:rgba(85, 255, 68,0.7);); Bind the Pane's preferred width and height to the Label's width and height and toggle setVisible on the Pane as appropriate.
Finally I found the workarround. I had to give a PauseTransition to give the system some time between unhighlight and highlight. CSS showed effect only after the pausetransaction if the labels were already highlighted. I think it may be a bug. I will file a jira. The duration of paustransition may be as low as 1 milisecond so that there is not lag from the user's point of view.

Update color of an image or Background color of Image

I am making Image Editor App.I want to add QColorDialog for update Color of image.
I have set image on QLabel.
This is my code that run fine but not able to change color of image.
void ImageViewer::updateColor()
{
QColor color = QColorDialog::getColor(Qt::white,ui->imageHolder);
if(color.isValid())
{
// QPixmap pixmap = *ui->imageHolder->pixmap();
// pixmap.fill(color);** //this is also not change color of image
ui->imageHolder->setPalette(QPalette(color));
ui->imageHolder->setAutoFillBackground(true);
ui->imageHolder->update();
}
}
I have no idea how to change color of image or backbround color of that image...
is it posible?
Any idea?
Thanks...
What is the type of your imageHolder widget ?
I would recommend using Qt Style Sheet to change the background color of a widget.
As Qt documentation states :
Using a QPalette isn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and by the native theme engine.
To change the background color of your widget (there might be some restriction depending on the type of imageHolder), here is what you could do something like this :
ui->imageHolder->setStyleSheet("background-color : " + color.name());
If you are trying to change the pixel values of the image QPalette is not what you think it is for. It's for changing the colors of the UI elements.
If the background color is all you want to change, QPalette can do the job but you will need a image that contains alpha channel or transparency mask. Load a PNG with alpha into your image holder and see if that works.

QT Transparent Layout

I am using a vertical layout (QVBoxLayout) to manage buttons. I would like to make its background color as 50% black transparent. Is it possible ?
sw
Depending where you want the border of the transparent area, you will need to group the buttons in a widget (as SigTerm said) and then you can assign a color either via the palette
QPalette palette = widget->palette();
palette.setColor(QPalette::Window, QColor(100,100,100,100));
widget->setPalette(palette);
or use a stylesheet
widget->setStylesheet("QWidget{background-color: rgba(100,100,100,100);}";
the stylesheet has the advantage that you can style all of your application from one spot that is not in the code and set an application wide stylesheet via QApplication::setStylesheet(QString)
Ahem... it's been a while since I used Qt, but as far as I know, QVBoxLayout has no background color, so no, it isn't possible. Layout isn't a widget and it isn't being painted at all, it only manages child widget sizes.
If you want to create colored layout, you'll probably have to create a widget with whatever color your want, and then parent QVBoxLayout to that widget.
It'll become more fun if you want color of all layout's children to be affected by color of QVBoxLayout's parent, but I think that "Embedded Dialogs" demo from Qt4 demo may give you an idea about how it can be done.

Resources