PDA

View Full Version : PyQt5 widget properties from .ui not showing in __init__ after load via uic.loadUi().



DDR
10th August 2018, 04:09
Hello all, this is a bit of a strange one. I have a subclass of QPushButton called simply Button, and I lay out a .ui file full of Buttons. I give them names and custom styles in QT Designer, but when examining these names and styles in Button's __init__() they're always blank. (This is after the call to super().__init__(…).)

Here's the relevant fragment.


class Button(QPushButton, TouchMarginPlugin):
def __init__(self, parent=None, showHitRects=False):
self.keepActiveLook = False
super().__init__(parent, showHitRects=showHitRects)
print('name', self.objectName(), self.styleSheet())

name and stylSheet are always ''. The name and styleSheet print correctly when I query them later, from a parent panel's __init__.

Does anyone know why this is happening, or how to retrieve the original name when the object's being __init__'d?

Thanks!
–DDR

d_stranz
10th August 2018, 17:15
QWidgets are not fully realized until they are "shown" (generally when their parent widget's showEvent() is executed). Visible properties (like size, position, etc.) are not valid until the layout has moved and sized child widgets prior to showing them. setupUi() merely creates the widget hierarchy, and it is the set of resize and other events prior to the showEvent() which fully fleshes it out.

So nothing you retrieve in __init__() (or in a C++ constructor) can be trusted to be valid.

DDR
30th August 2018, 02:26
Ahhh, thank you! This helps quite a bit. :)