8

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.

user2563661
  • 314
  • 1
  • 3
  • 13
  • Give us more context of your code please. – x squared Jul 27 '15 at 09:58
  • 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). – user2563661 Jul 27 '15 at 10:39
  • 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. – Mailerdaimon Jul 27 '15 at 13:32
  • Added more information – user2563661 Jul 27 '15 at 13:36

4 Answers4

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.

user2563661
  • 314
  • 1
  • 3
  • 13
  • 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. – Poriferous Jul 27 '15 at 13:39
  • 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, – user2563661 Jul 27 '15 at 13:43
  • 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. – Poriferous Jul 27 '15 at 13:54
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.

Amol Saindane
  • 1,568
  • 10
  • 19
  • 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? – user2563661 Jul 27 '15 at 10:47
  • yes I checked !! I will put screenshot , just for your reference – Amol Saindane Jul 27 '15 at 10:49
  • @user2563661: I updated the answer. You want like this right ? – Amol Saindane Jul 27 '15 at 10:56
  • 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. – user2563661 Jul 27 '15 at 10:59
  • @user2563661 : I am using windows 7. I checked your pasted code too, it works for me – Amol Saindane Jul 27 '15 at 11:19
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.

Poriferous
  • 1,566
  • 4
  • 20
  • 33
  • 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. – user2563661 Jul 27 '15 at 11:34
  • Then remove the `:hover` directive and you should get the style you want. – Poriferous Jul 27 '15 at 11:35
  • :!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. – user2563661 Jul 27 '15 at 11:39
  • 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. – Poriferous Jul 27 '15 at 11:44
  • 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. – user2563661 Jul 27 '15 at 12:52
-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.

Dmytro
  • 1
  • 1