python pyqt6 qStyleditemdelegate设置前景文本颜色

发布于 2025-02-11 20:17:14 字数 1127 浏览 2 评论 0原文

我正在尝试设置桌面视图的一列的前景颜色。我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文