0

UDPATE: I may have realized that QDockWidget isn't the way to go. I have posted a new question here: PyQt: Is it possible to drag/drop QWidgets in a QGridLayout to rearrange them?

Original question:

I am trying to make a scrollable area containing two columns of several dockable widgets. Furthermore, I would like the scrollable dock area to be placed to the right in a QHBoxLayout. Right now I have a single column of QDockWidgets with no scrolling, and a QTextEdit as my central widget.

I would like help to integrate the following:

  1. Add the possibility for another column of QDockWidgets
  2. Disable the function to undock to a new window. I would still like to rearrange the docks
  3. Add the scrollarea and scrollbar to the dockarea
  4. I would like the layout to be a QHBoxLayout with the dockarea to the right.

The image illustrates what I have and what I want. Hope you can help!

enter image description here

from PyQt5 import QtCore, QtWidgets, QtGui

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import random

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)


        self.textEdit = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit)

        # First dock
        dock1 = QtWidgets.QDockWidget("dock 1", self)
        dock1.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)
        dock1.setFloating(False)

        self.figure1 = Figure()  # a figure to plot on
        self.canvas1 = FigureCanvas(self.figure1)
        self.ax1 = self.figure1.add_subplot(111)  # create an axis
        data = [random.random() for i in range(10)]
        self.ax1.plot(data, '*-')  # plot data
        self.canvas1.draw()  # refresh canvas

        dock1.setWidget(self.canvas1)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock1)

        # Second dock
        dock2 = QtWidgets.QDockWidget("dock 2", self)
        dock2.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)

        self.figure2 = Figure()  # a figure to plot on
        self.canvas2 = FigureCanvas(self.figure2)
        self.ax2 = self.figure2.add_subplot(111)  # create an axis
        data = [random.random() for i in range(10)]
        self.ax2.plot(data, '*-')  # plot data
        self.canvas2.draw()  # refresh canvas

        dock2.setWidget(self.canvas2)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock2)


# import dockwidgets_rc

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frederik Petri
  • 451
  • 8
  • 24
  • Adding another column of docks is possible by setting the [`dockNestingEnabled()`](https://doc.qt.io/qt-5/qmainwindow.html#dockNestingEnabled-prop) property, although you need to add them carefully using [`splitDockWidget()`](https://doc.qt.io/qt-5/qmainwindow.html#splitDockWidget) also. On the other side, you can't add a scrollbar, because that's not how dock widgets are intended to use. I'm afraid that the only solution would be to implement your own dock widgets *and* dock area, especially if you want them to be floatable. – musicamante Mar 30 '20 at 15:59
  • Btw, I just watched the second image more carefully, it seems that you don't want the floating functionality, so I'm wondering: do you *really* need dock widgets? – musicamante Mar 30 '20 at 19:51
  • Thanks for the comment @musicamante. The dockNestingEnabled answer my question one. For your question: No maybe I really dont need dock widgets. But I would like the function to rearrange widgets by dragging them to new places. Maybe I could use QGridLayout and add the drag/drop functionality? Do you know if this could work? I am new to PyQt, so I am not sure myself :) – Frederik Petri Mar 31 '20 at 06:16
  • Update: I have asked a new question, asking if it is possible to use a QGridLayout instead: https://stackoverflow.com/questions/60945099/pyqt-is-it-possible-to-drag-drop-qwidgets-in-a-qgridlayout-to-rearrange-them – Frederik Petri Mar 31 '20 at 07:08

0 Answers0