QTreeWidget item and colored text



  • How I can do it:

    @QTreeWidgetItem *item = new QTreeWidgetItem;
    item->setText("FirstColoredText <font color = "another color">SecondColoredText</font>"); //-its not work@



  • Hello stima_ua,

    You should do like this to change the color of a QTreeWidgetItem:
    @QTreeWidgetItem* item = new QTreeWidgetItem(this);
    item->setText("FirstColoredText");
    item->setTextColor(0, QColor("red"));@

    But this works only with one color per item. I don't know if it possible to make the item's text multicolor.

    Cheers!



  • Sorry setTextColor() is obsolete. You should use: @void QTableWidgetItem::setForeground ( const QBrush & brush )@ And for the font: @void QTableWidgetItem::setFont ( const QFont & font )@ I don't see a way to put HTML text on an QTreeWidgetItem... maybe somebody knows.



  • There is no direct way to do that. I have worked on a rich-text capable delegate. It sort-of worked, but I came to the conclusion that it was not feasible to make the delegate in such a way that it would work nicely with rendering things like styled selection. To do it properly, it needs to be fixed quite deep in the Qt rendering code.



  • This functionality already exists for other objects, like QTextEdit. It's extrange that Qt developpers didn't thought of extending it.



  • I found a solution, but there were problems.

    When I don't redirect to parent painter i have not stylesheet. But when I redirect I have two text: text by control(rich text) and text by qtreewidgetitem.

    What I should do?

    @
    void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QStyleOptionViewItemV4 newOption(option);
    initStyleOption(&newOption, index);

    painter->save();
    
    QTextDocument doc;
    doc.setHtml(newOption.text);
    
    newOption.text = "";
    newOption.state &= ~QStyle::State_Selected;
    newOption.state &= ~QStyle::State_HasFocus;
    newOption.state &= ~QStyle::State_MouseOver;
    
    newOption.widget->style()->drawControl(QStyle::CE_ItemViewItem, &newOption, painter);
    
    painter->translate(newOption.rect.left() + 20, newOption.rect.top());
    QRect clip(0, 0, newOption.rect.width(), newOption.rect.height());
    doc.drawContents(painter, clip);
    
    painter->restore();
    

    // QStyledItemDelegate::paint(painter, newOption, index);
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.