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.
caThHaZ.png
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...
{
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
}
void TreeView::mousePressEvent (QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QRect textRect = QRect(checkboxX
+ 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
}
To copy to clipboard, switch view to plain text mode
{
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:
};
class PaintWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintWidget(const QRect &paintArea, QWidget *parent=nullptr)
: QWidget(parent), m_area(paintArea)
{
setGeometry(m_area);
QPalette pal = palette();
// set white background
pal.setColor(QPalette::Background, Qt::white);
setPalette(pal);
this->update();
}
~PaintWidget(){};
private:
QRect m_area;
};
To copy to clipboard, switch view to plain text mode
Bookmarks