Qt changing the background color of QWidget using palette doesn't work - c++

I want to change the background color in a custom subclass of QWidget.
Here is the code:
WorldView::WorldView(QWidget *parent) : QWidget(parent)
{
QPalette p(palette());
p.setColor(QPalette::Background, Qt::black);
setAutoFillBackground(true);
setPalette(p);
}
But it doesn't work as expected. The background color remains unchanged.
I don't know why.

As you can read in the documentation, QPalette::Background is obsolete. Use QPalette::Window instead. Note that some widgets use some other role for the background. See the QPalette::ColorRole documentation
Also:
Warning: Some styles do not use the palette for all drawing, for
instance, if they make use of native theme engines. This is the case
for both the Windows XP, Windows Vista, and the OS X styles.
In this case I suggest to use style sheets. See Qt Style Sheets Reference
However, if WorldView is a custom widget, with your custom paintEvent, it's up to you to draw the background

Use QPalette::Base instead of QPalette::Background
QPalette p(palette());
p.setColor(QPalette::Base, Qt::lightGray);
setPalette(p);
For some reason custom widget uses QPalette::Base for filling backgound

setStyleSheet('background-color:black;')

If the palette color doesnt work then it can have four reasons you should check
you have set the colors from style sheets which i don't recommend at all or maybe you have set the parents stylesheet
your color role in the palette doesnt match
make sure the palette is set for widget and i dont recommend to set palette for every widget unless you really wanted to, you should set the palette for your application
or maybe your window manager is forcing colors and palettes dont work at all
Thats all i got in my mind, if you know more, please edit this answere and add it to items

Related

How to add a myOwn-background image to QPlainTextEdit?

For example, if you create a simple plaintextedit, the background is just white. How do I change white background with my own image?
In design edit, you can set the styleSheet in properties of QWidget. In c++ code you can set the background using QSS, for example, setting
‍‍‍‍background-image: URL("path/image.png");:
myWidget->setStyleSheet("background-image: URL('path/image.png')");

How to get QWidget background-color after set a QStyleSheet

I have my desktop QApplication (Qt 5.9) on which I successfully set a style using a stylesheet. At runtime every QWidget look correctly styled as intended.
What I need to know is the background-color of a specific styled widget, let's say a QTooltip.
I tried the QWidget::palette method but what I got looked like the system's default color for the QPalette::ColorRole I asked.
Moreover, qt docs recommend not to use this method when using stylesheets.
So... more generally, how can one query for a widget property defined via stylesheets?
You can't directly access individual properties of the style sheet. What you can do, is retrieve the style sheet and then you would need to parse it and find the property you are looking for (some_widget->styleSheet() would return it as a QString).
If you don't want to do that, and want to access the palette directly, you must use that instead of style sheet to change the colors.
Example code how you can style your code using palette instead of style sheet:
QPalette px;
px.setColor(QPalette::Text, QColor(255, 255, 255)); // Set text color to white
px.setColor(QPalette::Base, QColor(0, 0, 0)); // Set background to black
some_widget->setPalette(px);

Set background color for QTabBar beyond tabs?

Simple as the question title, I have a QTabWidget and I wanted to set the background color for the QTabBar area, and this is how I do it:
ViewTabs->tabBar()->setStyleSheet("background-color: rgb(85,85,85)");
I expected this to set the back ground for the whole bar, but instead it sets the background on tabs only as in the attached picture.
How do I make the background color take effect even beyond the tabs? I'm using QT 5.7 with C++.
You can try to change the background color manually in the UI interface by clicking on "palette" in the proprieties.
It's easier to handle colors this way.

QDockWidget::background-color not applied when docked

I have a QDockWidget:
I would like to alert the user to certain events by setting the background color of the title bar.
I have achieved this by setting the style sheet for my DockWidget:
void DockWidget::setCriticalAlert()
{
setStyleSheet("QDockWidget { background-color:red; }");
}
The result is this:
The problem is that the background-color doesn't get applied when the QDockWidget is docked:
How can I get the background color to be applied when the QDockWidget is docked?
This is a bug in Qt.
Issue 10537
Quoting from the linked issue:
The problem is that in QDockWidget::paintEvent, there is a
isFloating() condition before drawing PE_FrameDockWidget. We cannot
jsut remove this condition as it would break the other style (that
does not whish to draw frame when the dockwidget is docked) We cannot
either use PE_Widget to draw the frame as then it goes over the
dockwidget's title The solution is maybe to introduce a new
PE_FrameDockWidgetDocked primitive element. Or some
SH_DockWidget_DrawDockedFrame stylehint to draw the frame in every
cases.
a valid workaround seems to be to set the stylesheet of the parent, and use the class-and-id selector. Forgive the python formatted code but the concept is the same - in this case, 'dock' is a QDockWidget which has been given an object name using setObjectName(), and its parent, the QMainWindow, is 'self':
self.setStyleSheet("QDockWidget#"+str(dock.objectName())+"::title {background-color:red}")
In PyQt5.5, this works at runtime, i.e., can be changed on the fly.
I find a solution like this:
Firstly put a frame behind all the widgets of dockwidget's center widget, as the background.
Then set stylesheet for the frame.
By this way, we could change the background color of dockwidget.
Or you can extend the dockwidget and overwrite the function
void QDockWidget::setWidget(QWidget *widget)
using private/qdockwidget_h. and add a frame as this widget's father.

QWidget background color does not fully cover the QWidget

Using Qt Creator, I have set a QWidget's background property to black. However, some parts of the QWidget, more specifically, between QFrames/QGroupBoxes are still in its system's default color.
Now, I thought that the QFrames and QGroupBoxes need to have its background property set to black too, but it did not work. I have also tried setting the border-color to black, but it does not work, since by default borders, margins are set to 0.
QWidget { background: black; }
Any advice on this issue?
EDIT
The QWidgets are placed in QMdiArea. However, if I make it a QWindow, it works. However, I want the QWidgets to be in the QMdiArea. Also, if I just show the QWidgets as it is, the spaces that I have mentioned above are transparent.
It sounds like you have some widgets within another widget, and are setting the contained widgets to be black, but then the space between them is not black. If that is the case, it is likely because you have a layout in the containing widget, which allocates space between each contained widget. The empty space between widgets will be drawn with the containing widget's background color.
Found out the solution. It seems that you need to set the background color at the QMdiSubWindow, not at QWidget. Don't know why, but it seems logical.