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. Styling branches in QTreeWidget per item
Forum Updated to NodeBB v4.3 + New Features

Styling branches in QTreeWidget per item

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.3k 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by Chris Kawa
    #1

    Hi,

    I'm styling a QTreeWidget by coloring its rows like so:
    treeview

    This is the code I used:

        QTreeWidgetItem * item, * child;
        QStringList texts;
        texts << "Label" << "value";
    
        item = new QTreeWidgetItem(treeWidget, texts);
        child = new QTreeWidgetItem(item, texts);
        item->setBackgroundColor(0,Qt::red);
        child->setBackgroundColor(0,Qt::red);
        item->setBackgroundColor(1,Qt::cyan);
        child->setBackgroundColor(1,Qt::cyan);
    
        item = new QTreeWidgetItem(treeWidget, texts);
        child = new QTreeWidgetItem(item, texts);
        item->setBackgroundColor(0,Qt::green);
        child->setBackgroundColor(0,Qt::green);
        item->setBackgroundColor(1,Qt::cyan);
        child->setBackgroundColor(1,Qt::cyan);
    

    Nothing special, but I'd like to make branches the same color as labels, so that the whole row is colored. Is there any way to do that per item?

    The only way I know to style branches is to use QTreeView::branch css style, but that applies one style to all of them, and I need something per-item based.
    Item delegate doesn't draw branches from what I know, does it?

    I'd like to avoid reimplementing whole QTreeWidget drawing if possible. Any ideas?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Just some quick brainstorming

      • subclass [[Doc:QStyledItemDelegate]]
      • reimplement paint, set the background in the option and call the base class' implementation with the manipulated data

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Thanks Volker,

        The delegate by default does not draw the branch part of the row, only the column data part (red, green and cyan parts on the pic), so changing bg color in paint options isn't enough (btw. bg color is for some strange reason set to RGBA {1,1,1,0} there so the way to get it is from index bg color role).

        Anyway Your suggestion was close enough to lead me to this solution:

        class MyDelegate: public QStyledItemDelegate
        {
        public:
            void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                QStyledItemDelegate::paint(painter, option, index);
        
                if(index.column() == 0)
                {
                    QRect branchRect(0, option.rect.top(), option.rect.left(), option.rect.height());
                    painter->setCompositionMode(QPainter::CompositionMode_Multiply);
                    painter->fillRect(branchRect, index.data(Qt::BackgroundColorRole).value<QColor>());
                    painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
                }
            }
        };
        

        As I imagine this is not 100% correct. Overpainting the branch area in items paint event will not work if on some platforms branches are drawn AFTER the items themselves, but I assume this won't happen in any sane implementation.
        Of course, as Everett McGill said "assumption is the mother of all f**kups", but hey, it works on my machine :)

        Thanks a bunch again.

        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