QToolBar background color only applies to actions
Asked Answered
P

4

10

I am trying to set the bacground color of a QToolBar in Qt designer with the following in stylesheet background : rgb(30, 30, 30). For some reason the background color is only applied to the action's background as can be seen in the image. How can I change the color of the whole toolbar?

enter image description here

Edit.

Even if I change the background color of my whole window, the area of toolbar is not affected:

enter image description here

This is an empty default Qt widgets application where I only added a QToolBar and one QAction and in the stylesheet of my QMainWindow

background : red;
QToolBar { background : red }
QToolButton {background : red}

Is this expected behaviour or a bug on qt with linux?

edit.

I tried this code on Xubuntu 14.04 with Qt 4.8 and Qt 5.4.2. This seems to be a bug on Qt. See my own answer below.

Poly answered 27/7, 2015 at 9:17 Comment(4)
Give us more context of your code please.Flatwise
Well, there isn't really any code besides the 'backgound : rgb(30, 30, 30)' added to the styleSheet property. I am using qt designer. I added a QToolBar to the QMainWindow, some QActions to the toolbar and in the property 'styleSheet' I added background : rgb(30, 30, 30).Poly
More context needed: OS, Qt Version you may have found a bug as this should work as you tried, the more information you can provide the better.Hoarhound
Added more informationPoly
P
4

Ok, so I did some digging and found this https://forum.qt.io/topic/23800/solved-change-background-color-of-qtoolbar-doesn-t-work-in-linux . Apparently this is a specific problem on some Linux distributions. Adding border: none after the background : rgb(30, 30, 30) fixed the problem. Don't know why my question was downvoted though.

Poly answered 27/7, 2015 at 13:31 Comment(3)
I have tested the code on my Linux machine and it works; and that thread is years old so it must be fixed by now. You need to copy and paste your code onto your question so we can help you because it's not clear which bit you're stuck on. I gave you a solution but you don't seem to have taken that into account.Damaraland
I tried your code and it didn't work as I told you already. All the code I have written is in the question above. The only piece of code I had: background : red; QToolBar { background : red } QToolButton {background : red} This didn't work. After I added border: none to QToolBar it started working. I don't know if you are confused about Qt designer. I don't need to write any c++ there. There is a property called stylesheet where I added the code above,Poly
Weird, I changed my code to background: red for all CSS rules and the results are as expected. I don't know why it wouldn't work for you though. It may be an issue with XFCE.Damaraland
G
2

You can use QT StyleSheet as below :

ui->mainToolBar->setStyleSheet("QToolButton:!hover {background-color:lightgray} QToolBar {background: rgb(30, 30, 30)}");

First color parameter I am setting for ToolBar Button's Background and Second one for Setting color of toolbar Background.

If you want to set only Background color then use StyleSheet as below:

 ui->mainToolBar->setStyleSheet("QToolBar {background: rgb(30, 30, 30)}");

Please check below image for your reference:

enter image description here

I hope you want Toolbar as per above image.

Gusti answered 27/7, 2015 at 10:37 Comment(5)
This still only applies to the area under the QAction. I created an empty project with nothing but a QMainWindow a QToolBar and one QAction, still not working. Is that code actually working for you?Poly
yes I checked !! I will put screenshot , just for your referenceGusti
@user2563661: I updated the answer. You want like this right ?Gusti
Yes exactly. This is strange. The only code I have in my project right now is MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->mainToolBar->setStyleSheet("QToolButton:!hover {background-color:darkgray} QToolBar {background: rgb(30, 30, 30)}"); } What OS are you on? I am using Xubuntu.Poly
@Poly : I am using windows 7. I checked your pasted code too, it works for meGusti
D
1

I'm using Ubuntu and user256..'s code doesn't work. But, I did try Samurai Jack's code and it works. Here is where your code doesn't work: !hover isn't valid. To get the code you want, do this:

ui->mainToolBar->setStyleSheet("QToolButton:hover {background-color: darkgray; }"
                              " QToolBar {background: rgb(30, 30, 30) }");

Where :hover is a CSS directive which is what I'm assuming you're trying to do. I'm testing this on Ubuntu 15.04 and it works.

Damaraland answered 27/7, 2015 at 11:26 Comment(5)
I am not trying to set the hover effect. I am just trying to set the background color of the whole toolbar. It seems that in my system QToolBar {background: rgb(30, 30, 30) } doesn't have any effect.Poly
Then remove the :hover directive and you should get the style you want.Damaraland
:!hover was what Samurai Jack suggested. I don't have it in my actual code. I updated in the question a picture of an empty default project where I only try to change the color of the toolbar. No effect.Poly
Firstly, it's not :!hover. Have you copy and pasted (replacing the ui->mainToolBar bit with mine? It should compile and run fine with the desired effect you want. If you don't want the hover bit, that's fine; just remove it.Damaraland
I don't have :hover in my code nor do I have :!hover as you can see in my question. This is not something I want and it was suggested by Samurai Jack. I tried your code also and it doesn't work in my system. As you can see from my updated question, the background changes for everything else except the toolbar. I even tried with some QPushButtons and other widgets and it works for everything else except QToolBar. I am starting to think that this is a bug with qt on Xubuntu 14.04 as I have seen some other weird gtk warnings before when using qt software.Poly
T
-1

I know that it is while already, but I had experienced the problem with ToolBar background and found out that it can be changed with QPalette. The main thing was that the QPalette Color Role have to be set as QPlatte::Button.

Train answered 3/12, 2018 at 16:54 Comment(1)
This works only if autoFillBackground is set to true.Austronesia

© 2022 - 2024 — McMap. All rights reserved.