Search code examples
python-2.7pyqt4qdockwidgetdockable

how to add QDockWidget to QFrame in PyQt4


How do I add QDockWidget to QFrame ? since QFrame does not have addDockWidget !!!

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

app = QApplication( sys.argv )

qmainwin = QFrame()
#qmainwin.setWindowFlags(Qt.FramelessWindowHint)
s = QWidget()
vboxlayout = QVBoxLayout( )

spin1 = QSpinBox()
vboxlayout.addWidget( spin1 )

spin2 = QSpinBox()
vboxlayout.addWidget( spin2 )
s.setLayout( vboxlayout )

qdock = QDockWidget( )
qdock.setWindowFlags(Qt.FramelessWindowHint)
qdock.setWidget( s )
qmainwin.addDockWidget( Qt.TopDockWidgetArea, qdock )
qmainwin.show()

app.exec_()

Solution

  • How about this? I added the QDockWidget to an QLayout and then set the layout of the QFrame.

    import sys
    from PyQt4 import QtGui, QtCore
    
    class Example(QtGui.QWidget):
    
        def __init__(self):
            super(Example, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            frame = QtGui.QFrame()
            frame.setFrameStyle(QtGui.QFrame.Panel |
                    QtGui.QFrame.Plain)
    
            label = QtGui.QLabel('This is random text')
    
            dockWidget = QtGui.QDockWidget('Main', self)
            # set the widget to non-movable, non-floatable and non-closable
            dockWidget.setFeatures(dockWidget.NoDockWidgetFeatures)
            dockWidget.setWidget(label)
    
            # add the QDockWidget to the QLayout
            hbox = QtGui.QHBoxLayout()
            hbox.addWidget(dockWidget)
    
            # set the layout of the QFrame
            frame.setLayout(hbox)
    
            # create another QLayout to add QFrame
            vbox = QtGui.QVBoxLayout()
            vbox.addStretch(1)
            vbox.addWidget(frame)
    
            self.setLayout(vbox)
    
            self.setGeometry(300, 300, 500, 400)
            self.setWindowTitle('Test')
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()