[PyQt] setLayout for a QDockWidget

TP paratribulations at free.fr
Thu Jun 25 17:56:57 BST 2009


Try the small script below. I cannot manage to do setLayout for a
QDockWidget: QDockWidget.setWidget() works correctly, but not
QDockwidget.setLayout(), I obtain the following message:

QWidget::setLayout: Attempting to set QLayout "" on QDockWidget "", which
already has a layout

So, with some googling, I found:


It seems that there is a default layout inside a QDockWidget when it is
created. How to delete this default layout in PyQt, and then set as new
layout "vboxlayout" with the spin box in the following code?

Thanks a lot,


from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

app = QApplication( sys.argv )

spin = QSpinBox()
vboxlayout = QVBoxLayout( )
vboxlayout.addWidget( spin )

qmainwin = QMainWindow()

qdock = QDockWidget( )
existing_layout = qdock.layout()
print existing_layout
# how to delete the existing layout??
qdock.setLayout( vboxlayout )

qmainwin.addDockWidget( Qt.TopDockWidgetArea, qdock )


python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)

More information about the PyQt mailing list