gpt4 book ai didi

python - PyQt5 ComboBox - 如何在不影响下拉列表的情况下设置 CurrentText 的颜色?

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:38 25 4
gpt4 key购买 nike

以下代码段正确设置了 ComboBox 下拉列表中各个条目的颜色。 However, when an item is selected and transferred to the CurrentText field, all of the entries in the dropdown change to the color of CurrentText.如何转换条目的颜色以显示为 CurrentText 而不影响下拉列表?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class ComboDemo(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):

def combo_changed():
for color in ('red', 'green', 'blue'):
if color == cb.currentText():
cb.setStyleSheet('color: {}'.format(color))

grid = QGridLayout()
cb = QComboBox()
grid.addWidget(cb, 0, 0)
model = cb.model()
for color in ('red', 'green', 'blue'):
entry = QStandardItem(color)
entry.setForeground(QColor(color))
model.appendRow(entry)

cb.currentIndexChanged.connect(combo_changed)

self.setLayout(grid)
self.show()

app = QApplication(sys.argv)
c = ComboDemo()
app.exec_()

最佳答案

你必须使用QComboBox:editable:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class ComboDemo(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):

def combo_changed():
for color in ('red', 'green', 'blue'):
if color == cb.currentText():
cb.setStyleSheet("QComboBox:editable{{ color: {} }}".format(color))

grid = QGridLayout()
cb = QComboBox()
grid.addWidget(cb, 0, 0)
model = cb.model()
for color in ('red', 'green', 'blue'):
entry = QStandardItem(color)
entry.setForeground(QColor(color))
model.appendRow(entry)

cb.currentIndexChanged.connect(combo_changed)
self.setLayout(grid)
self.show()

app = QApplication(sys.argv)
c = ComboDemo()
app.exec_()

关于python - PyQt5 ComboBox - 如何在不影响下拉列表的情况下设置 CurrentText 的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50711763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com