How to style (rich text) in QListWidgetItem and QCombobox items? (PyQt/PySide)

2024/11/6 9:52:34

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 = serverelse: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 )
Answer

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.

https://en.xdnf.cn/q/70956.html

Related Q&A

Reconnecting to device with pySerial

I am currently having a problem with the pySerial module in Python. My problem relates to connecting and disconnecting to a device. I can successfully connect to my device and communicate with it for a…

How to check if a sentence is a question with spacy?

I am using spacy library to build a chat bot. How do I check if a document is a question with a certain confidence? I know how to do relevance, but not sure how to filter statements from questions.I a…

Python: Lifetime of module-global variables

I have a shared resource with high initialisation cost and thus I want to access it across the system (its used for some instrumentation basically, so has to be light weight). So I created a module man…

Power spectrum with Cython

I am trying to optimize my code with Cython. It is doing a a power spectrum, not using FFT, because this is what we were told to do in class. Ive tried to write to code in Cython, but do not see any di…

Detect abbreviations in the text in python

I want to find abbreviations in the text and remove it. What I am currently doing is identifying consecutive capital letters and remove them.But I see that it does not remove abbreviations such as MOOC…

filtering of tweets received from statuses/filter (streaming API)

I have N different keywords that i am tracking (for sake of simplicity, let N=3). So in GET statuses/filter, I will give 3 keywords in the "track" argument.Now the tweets that i will be recei…

UndefinedError: current_user is undefined

I have a app with flask which works before But Now I use Blueprint in it and try to run it but got the error so i wonder that is the problem Blueprint that g.user Not working? and how can I fix it Thn…

Scipy filter with multi-dimensional (or non-scalar) output

Is there a filter similar to ndimages generic_filter that supports vector output? I did not manage to make scipy.ndimage.filters.generic_filter return more than a scalar. Uncomment the line in the cod…

How do I stop execution inside exec command in Python 3?

I have a following code:code = """ print("foo")if True: returnprint("bar") """exec(code) print(This should still be executed)If I run it I get:Tracebac…

sqlalchemy concurrency update issue

I have a table, jobs, with fields id, rank, and datetime started in a MySQL InnoDB database. Each time a process gets a job, it "checks out" that job be marking it started, so that no other p…