Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget item and colored text
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget item and colored text

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 17.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stima_ua
    wrote on last edited by
    #1

    How I can do it:

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seba84
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Seba84
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Seba84
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stima_ua
              wrote on last edited by
              #6

              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);
              }@

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved