gpt4 book ai didi

qt - 无法更改自定义QListView行的突出显示的文本颜色

转载 作者:行者123 更新时间:2023-12-04 00:52:45 26 4
gpt4 key购买 nike

由于某些奇怪的原因,无法覆盖QListView中突出显示的文本颜色。效果很好(突出显示的文本颜色会自动更改为白色),直到我定义了自己的小部件来代表一行为止。

现在,我可以更改所选行的背景颜色和其他一些视觉效果,但是文本颜色始终保持默认的黑色。

已经尝试使用QSSQPalettedata() / Qt.ForegroundRole进行所有可能的操作-技巧无济于事。

这是一个简化的代码,仍然受到OS X问题的困扰。不幸的是,我没有机会在WindowsGNU/Linux上进行测试。

from PySide.QtCore import *
from PySide.QtGui import *
import sys

view = None
mapp = {}

style = '''
QListView {
show-decoration-selected: 1;
selection-color: white;
selection-background-color: #0068d9;
}

QListView::item:selected:active:hover{
background-color:red; color: white;
}
QListView::item:selected:active:!hover{
background-color: #0068d9; color: white;
}
QListView::item:selected:!active{
background-color:yellow; color: white;
}
QListView::item:!selected:hover{
background-color:green; color: white;
}
'''

class SimpleListModel(QAbstractListModel):

def __init__(self, mlist):
QAbstractListModel.__init__(self)
self._items = mlist

def rowCount(self, parent = QModelIndex()):
return len(self._items)

def index(self, row, column, parent=QModelIndex()):
node = self._items[row]

if not(str(row) in mapp):
index = self.createIndex(row, column)
widget = QLabel(node)
view.setIndexWidget(index, widget)
mapp[str(row)] = index
return index

return mapp[str(row)]

def data(self, index, role = Qt.DisplayRole):
return None

def flags(self, index):
return Qt.ItemIsSelectable | Qt.ItemIsEnabled

class MyMainWindow(QWidget):
def __init__(self):
global view
QWidget.__init__(self, None)

self._model = SimpleListModel(["test", "tes1t", "t3est", "t5est", "t3est"])

vbox = QVBoxLayout()
view = QListView()
view.setModel(self._model)
vbox.addWidget(view)
self.setLayout(vbox)

view.setStyleSheet(style)

first = self._model.index(0, 0)
view.setCurrentIndex(first)


if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyMainWindow()
w.show()
w.raise_()
app.exec_()
sys.exit()

最佳答案

不是最干净或最好的解决方案,但这是我经过一些调整后得出的。

结果:



蓝色项目是选中的项目。绿色项目是悬停的项目。

码:

from PySide.QtCore import *
from PySide.QtGui import *
import sys

view = None
mapp = {}

style = '''
QListView {
show-decoration-selected: 1;
selection-color: white;
selection-background-color: #0068d9;
}

QListView::item:selected:active:hover{
background-color:red; color: white;
}
QListView::item:selected:active:!hover{
background-color: #0068d9; color: white;
}
QListView::item:selected:!active{
background-color:yellow; color: white;
}
QListView::item:!selected:hover{
background-color:green; color: white;
}
'''

class SimpleListModel(QAbstractListModel):

def __init__(self, mlist):
QAbstractListModel.__init__(self)
self._items = mlist

def rowCount(self, parent = QModelIndex()):
return len(self._items)

def index(self, row, column, parent=QModelIndex()):
node = self._items[row]

if not(str(row) in mapp):
index = self.createIndex(row, column)
widget = QLabel(node)
view.setIndexWidget(index, widget)

mapp[str(row)] = index
return index


return mapp[str(row)]

def data(self, index, role = Qt.DisplayRole):
# The following code shouldn't be put in this function but i'm in a hurry right now...
selectedIndexes = view.selectedIndexes()

# Set all items to black
for i in range(0, self.rowCount()):
currentRowIndex = self.index(i, 0, QModelIndex())
myWidget = view.indexWidget(currentRowIndex)
myWidget.setStyleSheet("color: black")

# Set selected items to white
for i in selectedIndexes:
myWidget = view.indexWidget(i)
myWidget.setStyleSheet("color: white")

return None

def flags(self, index):
return Qt.ItemIsSelectable | Qt.ItemIsEnabled

class MyMainWindow(QWidget):
def __init__(self):
global view
QWidget.__init__(self, None)

self._model = SimpleListModel(["test", "tes1t", "t3est", "t5est", "t3est"])

vbox = QVBoxLayout()
view = QListView()
view.setModel(self._model)
vbox.addWidget(view)
self.setLayout(vbox)

view.setStyleSheet(style)

first = self._model.index(0, 0)
view.setCurrentIndex(first)


if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyMainWindow()
w.show()
w.raise_()
app.exec_()
sys.exit()


如果有我听不懂或代码不清楚的地方,请告诉我。

关于qt - 无法更改自定义QListView行的突出显示的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476876/

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