使用特定样式向QTreeView添加项目?

时间:2013-11-21 20:59:23

标签: pyqt

给出以下代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *

import sys

class MyWidget(QWidget):

    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        self.treeView = QTreeView(self)
        self.treeView.setGeometry(QRect(10, 20, 601, 231))
        self.treeView.setObjectName("treeView")
        self.model = QStandardItemModel()
        self.treeView.setModel(self.model)
        self.model.appendRow(QStandardItem("<b>Hello</b>"))
        self.model.appendRow(QStandardItem("World"))


if __name__=='__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    app.exec_()

如何让<b>Hello</b>文字实际上以粗体显示?

1 个答案:

答案 0 :(得分:4)

我使用QTreeWidget解决了这个问题:

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *

import sys

class MyWidget(QWidget):

    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        self.treeView = QTreeWidget(self)
        self.treeView.setGeometry(QRect(10, 20, 601, 231))
        self.treeView.setObjectName("treeView")
        t = QTreeWidgetItem(self.treeView)
        font = QFont()
        font.setWeight(QFont.Bold)
        t.setFont(0, font)
        t.setText(0, "hello")
        QTreeWidgetItem(self.treeView).setText(0,"world")


if __name__=='__main__':
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    app.exec_()