magu_ magu_ - 2 months ago 14
Python Question

Add multiple docked widgets

I try to add multiple tabified QDockWidgets but somehow I can only have to docked at the same time.

mwe code:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class DockWindow(QDockWidget):
def __init__(self, parent, name):
super().__init__(parent)

parent.addDockWidget(Qt.TopDockWidgetArea, self)
self.setWindowTitle(name)
child = parent.findChildren(DockWindow)

if len(child) > 1:
parent.tabifyDockWidget(self, child[0])
self.raise_()


app = QApplication(sys.argv)
main = QMainWindow()

for i in range(10):
DockWindow(main, str(i))


main.show()
sys.exit(qApp.exec_())


enter image description here

Answer Source

According to the docs:

void QMainWindow::tabifyDockWidget(QDockWidget * first, QDockWidget * second)

Moves second dock widget on top of first dock widget, creating a tabbed docked area in the main window.

From what is concluded in the first parameter must be the initial QDockWidget, if any child[0], second the new QDockWidget, in your case self. The problem is solved by changing:

parent.tabifyDockWidget(self, child[0])

to:

parent.tabifyDockWidget(child[0], self)

Screenshot:

enter image description here