Qt样式表-组合框列表颜色

时间:2019-04-10 17:05:42

标签: qt qtstylesheets

我正在使用Qt Designer样式表。这是Qt Designer生成的ui文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'config_assign.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_ConfigAssign(object):
    def setupUi(self, ConfigAssign):
        ConfigAssign.setObjectName("ConfigAssign")
        ConfigAssign.resize(590, 416)
        ConfigAssign.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        ConfigAssign.setStyleSheet("background-color: rgb(25, 35, 45);")
        self.centralwidget = QtWidgets.QWidget(ConfigAssign)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.frame_4 = QtWidgets.QFrame(self.centralwidget)
        self.frame_4.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.frame_4.setFrameShadow(QtWidgets.QFrame.Plain)
        self.frame_4.setLineWidth(0)
        self.frame_4.setObjectName("frame_4")
        self.label_2 = QtWidgets.QLabel(self.frame_4)
        self.label_2.setGeometry(QtCore.QRect(30, 30, 281, 61))
        font = QtGui.QFont()
        font.setFamily("Roboto")
        font.setPointSize(28)
        font.setUnderline(False)
        self.label_2.setFont(font)
        self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_2.setStyleSheet("color: rgb(255, 255, 255);")
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.frame_4, 0, 0, 1, 1)
        self.frame_10 = QtWidgets.QFrame(self.centralwidget)
        self.frame_10.setStyleSheet("#frame_10 {border-radius: 7px; border-style: solid; border-width: 0.5px;\n"
"border-color: rgb(69,90,100);}")
        self.frame_10.setFrameShape(QtWidgets.QFrame.WinPanel)
        self.frame_10.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_10.setObjectName("frame_10")
        self.label = QtWidgets.QLabel(self.frame_10)
        self.label.setGeometry(QtCore.QRect(120, 30, 101, 25))
        font = QtGui.QFont()
        font.setFamily("Roboto")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(255, 255, 255);")
        self.label.setObjectName("label")
        self.customerlistbox = QtWidgets.QComboBox(self.frame_10)
        self.customerlistbox.setGeometry(QtCore.QRect(20, 70, 291, 31))
        font = QtGui.QFont()
        font.setFamily("Roboto")
        font.setPointSize(12)
        self.customerlistbox.setFont(font)
        self.customerlistbox.setStyleSheet("#customerlistbox {color: rgb(255, 255, 255);\n"
"border-color: rgb(69,90,100);\n"
"border-width: 0.5px;\n"
"border-style: solid;\n"
"border-radius: 3px;\n"
"padding: 1px 0px 1px 3px;}\n"
"\n"
"#customerlistbox QAbstractItemView {color: rgb(255, 255, 255);}")
        self.customerlistbox.setCurrentText("")
        self.customerlistbox.setMaxVisibleItems(20)
        self.customerlistbox.setObjectName("customerlistbox")
        self.gridLayout.addWidget(self.frame_10, 1, 0, 1, 1)
        ConfigAssign.setCentralWidget(self.centralwidget)

        self.retranslateUi(ConfigAssign)
        QtCore.QMetaObject.connectSlotsByName(ConfigAssign)

    def retranslateUi(self, ConfigAssign):
        _translate = QtCore.QCoreApplication.translate
        ConfigAssign.setWindowTitle(_translate("ConfigAssign", "Test Prog"))
        self.label_2.setText(_translate("ConfigAssign", "TEST"))
        self.label.setText(_translate("ConfigAssign", "SELECT:"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ConfigAssign = QtWidgets.QMainWindow()
    ui = Ui_ConfigAssign()
    ui.setupUi(ConfigAssign)
    ConfigAssign.show()
    sys.exit(app.exec_())

下面是一个示例程序,该程序调用上述名为config_assign_ui.py的文件:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QApplication
from config_assign_ui import Ui_ConfigAssign


class Main(QtWidgets.QMainWindow, Ui_ConfigAssign):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.combo_list()

    def combo_list(self):
        self.customerlistbox.clear()
        list1 = ['A','B','C','D','E','F','G','H']
        self.customerlistbox.addItems(list1)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('WindowsVista')
    window = Main()
    window.show()
    sys.exit(app.exec_())

该下拉列表包含几行项目。组合框中的文本根据需要为白色。但是,当您按下下拉菜单时,其余项目的文本为黑色,而我想将它们全部变为白色。

enter image description here

在样式表中是否可以做些什么来实现此目的?我要输入到Designer中的样式表代码是这样的:

#customerlistbox {color: rgb(255, 255, 255);
border-color: rgb(69,90,100);
border-width: 0.5px;
border-style: solid;
border-radius: 3px;
padding: 1px 0px 1px 3px;}

#customerlistbox QAbstractItemView {color: rgb(255, 255, 255);}

0 个答案:

没有答案