1 Attachment(s)
Trying to overlay widget on a QTreeView item
I'm trying to align a rectangle over the text portion of a QTreeView item. Calculating the rectangle is a bit complicated because the checkbox indents add up depending on the number of parents, etc.
So to debug this I figured I'd just draw a physical rectangle over the math prediction. However, I'm unable to render a QWidget on top of a tree view item. It never shows up. Any ideas what's wrong or how to overlay a visual over a QTreeView item?
Basically, I want to see something like this, so I can tell where my textRect calc is off.
Attachment 13428
My attempt below. I'm trying to draw the rectangle in the treeview's mousePressEvent. But, no matter what I do, the overlay widget never appears...
Code:
{
if (event->button() == Qt::LeftButton)
{
+ style
()->pixelMetric
(QStyle::PM_IndicatorWidth) + style
()->pixelMetric
(QStyle::PM_CheckBoxLabelSpacing) + 30 // color chip and padding
+ 3, // little padding
vrect.y(),
treeItemWidth,
vrect.height());
// Overlay colored widget to see the rectangle
PaintWidget *overlay = new PaintWidget(textRect, this);
overlay->repaint(); // try to force the repaint
}
Code:
{
Q_OBJECT
public:
explicit PaintWidget
(const QRect &paintArea,
QWidget *parent
=nullptr
) : QWidget(parent
), m_area
(paintArea
) {
setGeometry(m_area);
// set white background
pal.
setColor(QPalette::Background, Qt
::white);
setPalette(pal);
this->update();
}
~PaintWidget(){};
private:
};
Re: Trying to overlay widget on a QTreeView item
You cannot do any on-screen painting anywhere except in a paintEvent().
I think you can probably accomplish what you want with a QStyledItemDelegate. All of the geometry calculations are made for you. Take a look at the Star Delegate example.
You might also be able to do this by adding additional cases to your QAbstractItemModel::data() method to handle the Qt::BackgroundRole.
Re: Trying to overlay widget on a QTreeView item
Thanks d_stranz, after some more attempts here's the working code.
Code:
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
{
Q_OBJECT
public:
explicit PaintWidget
(const QRect &paintArea,
QWidget *parent
=nullptr
) : QWidget(parent
), m_area
(paintArea
) {
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
}
~PaintWidget(){};
protected:
{
QPainter(this).
fillRect(m_area,
{255,
0,
0,
100});
}
private:
};
And the calling function I resize the overlay widget to be the entire size of the treeview, but only paint the rectangle of interest.
Code:
PaintWidget *overlay = new PaintWidget(textRect, this);
overlay->resize(this->width(), this->height());
overlay->show();