Brendan Abel
Brendan Abel

Reputation: 37509

Using Qt stylesheets with QMenu::indicator

I'm using stylesheets to customize the appearance of one of my Qt apps (pyqt actually, but just the same).

Here's the stylesheet code I'm using for the QMenu's. It correctly colors all my QMenu's, but it also causes all my indicators (checked QMenu items) to have a checkered background, as if they are transparent.

QMenu {
    background-color: "#242424"; 
    color: "#D5D5D5";
}

If I set a background-color on the indicator, the center of the indicator background is colored correctly, but there's a large border around the center square that is still checkered and the checks stop appearing.

QMenu::indicator {
    background-color: "#242424"; 
}

I've also noticed that the properties for width and height, which are shown in the documenation examples, don't work.

Has anyone had similar problems trying to customize the color and appearance of QMenus? How did you solve them?

Upvotes: 2

Views: 4635

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37509

I found a workaround for what appears to be a documented bug in how Qt processes stylesheets for QMenu::indicators.

Instead of using the image property, I used the background-image property, which inserts the image at the native size, instead of scaling it down. It still isn't possible to set the size of the indicator, but you can use the padding property to move the Menu item text over so that the whole indicator image is displayed.

QMenu::item {
    padding: 3px 20px;
}

QMenu::indicator:checked {
    background-image: url(/path/to/image.png);
}

Upvotes: 1

Eric Hulser
Eric Hulser

Reputation: 4022

If that is all you're doing, I'd use the QPalette instead to drive your color. But if this is just the tip of the iceburg and you're looking for more style customization, you have to define almost all of the properties (can be found in the Qt docs). For whatever reason, Qt doesn't seem to do inheritance with their style sheets, so if you even try to do something like round the corners on the menu, you have to define all the other properties too.

Also, a good thing to note that I haven't really seen in the docs, is if you want to modify the stylesheet but still have access to the palette colors, you can use palette(ColorRole) in your stylesheet, like so:

QMenu {
    background-color: palette(Window);
}

At any rate...here are some options:

QPalette Solution

menu = QMenu(self)
palette = menu.palette()
palette.setColor(menu.backgroundRole(), QColor('#242424'))
menu.setPalette(palette)

Stylesheet Solution

http://doc.qt.nokia.com/4.7-snapshot/stylesheet-examples.html#customizing-qmenu

I'd copy the advanced customization one and tweak all the values, not really removing any of them since its all a house of cards.

Upvotes: 2

Related Questions