QComboBox设置背景色,每个选项的高和选中背景色
时间: 2024-03-09 13:46:31 浏览: 61
要设置QComboBox的背景色,可以使用QComboBox的setStyleSheet()方法,例如:
```
QComboBox {
background-color: green;
}
```
要设置每个选项的高度,可以使用QComboBox的view()方法来访问下拉列表视图,然后设置QListView的行高,例如:
```
QComboBox QAbstractItemView {
background-color: white;
selection-background-color: blue;
font: 14px;
min-width: 80px;
height: 60px;
}
QComboBox QAbstractItemView::item {
height: 40px;
}
```
这个样式表将会设置QComboBox的下拉列表视图的背景颜色为白色,选中项的背景颜色为蓝色,字体大小为14px,最小宽度为80px,高度为60px,每个选项的高度为40px。
相关问题
设置qcombobox选中项的背景色
要设置QComboBox选中项的背景色,可以使用QProxyStyle类来重写QComboBox的paintEvent()方法。在该方法中,可以使用QStyleOptionComboBox类来获取当前选中项的矩形区域,并设置它的背景色。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QComboBox, QProxyStyle, QStyleOptionComboBox
from PyQt5.QtGui import QPainter, QColor
class ComboBoxStyle(QProxyStyle):
def drawControl(self, element, option, painter, widget=None):
if element == QComboBox.ComboboxPopup:
combo_option = QStyleOptionComboBox(option)
if widget:
index = widget.currentIndex()
if index.isValid():
rect = self.subControlRect(QStyle.CC_ComboBox, combo_option, QStyle.SC_ComboBoxEditField, widget)
combo_option.currentTextRect = rect
current_rect = self.subControlRect(QStyle.CC_ComboBox, combo_option, QStyle.SC_ComboBoxArrow, widget)
combo_option.rect = current_rect
painter.save()
painter.fillRect(rect, QColor(0, 0, 255)) # 设置选中项的背景色
self.drawPrimitive(QStyle.PE_IndicatorArrowDown, combo_option, painter, widget)
painter.restore()
else:
self.drawPrimitive(element, combo_option, painter, widget)
else:
self.drawPrimitive(element, option, painter, widget)
class MyComboBox(QComboBox):
def __init__(self, *args, **kwargs):
super(MyComboBox, self).__init__(*args, **kwargs)
self.setStyle(ComboBoxStyle())
```
在上面的代码中,ComboBoxStyle类继承自QProxyStyle类,并重写了drawControl()方法。该方法用于绘制QComboBox的控件,其中QComboBox.ComboboxPopup表示下拉框的弹出菜单。在绘制选中项时,先获取当前选中项的矩形区域,然后使用painter.fillRect()方法设置选中项的背景色。最后调用drawPrimitive()方法来绘制下拉框的箭头。
在MyComboBox类中,我们使用ComboBoxStyle类来设置QComboBox的样式。这样,在使用MyComboBox类创建下拉框时,就会自动应用我们定义的样式。
QCOmbox的item设置背景色
要设置QComboBox的item的背景色,可以使用QStyledItemDelegate类来自定义QComboBox的item样式。以下是一个例子:
```python
from PyQt5.QtWidgets import QStyledItemDelegate, QComboBox, QApplication, QStyleOptionViewItem
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtCore import Qt
class ComboBoxDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
painter.save()
# 绘制背景色
if option.state & QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight())
else:
painter.fillRect(option.rect, QColor(255, 255, 255))
# 绘制文本
painter.setPen(Qt.black)
painter.drawText(option.rect, Qt.AlignLeft | Qt.AlignVCenter, index.data())
painter.restore()
class MyComboBox(QComboBox):
def __init__(self, parent=None):
super().__init__(parent)
self.setItemDelegate(ComboBoxDelegate())
if __name__ == '__main__':
app = QApplication([])
combo = MyComboBox()
combo.addItem('Item 1')
combo.addItem('Item 2')
combo.show()
app.exec_()
```
在这个例子中,我们自定义了一个QStyledItemDelegate类来绘制QComboBox的item。在paint()方法中,我们首先绘制了背景色,如果item处于选中状态,我们使用QStyleOptionViewItem中的highlight()方法来获取选中时的背景色,否则使用白色填充背景。然后我们绘制了文本。最后,在MyComboBox的构造函数中,我们设置了自定义的item delegate。