QListWidget change part color of text

Error processing SSI file

Answers

  1. Koda

    • 2021/7/22

    QListWidget by default does not render Html, but for this Qt has the delegate classes that allow customize the view.

    In this case we use the following delegate:

    #ifndef HTMLDELEGATE_H
    #define HTMLDELEGATE_H
    
    #include <QPainter>
    #include <QStyledItemDelegate>
    #include <QTextDocument>
    
    class HtmlDelegate : public QStyledItemDelegate
    {
    public:
        void paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
        {
            QStyleOptionViewItem options = option;
            initStyleOption(&options, index);
    
            painter->save();
    
            QTextDocument doc;
            doc.setHtml(options.text);
    
            options.text = "";
            options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
    
            painter->translate(options.rect.left(), options.rect.top());
            QRect clip(0, 0, options.rect.width(), options.rect.height());
            doc.drawContents(painter, clip);
            painter->restore();
        }
    
        QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
        {
            QStyleOptionViewItem options = option;
            initStyleOption(&options, index);
    
            QTextDocument doc;
            doc.setHtml(options.text);
            doc.setTextWidth(options.rect.width());
            return QSize(doc.idealWidth(), doc.size().height());
        }
    };
    
    #endif // HTMLDELEGATE_H
    

    Then use the setItemDelegate() method of QListWidget as shown below:

    ui->listWidget->setItemDelegate(new HtmlDelegate);
    

    Obtaining what is shown in the following image:

    The complete example can be found at the following link.

  2. Edgar

    • 2016/11/20

    I want to change the text color of the items in QListWidget . For example, some items are in red text while others are in blue text. How do I do 

  3. Bjorn

    • 2015/5/13

    I want to change the text color of the items in QListWidget. For example, some items are in red text while others are in blue text. QListWidget change part color

  4. Augustus

    • 2018/5/31

    Ps: Accually what I really want is the picture below: When I type the word "cola", the list of QListwidget will Highlight/Change the color "% 

  5. Zahir

    • 2021/7/24

    I'm trying to change the color(s) of the selected item from the default blue and light grey to a predefined rgb color and seem to be having a bit of a problem. I'm using the folowing code. p.setColor(QPalette::Base, QColor(0,0,0)); // Black Background

  6. Hugo

    • 2017/9/16

    Click Here to open the Sample Picture, the red Arrow is what I want, But the output just show all the code and didn't work just like the Blue Arrow does.

  7. Carmelo

    • 2017/12/25

    In this #PyQt5 tutorial, we will learn how to change #QListWidget item font color using few lines of code in Python.Buy Me a Coffee? Your support is much app

  8. Terry

    • 2015/11/28

    I can change the text color of a Qlabel, for instance, not for a item in a list Currenltly, my code is the following: QListWidgetItem* w = ui 

  9. Casen

    • 2017/10/26

    QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.

  10. Jack

    • 2018/1/6

    Hi, I have created a QListWidget dynamically to populate menu items How do i go about changing the set current row highlighted color??

  11. Zeke

    • 2019/3/31

    QListWidgetItem:: QListWidgetItem (const QString & text, QListWidget * parent = 0, int type = Type) Constructs an empty list widget item of the specified type with the given text and parent . If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem ().

  12. Maximus

    • 2015/9/21

    You can use the color attributes for aqlistwidgetitem setBackgroundColor setTextColor using your example to set the text colour white and 

  13. Jaime

    • 2018/5/13

    QListWidgetItem:: QListWidgetItem (const QIcon & icon, const QString & text, QListWidget * parent = nullptr, int type = Type) Constructs an empty list widget item of the specified type with the given icon, text and parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem ().

  14. Lawrence

    • 2015/7/3

    QTableWidget and QListWidget style sheet settings, scroll bar style settings: Set the font and color of the header text //Get the item object of the 

  15. Alvarez

    • 2015/2/5

    If I have to change the color of a word inside the QListView, for example if in a string I get the word "outdated", how to change the color 

  16. Cain

    • 2021/7/17

    The QListView uses MVC, and will be covered in later section. Note that we set the color of the text and background color after we get the 

Comments are closed.

More Posts