0
當它動態添加到佈局時,我需要獲取小部件的真實寬度(),因爲我需要根據小部件的寬度對小部件進行繪製( )。但是下面的代碼不能像我預期的那樣工作:w.width()總是640,這顯然不是真正的寬度。 有什麼想法?小部件在添加到佈局時沒有獲得適當的大小
# -*- coding: utf-8 -*-
import os, sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWidget(QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.setupUI()
def setupUI(self):
self.mainLayout = QVBoxLayout(self)
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
layout = QVBoxLayout(self)
layout.addWidget(QLabel('label'))
def minimunSizeHint(self):
return QSize(30, 30)
def sizeHint(self):
return QSize(100, 100)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = MainWidget()
main.show()
l = QGridLayout()
l.addWidget(MyWidget(), 0, 0)
l.addWidget(MyWidget(), 0, 1)
l.addWidget(MyWidget(), 1, 0)
l.addWidget(MyWidget(), 1, 1)
main.mainLayout.addLayout(l)
w = l.itemAtPosition(0, 0).widget()
print w.width(), w.height()
sys.exit(app.exec_())