4

I have a Qt QMenu in my app, consisting of two levels (the top level of submenus, then each submenu containing actions), and we have a custom dark-grey style whereby the menu background is grey, the text color is white, and the right arrow is white. When a submenu item is highlighted (mouse-over), the item background is white, the text is black, and I want the right arrow to switch to a black image as well. I'm using a CSS stylesheet to do this. However, I can't seem to find the right syntax to set an alternate right-arrow image for the item selected state. My CSS looks like this:

QMenu
{
  background-color: rgb( 24, 24, 24 );
  color: white;
}

QMenu::item:selected
{
  background-color: white;
  color: black;
}

QMenu::right-arrow
{
  image: url(Resources/MenuRight.png);
}

I've tried the following additions after the above code (where MenuRightSelected.png is an inverted-color image of MenuRight.png):

QMenu::right-arrow:selected
{
  image: url(Resources/MenuRightSelected.png);
}

and

QMenu::item::right-arrow:selected
{
  image: url(Resources/MenuRightSelected.png);
}

Neither of these affect the displayed QMenu. Does anyone know if it is possible to do what I'm after, and if so, how?

  • Does your image is in a resource file ? – Adrien BARRAL Aug 14 '12 at 16:27
  • I tryied with the first code snippest you provide and that works well. For me image is called "LEFT" and it is in a resource file (.qrc) so i wrote : image: url(:/icons-tda/LEFT.png); – Adrien BARRAL Aug 14 '12 at 16:32
  • @abarral: The image is on disk (Resources/MenuRight.png is displayed correctly, so finding the images isn't the problem). Regarding your second comment, the first code snippet works for me too - however, my problem is that I want the image to change when the submenu is highlighted, and the second image (Resources/MenuRightSelected.png) never gets displayed. – Eric Greveson Aug 16 '12 at 08:32

1 Answers1

1

Possibly you only have to change the order of the items in your css file. I once heard that Qt parses the css files from upwards. Items further up overwrite the behaviour of items further down.

Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
  • Ralph - do you mean that I should try QMenu::right-arrow:selected above QMenu::right-arrow? Just to test this theory, I tried it out, but with no success. I'm sure the CSS file is parsed top-down, but I would imagine that in this case, the selectors would take precedence over file order (and file order would only be used to resolve conflicts, e.g. duplicate selectors, of which there should be none in this case). – Eric Greveson Aug 16 '12 at 08:36
  • Maybe, I shouldn't have posted that, since I might be wrong. It's just what I heard from a colleage, who figured this out after two days of debugging frustration. I just want to safe you from this latent hurdle. – Ralph Tandetzky Aug 16 '12 at 13:15