7

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.

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 )
fredrik
  • 9,631
  • 16
  • 72
  • 132

1 Answers1

6

You could use html/css-likes styles, i.e just wrap your text inside tags:

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.

In the case of a combo-box use 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.

dorvak
  • 9,219
  • 4
  • 34
  • 43
  • 2
    It is not in the data I wish to style the text, it is in the actual visible text for each item; QComboBox.addItem() and QListWidgetItem.setText(). Sorry if I was unclear on that. I tried doing what you suggested for this visible text, but that doesn't seem to work. – fredrik Mar 07 '14 at 11:32
  • 1
    `item.setFont(QtGui.QFont('Verdana', bold=True))` seems to work for QListWidgetItem, but I can't get this to work with QComboBox.addItem() as I cannot perform setFont on a str. Any ideas? – fredrik Mar 07 '14 at 11:42
  • See my edits ;) This works for me (put this inside your loop) – dorvak Mar 07 '14 at 12:27
  • 1
    Another way of achieving bold: `QtGui.QFont('Tahoma', 8, QtGui.QFont.Bold)` – fredrik Mar 07 '14 at 12:57