简体   繁体   中英

How do I highlight specific letters in the text of a QListWidgetItem?

I am writing a search algorithm that searches a QListWidget and returns relevant matches based on what the user types in the search bar. I would like any matches to be highlighted in a yellowish color.

For example:

If the user types "ilt" into the search bar, I would like to highlight the corresponding letters that are in the QListWidgetItem with the text "Write F ilt er" (bolded, for convenience).

Assuming that I have access to both phrases and can programmatically identify which letters in the QListWidgetItem text that I need to highlight, is there an API or algorithm that I can use to do this?

Usually you have to create a custom delegate that paints the text the way you want. You can read about it here .

But if you want to customize a QListWidget you can simply use the QListWidget::setItemWidget method:

QListWidget *list = new QListWidget;

QListWidgetItem *item = new QListWidgetItem(list);

QLabel *label = new QLabel("my <span style=\"color: red\">red</span> text");
list->setItemWidget(item, label);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM