如何设置 QComboBox 按钮的背景颜色?

How to set background color of QComboBox button?(如何设置 QComboBox 按钮的背景颜色?)

本文介绍了如何设置 QComboBox 按钮的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

版本/环境:

  • Windows 10 64 位
  • Qt 5.11.0 MSVC2017 64 位

我有一个简单的

现在我想更改按钮的背景颜色,而不是所选项目.

我的第一个方法是简单地使用 QWidget

我还使用了特定的变体

仅使用 QComboBox {background: red;} 与使用 background-color: red; 的结果相同,只是选择的项目不着色.

正如

我还尝试了几乎所有其他 QPalette

解决方案

QComboBox 总是很难自定义,因为它是由子小部件(甚至是条件子小部件)组成的.
我进行了测试,您的简单样式表 QComboBox {background:red} 在 Linux 上对我来说几乎可以正常工作,只是下拉菜单中的框边框也是红色的.根据 GUI 样式,行为似乎有所不同.p>

来自 doc :

<块引用>

注意:对于复杂的小部件,例如 QComboBox 和 QScrollBar,如果有的话属性或子控件是自定义的,所有其他属性或子控件也必须自定义.

如果您想自定义 按钮,您似乎必须自定义所有内容...那么如果不覆盖或抑制标准样式,就无法更改颜色.

您可以设置整个下拉菜单的样式以模仿原生外观,但这样做并不好,而且不健壮(而且不便携).

QComboBox QAbstractItemView {边框:1px 纯灰色;背景:白色;选择背景颜色:蓝色;}QComboBox {背景:红色;}

Version/Environment:

  • Windows 10 64 bit
  • Qt 5.11.0 MSVC2017 64 bit

I have a simple QComboBox to enable/disable a feature:

QComboBox *onOffComboBox = new QComboBox();
onOffComboBox->insertItem(0, "Off");
onOffComboBox->insertItem(1, "On");

The combo box is added as a cell widget to a table:

this->ui->settingsTable->setCellWidget(rowNumber, 1, onOffComboBox);

Now i want to change the background color of the button but not the select items.

My first approach was simply to use QWidget's setStyleSheet function:

onOffComboBox->setStyleSheet("background-color: red;");

But this suppresses the standard style:

I also used variations with specific QComboBox styles according to the documentation:

onOffComboBox->setStyleSheet("QComboBox::drop-down {background: red;}");

But this only colors the part with the arrow and suppresses it's style:

Using just QComboBox {background: red;} has the same result as with background-color: red; just the select items are not colored.

Just as described in this answer another approach is to use QPallete:

QPalette pal = onOffComboBox->palette();
pal.setColor(QPalette::Base, QColor("red"));
onOffComboBox->setPalette(pal);
onOffComboBox->update(); // just in case this has any effect

This only colors the select items:

I also tried nearly all other QPalette color roles:

  • QPalette::Window, QPalette::Foreground, QPalette::Button - do nothing
  • QPalette::Base - colors the select items (see pic)
  • QPalette::Text - colors the text of the button and the select items

So, how can i change the color of the QComboBox drop-down button background WITHOUT overwritting or suppressing the standard style?

The styles of the pop-up items also shouldn't change.

Here is an image of what i want:

解决方案

QComboBox is always tricky to customize because it is made of subwidgets (even conditional subwidgets).
I made tests and your simple stylesheet QComboBox {background:red} works almost fine for me on Linux, except that the box-border is also red in the dropdown. The behaviour seems to be different depending on the GUI style.

From the doc :

Note: With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.

It looks like you have to customize everything if you want to customize the button... Then it would not be possible to change the colour without overwriting or suppressing the standard style.

You could style the whole drop-down to mimic the native look, but it's not nice to do and not robust (and not portable).

QComboBox QAbstractItemView {
  border: 1px solid grey;
  background: white;
  selection-background-color: blue;
}
QComboBox {
  background: red;
}

这篇关于如何设置 QComboBox 按钮的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何设置 QComboBox 按钮的背景颜色?

基础教程推荐