Learn Python PyQt

QDockWidget PyQt Example

QDockWidget is a window control that can be docked within QMainWindow.

It can be kept in a floating state or attached to the main window as a child window at a specified location.

Book: Create Desktop Apps with Python PyQt5

QDockable example

The program below creates a QDockable with a list inside it

You can drag it around and put it on the left, right etc.

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DockDemo(QMainWindow):
    def __init__(self,parent=None):
        super(DockDemo, self).__init__(parent)
        layout=QHBoxLayout()
        bar=self.menuBar()
        file=bar.addMenu('File')
        file.addAction('New')
        file.addAction('Save')
        file.addAction('quit')

        self.items=QDockWidget('Dockable',self)

        self.listWidget=QListWidget()
        self.listWidget.addItem('Item1')
        self.listWidget.addItem('Item2')
        self.listWidget.addItem('Item3')
        self.listWidget.addItem('Item4')

        self.items.setWidget(self.listWidget)
        self.items.setFloating(False)
        self.setCentralWidget(QTextEdit())
        self.addDockWidget(Qt.RightDockWidgetArea,self.items)

        self.setLayout(layout)
        self.setWindowTitle('Dock')

if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=DockDemo()
    demo.show()
    sys.exit(app.exec_())
qdockable

Code Analysis

In this example, the top window is a QMainWindow object and the QTextEdit object is its central widget.

self.setCentralWidget(QTextEdit())

First create a dockable window Item

self.items=QDockWidget('Dockable',self)

Then, add the QListWidget object in the docked window

self.listWidget=QListWidget()
self.listWidget.addItem('Item1')
self.listWidget.addItem('Item2')
self.listWidget.addItem('Item3')
self.listWidget.addItem('Item4')

#Set up QWidget in window area, add list control
self.items.setWidget(self.listWidget)