gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:29:51 25 4
gpt4 key购买 nike

鉴于以下代码:

#!/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>文字居然以粗体显示?

最佳答案

我通过使用 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_()

关于pyqt - 使用特定样式向 QTreeView 添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131761/

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