I have found similar questions being asked, but without answers or where the answer is an alternative solution.
我也遇到过类似的问题,但没有答案,或者答案是另一种解决方案。
I need to create a breadcrumb trail in both QComboBoxes and QListWidgets (in PySide), and I'm thinking making these items' text bold. However, I have a hard time finding information on how to achieve this.
我需要在qcombobox和QListWidgets(在PySide)中创建一个breadcrumb trail,我正在考虑将这些项目的文本加粗。然而,我很难找到如何实现这一点的信息。
This is what I have:
这就是我所拥有的:
# QComboBox
for server in servers:
if optionValue == 'top secret':
optionValue = server
else:
optionValue = '<b>' + server + '</b>'
self.comboBox_servers.addItem( optionValue, 'data to store for this QCombobox item' )
# QListWidgetItem
for folder in folders:
item = QtGui.QListWidgetItem()
if folder == 'top secret':
item.setText( '<b>' + folder + '</b>' )
else:
item.setText( folder )
iconSequenceFilepath = os.path.join( os.path.dirname(__file__), 'folder.png' )
item.setIcon( QtGui.QIcon(r'' + iconSequenceFilepath + ''))
item.setData( QtCore.Qt.UserRole, 'data to store for this QListWidgetItem' )
self.listWidget_folders.addItem( item )
1 个解决方案
#1
3
You could use html/css-likes styles, i.e just wrap your text inside tags:
你可以使用html/css样式,i。e将你的文本包装在标签内:
item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))
Another option is setting a font-role:
另一个选择是设置字体角色:
item.setData(0, QFont("myFontFamily",italic=True), Qt.FontRole)
Maybe you'd have to use QFont.setBold() in your case. However, using html-formating might be more flexible at all.
也许您需要在您的案例中使用QFont.setBold()。然而,使用html格式可能更加灵活。
In the case of a combo-box use setItemData():
对于combobox使用setItemData():
# use addItem or insertItem (both works)
# the number ("0" in this case referss to the item index)
combo.insertItem(0,"yourtext"))
#set at tooltip
combo.setItemData(0,"a tooltip",Qt.ToolTipRole)
# set the Font Color
combo.setItemData(0,QColor("#FF333D"),Qt.BackgroundColorRole)
#set the font
combo.setItemData(0, QtGui.QFont('Verdana', bold=True), Qt.FontRole)
Using style-sheet formating will not work for the Item-Text itself, afaik.
使用样式表格式不能用于项目文本本身。
#1
3
You could use html/css-likes styles, i.e just wrap your text inside tags:
你可以使用html/css样式,i。e将你的文本包装在标签内:
item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))
Another option is setting a font-role:
另一个选择是设置字体角色:
item.setData(0, QFont("myFontFamily",italic=True), Qt.FontRole)
Maybe you'd have to use QFont.setBold() in your case. However, using html-formating might be more flexible at all.
也许您需要在您的案例中使用QFont.setBold()。然而,使用html格式可能更加灵活。
In the case of a combo-box use setItemData():
对于combobox使用setItemData():
# use addItem or insertItem (both works)
# the number ("0" in this case referss to the item index)
combo.insertItem(0,"yourtext"))
#set at tooltip
combo.setItemData(0,"a tooltip",Qt.ToolTipRole)
# set the Font Color
combo.setItemData(0,QColor("#FF333D"),Qt.BackgroundColorRole)
#set the font
combo.setItemData(0, QtGui.QFont('Verdana', bold=True), Qt.FontRole)
Using style-sheet formating will not work for the Item-Text itself, afaik.
使用样式表格式不能用于项目文本本身。