python pyqt6 qStyleditemdelegate设置前景文本颜色
我正在尝试设置桌面视图的一列的前景颜色。我正在尝试使用qStyledIteMdelegate()
类的initstyleoption()
。我能够通过paint()
方法使颜色变为红色,但它以我做的方式吸引了两个项目。这是我目前要做的事情:
class MainGUI(qt.QMainWindow):
def __init__(self):
super().__init__()
self.resize(653, 500)
frame = qt.QFrame()
self.setCentralWidget(frame)
self.grid = qt.QGridLayout(frame)
self.model = QStandardItemModel()
self.view = qt.QTableView()
self.view.setModel(self.model)
self.view.setItemDelegate(TableDisplay())
self.grid.addWidget(self.view, 0, 0)
lst1 = [QStandardItem('testing...')] * 10
lst2 = [QStandardItem('more...')] * 10
self.model.appendColumn(lst1)
self.model.appendColumn(lst2)
class TableDisplay(qt.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(TableDisplay, self).initStyleOption(option, index)
if index.column() == 1:
option.palette.setColor(QPalette.ColorRole(0), Qt.GlobalColor.red)
如何使第一列变成红色,而不是黑色?使用上述代码,没有更改。没有错误(我可以使用),但演示文稿没有变化。我想念什么?
I'm trying to set the foreground color of one column of my TableView. I am trying with initStyleOption()
of the QStyledItemDelegate()
class. I was able to get the color to be red via the paint()
method, but it drew two items in the way I did it. Here's what I'm currently trying to do:
class MainGUI(qt.QMainWindow):
def __init__(self):
super().__init__()
self.resize(653, 500)
frame = qt.QFrame()
self.setCentralWidget(frame)
self.grid = qt.QGridLayout(frame)
self.model = QStandardItemModel()
self.view = qt.QTableView()
self.view.setModel(self.model)
self.view.setItemDelegate(TableDisplay())
self.grid.addWidget(self.view, 0, 0)
lst1 = [QStandardItem('testing...')] * 10
lst2 = [QStandardItem('more...')] * 10
self.model.appendColumn(lst1)
self.model.appendColumn(lst2)
class TableDisplay(qt.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(TableDisplay, self).initStyleOption(option, index)
if index.column() == 1:
option.palette.setColor(QPalette.ColorRole(0), Qt.GlobalColor.red)
How can I get column 1 to be red, and not black? With the above code, there is no change. No errors (which I could work with), but no change in presentation. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论