How is the style (rich text) in the QListWidgetItem and QCombobox elements? (PyQt / PySide)

I asked similar questions, but did not answer and did not answer that this is an alternative solution.

I need to create a search chain in QComboBoxes and QListWidgets (in PySide), and I'm going to make the text of these elements bold. However, I find it difficult to find 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 )
+4
source share
1 answer

You can use html / css-like styles, i.e. just wrap the text inside the tags:

item.setData( QtCore.Qt.UserRole, "<b>{0}</b>".format('data to store for this QListWidgetItem'))

Another option is to set the font role:

item.setData(0, QFont("myFontFamily",italic=True), Qt.FontRole)

You may need to use QFont.setBold () in your case. However, using html formatting can be more flexible.

combo-box 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)

-, afaik.

+3

Source: https://habr.com/ru/post/1530579/


All Articles