Hello,<br><br>I'm laying out part of the UI for my app using a QMainWindow with no central widget and 4 dock widgets. I'd like the dock widgets to be sized proportionally to each other but they seem to ignore the value returned by sizeHint(). Am I missing something?<br>
<br>- John<br><br>Here's a concrete example illustrating my problem:<br><br>import cPickle<br>import os.path<br>import sys<br>from PyQt4 import QtGui, QtCore<br><br>class _ViewContainer(QtGui.QMainWindow):<br> """ This class is a container for a view that allows an initial size<br>
(specified as a tuple) to be set.<br> """<br> def __init__(self, size, main_window):<br> QtGui.QMainWindow.__init__(self)<br> # Save the size and main window.<br> self._width, self._height = size<br>
self._main_window = main_window<br> # set a minimum size to quiet Qt<br> self.setMinimumSize(100, 100)<br> self.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)<br><br> def minimumSizeHint(self):<br>
try:<br> return self.centralWidget().minimumSizeHint()<br> except AttributeError:<br> return super(_ViewContainer, self).minimumSizeHint()<br><br> def sizeHint(self):<br> sh = self.centralWidget().sizeHint()<br>
if self._width > 0:<br> if self._width > 1:<br> w = self._width<br> else:<br> w = self._main_window.width() * self._width<br> sh.setWidth(int(w))<br>
if self._height > 0:<br> if self._height > 1:<br> h = self._height<br> else:<br> h = self._main_window.height() * self._height<br> sh.setHeight(int(h))<br>
return sh<br><br><br>class Ui_MainWindow(object):<br> def setupUi(self, MainWindow):<br> MainWindow.setObjectName("MainWindow")<br> MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,800,600).size()).expandedTo(MainWindow.minimumSizeHint()))<br>
<br> self.menubar = QtGui.QMenuBar(MainWindow)<br> self.menubar.setGeometry(QtCore.QRect(0,0,800,21))<br> self.menubar.setObjectName("menubar")<br> MainWindow.setMenuBar(self.menubar)<br>
<br> self.statusbar = QtGui.QStatusBar(MainWindow)<br> self.statusbar.setObjectName("statusbar")<br> MainWindow.setStatusBar(self.statusbar)<br><br> self.retranslateUi(MainWindow)<br>
QtCore.QMetaObject.connectSlotsByName(MainWindow)<br><br> def retranslateUi(self, MainWindow):<br> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow",<br>
None, QtGui.QApplication.UnicodeUTF8))<br><br><br>class MainWindow(QtGui.QMainWindow, Ui_MainWindow):<br> def __init__(self, parent=None):<br> super(MainWindow, self).__init__(parent)<br>
self.ui = Ui_MainWindow()<br> self.ui.setupUi(self) <br> self.createDockWindows()<br> <br> def closeEvent(self, event):<br> self.saveLayout()<br> super(MainWindow, self).closeEvent(event)<br>
<br> def createDockWidget(self, name, size):<br> dock = QtGui.QDockWidget(name, self)<br> child = QtGui.QLabel(name)<br> vc = _ViewContainer(size, self)<br> dock.setWidget(vc)<br> dock.setObjectName(name)<br>
self._qt4_adjust_widget_layout(child)<br> dock.widget().setCentralWidget(child)<br> <br> return dock;<br><br> def createDockWindows(self):<br> dock1 = self.createDockWidget("Dock1", (0.5, 0.75))<br>
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, dock1)<br> dock2 = self.createDockWidget("Dock2", (0.5, 0.75))<br> self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, dock2)<br> dock3 = self.createDockWidget("Dock3", (0.5, 0.25))<br>
self.splitDockWidget(dock2, dock3, QtCore.Qt.Horizontal)<br> dock4 = self.createDockWidget("Dock4", (0.5, 0.25))<br> self.splitDockWidget(dock1, dock4, QtCore.Qt.Horizontal)<br> <br> def saveLayout(self):<br>
filename = './layout.dat'<br> f = file(filename, 'w')<br> state = str(self.saveState())<br> cPickle.dump((0,state), f)<br> f.close()<br> <br> def restoreLayout(self):<br>
filename = './layout.dat'<br> if os.path.exists(filename):<br> f = file(filename, 'r')<br> vers, state = cPickle.load(f)<br> f.close()<br> self.restoreState(state)<br>
<br> @staticmethod<br> def _qt4_adjust_widget_layout(w):<br> lay = w.layout()<br> if lay is not None:<br> lay.setAlignment(QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)<br> lay.setContentsMargins(0, 0, 0, 0)<br>
<br><br>def main(): <br> app = QtGui.QApplication(sys.argv) <br> mainwindow = MainWindow()<br> mainwindow.restoreLayout()<br> mainwindow.show()<br> sys.exit(app.exec_())<br><br>if __name__ == '__main__': <br>
main()<br>