Grid layout for Dockarea Dock

209 views
Skip to first unread message

Hikmet Sezen

unread,
Oct 3, 2022, 4:05:50 AM10/3/22
to pyqtgraph
It's interesting that (as far as I know) Dockarea does not offer a grid layout option:
[0 1 2]
[3 4 5]
[6 7 8]

Here is an example of how I do it:

import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets
import numpy as np
from pyqtgraph.dockarea import DockArea, Dock
import sys

app = QtWidgets.QApplication(sys.argv)
win = QtWidgets.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(900, 800)
win.setWindowTitle('pyqtgraph example: dockarea')

row_num = 5
col_num = 3
num_graph = row_num * col_num
dock = [[], ] * num_graph
layout = [[], ] * num_graph

for num in range(num_graph):
    row, col = num // col_num, num % col_num
    dock[num] = Dock(f'Dock{num}')
    if num == 0:
        area.addDock(dock[num], 'left')
    elif col == 0:
        area.addDock(dock[num], 'bottom', dock[num - col_num])
    elif col != 0:
        area.addDock(dock[num], 'right', dock[num - 1])
        if row != 0:
            area.moveDock(dock[num-col_num], 'top', dock[num])
            area.moveDock(dock[num - col_num], 'right', dock[num - col_num-1])

for ids in range(num_graph):
    layout[ids] = pg.PlotWidget(title=f'Dock {ids} Plot')
    layout[ids].plot(np.random.normal(size=101) * 5**ids)
    layout[ids].plotItem.showGrid(x=True, y=True, alpha=0.5)
    dock[ids].addWidget(layout[ids])

    if ids > 0:
        layout[ids].setXLink(layout[ids-1])
    dock[ids].addWidget(layout[ids])

win.show()
sys.exit(app.exec_())
grid_dockarea.png
Reply all
Reply to author
Forward
0 new messages