Reputation: 390
I created only one frame and want to caculate it's height. I can get 30(default) in constructor function. but later. I can get 259(correct value) . can anyone explain me this? and I want to get correct value in init function.
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.frame = QWidget(self)
vbox = QVBoxLayout(self)
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
vbox.addWidget(QLabel('11111111'))
self.frame.setLayout(vbox)
self.setCentralWidget(self.frame)
print(self.frame.height())
def enterEvent(self,event):
print(self.frame.height())
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Upvotes: 0
Views: 42
Reputation: 244311
The geometry of the widgets is only updated when necessary by optimization. In the case of the constructor it is not visible yet, so the update is not necessary.
If you want to get the size then you must invoke it after using the show() method:
self.setCentralWidget(self.frame)
self.show()
print(self.frame.height())
Another possibility is to use the sizeHint that returns the default size that the widget should have based on its content (the QLabels)
self.setCentralWidget(self.frame)
print(self.frame.sizeHint().height())
Upvotes: 2