在 QTreeView 中设置粗体行

发布于 2024-08-30 03:50:46 字数 477 浏览 4 评论 0原文

我在 pyqt 应用程序中有一个 QTreeView 的自定义子类。我试图让用户能够突出显示和“低亮”(由于缺乏更好的术语)行。突出显示的行应具有粗体文本和(可选)不同的背景颜色。有什么想法吗?
我正在考虑将样式表作为一种选择,但到目前为止还无法使其发挥作用。如果我设置 QTreeView 的样式表:

self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")

我无法弄清楚如何手动启用仅将所需的行保留在特定状态的“状态”。如果我尝试设置单个项目的样式表:

#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()

我会收到段错误。
我不相信样式表是正确的选择,我对所有想法持开放态度。谢谢!

I have a custom subclass of a QTreeView in a pyqt application. I'm trying to give the user the ability to highlight and "lowlight" (for lack of a better term) rows. Highlighted rows should have bold text and (optionally) a different background color. Any ideas?
I'm considering StyleSheets as an option, but have so far been unable to get that to work. If I set the QTreeView's stylesheet:

self.setStyleSheet("QTreeView::item:selected {border: 1px solid #567dbc;}")

I can't figure out how to manually enable 'states' that would keep only the desired rows at a particular state. If I try setting an individual item's stylesheet:

#modelIndex is a valid QModelIndex
modelIndex.internalPointer().setStyleSheet()

I get a segfault.
I'm not convinced stylesheets are the way to go, I'm open to all ideas. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-09-06 03:50:46

从你所说的看来,最简单的解决方案是定义一个 自定义项目委托 为您的树视图并在需要时将项目字体粗细设置为粗体。请检查下面的示例是否适合您,它应该创建一个带有自定义项目委托的树视图,这将更改项目的字体样式。

import sys
from PyQt4 import QtGui, QtCore

class BoldDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        # decide here if item should be bold and set font weight to bold if needed 
        option.font.setWeight(QtGui.QFont.Bold)
        QtGui.QStyledItemDelegate.paint(self, painter, option, index)


class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        model = QtGui.QStandardItemModel()

        for k in range(0, 4):
            parentItem = model.invisibleRootItem()
            for i in range(0, 4):
                item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                parentItem.appendRow(item)
                parentItem = item

        self.view = QtGui.QTreeView()
        self.view.setModel(model)
        self.view.setItemDelegate(BoldDelegate(self))

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

希望这有帮助,问候

From what you've said it looks like the easiest solution would be to define a custom item delegate for your treeview and set items font weight to bold whenever it's needed. Pls, check if an example below would work for you, it should create a treeview with custom item delegate which would change item's font style.

import sys
from PyQt4 import QtGui, QtCore

class BoldDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        # decide here if item should be bold and set font weight to bold if needed 
        option.font.setWeight(QtGui.QFont.Bold)
        QtGui.QStyledItemDelegate.paint(self, painter, option, index)


class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        model = QtGui.QStandardItemModel()

        for k in range(0, 4):
            parentItem = model.invisibleRootItem()
            for i in range(0, 4):
                item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                parentItem.appendRow(item)
                parentItem = item

        self.view = QtGui.QTreeView()
        self.view.setModel(model)
        self.view.setItemDelegate(BoldDelegate(self))

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

hope this helps, regards

千年*琉璃梦 2024-09-06 03:50:46

我可以想出几种方法来做到这一点。如果您有权访问模型,最简单的方法是在模型中添加一些索引的状态跟踪,并为 data() 函数中请求的角色返回正确的选项。这样做的缺点是,如果您在不同的视图中使用相同的模型,并且希望将高光保留在一个视图的本地。

第二简单的方法可能是创建一个代理模型,它跟踪数据本身,并从原始模型中获取所有其他数据。在这种情况下(您不更改原始模型的行或列),这可能非常容易。

最难的是创建一个自定义委托来跟踪应突出显示哪些行/列,并根据其正在绘制的模型索引的行/列以不同的方式绘制自身。您必须保留对委托的访问权限,以便您可以告诉它需要设置哪些行和列。您还需要处理模型更改时发生的情况。

I can think of a few ways to do this. The easiest, if you have access to the model, is to add some state tracking of the indexes in the model, and return the proper options for the roles requested in the data() function. The drawback to this is if you are using the same model in different views, and want to keep the highlights local to one view.

The second-easiest is probably to make a proxy model, which keeps track of the data itself, and gets all other data from the original model. In this situation (where you aren't changing the rows or columns of the original model) it would probably be pretty easy.

The hardest would be to make a custom delegate that keeps track of which rows/columns should be highlighted, and draws itself differently based on the row/column of the model index it is drawing. You would have to keep access to the delegate so that you could tell it which rows and columns need to be set. You would also need to deal with what happens when the model changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文