2
votes

I'm working on a little program and I have a bunch of panels in it. I want it so that when I focus into a panel, it draws a thin inline around it to show that it is focused. I got it working with all my panels except my tree view.

Here's an example that works with a QWidget:

void Test::paintEvent(QPaintEvent *event)
{
    if(hasFocus())
    {
        QPainter painter(this);

        QPen pen(Qt::blue);
        pen.setWidth(1);
        painter.setPen(pen);

        painter.drawRect(geometry());

    }

    QWidget::paintEvent(event);
}

When I use the QPainter on the QTreeWidget I get these debug messages in the console:

  • QWidget::paintEngine: Should no longer be called
  • QPainter::begin: Paint device returned engine == 0, type: 1
  • QPainter::setPen: Painter not active
  • QPainter::drawRects: Painter not active

So my problem is that I can't use QPainter with the QTreeWidget so I am unable to draw my outline. Is there an alternative method I can use?

1
Why not use paintEvent in a QTreeWidget? You can call QTreeWidget::paintEvent(event) to perform the painting of the superclassmsrd0
That's what I'm already doing. I always call the superclass in the paint event, I did however forget to add it to the given example. But that's not my problem anyway. My problem is I need to draw a rect around the geometry of the QTreeWidget, which I cannot do with just by calling the superclass. I need like a QPainter or something...David Ludwig
Why can't you create a QPainter therefore? A QTreeWidget should be a subclass of QWidget ... and btw you should add QWidget::paintEvent(event) outside of the ifmsrd0
Try it and you'll see why. I don't know why it won't let me, but I get these messages in the debug output. I'll add them to the questionDavid Ludwig
You can try to create your own widget and add the QTreeWidget to it, that should work, but I don't know if that's the best optionmsrd0

1 Answers

3
votes

You don't need paintEvent at all. Use just stylesheet (setStyleSheet() method):

QTreeWidget:focus{ border: 1px solid red}
QTreeWidget:!focus{}

Result:

enter image description here