[PySide] Can't override paintEvent on QTableWidget

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Tue Aug 30 09:37:31 CEST 2022



On 8/30/22 07:48, Frank Rueter wrote:
> Hi all,

Hey there,

> I am trying to override the paintEvent in a QTableWidget to draw a 
> custom progressbar into the table area until the table data is loaded. I 
> have done this with other widgets in the past but the table widget won't 
> let me do this.
> I am getting these errors:
> QWidget::paintEngine: Should no longer be called
> QPainter::begin: Paint device returned engine == 0, type: 1
> QPainter::setBrush: Painter not active
> 
> Here is a simple code snippet for testing:
> https://gist.github.com/frueter/a5b722926a847c291d1472a6fd145dbf 
> <https://gist.github.com/frueter/a5b722926a847c291d1472a6fd145dbf>
> 
> When I inherit from QWidget it works fine.
> Is this sort of thing not possible with a QTableWidget?

It's because QTableWidget is a subclass of QTableView (and that's one 
from QAbstractScrollArea),
and AFAIK you need to open the painter based on the base viewport.

https://doc.qt.io/qt-6/qabstractscrollarea.html#paintEvent

and there you can see the hint to use the viewport.

In your code,
just replace:

painter = QtGui.QPainter(self)
by
painter = QtGui.QPainter(self.viewport())

and it should work. At least locally, I can see the gradient.

> Cheers,
> frank
> 

Cheers

-- 
Dr. Cristián Maureira-Fredes
Senior R&D Manager

The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin

Geschäftsführer: Mika Pälsi,
Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B


More information about the PySide mailing list