Thanks d_stranz, after some more attempts here's the working code.
Qt Code:
  1. #include <QPaintEvent>
  2. #include <QPainter>
  3. #include <QDebug>
  4. class PaintWidget : public QWidget
  5. {
  6. Q_OBJECT
  7.  
  8. public:
  9. explicit PaintWidget(const QRect &paintArea, QWidget *parent=nullptr)
  10. : QWidget(parent), m_area(paintArea)
  11. {
  12. setAttribute(Qt::WA_NoSystemBackground);
  13. setAttribute(Qt::WA_TransparentForMouseEvents);
  14. }
  15. ~PaintWidget(){};
  16.  
  17. protected:
  18. void paintEvent(QPaintEvent */*event*/) override
  19. {
  20. QPainter(this).fillRect(m_area, {255, 0, 0, 100});
  21. }
  22. private:
  23. QRect m_area;
  24. };
To copy to clipboard, switch view to plain text mode 

And the calling function I resize the overlay widget to be the entire size of the treeview, but only paint the rectangle of interest.
Qt Code:
  1. PaintWidget *overlay = new PaintWidget(textRect, this);
  2. overlay->resize(this->width(), this->height());
  3. overlay->show();
To copy to clipboard, switch view to plain text mode