1

I am trying to create an application using pyqt python.Application's Main window is filled with many dock widgets, some dock widgets are just used to list certain string data. These widgets are occupying more space.

enter image description here

the drawer to the left in the image is my interest. That drawer opens on mouse click.

enter image description here

Is there any way I could hide these widgets to the side of main window and open when mouse is hovered over it?

or if you know any pyqt UI element which could do this. please suggest.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
vaasu varma
  • 85
  • 1
  • 7
  • Are you using the DockWidgets in conjunction with QMainWindow to do this? Because if you are not then I would suggest you look into that as they do not occupy the Central Widget area and can even be detached and made a separate window by simply dragging/dropping – Dennis Jensen Nov 06 '19 at 16:59
  • yes , I am using them in conjunction with Qmainwindow. But I still want that non-important string data to be hidden somewhere to the side and I should be able to see whenever i want on mouse hover over it. – vaasu varma Nov 06 '19 at 17:06
  • if you know any UI element which could do this, please suggest – vaasu varma Nov 06 '19 at 17:07
  • @vaasuvarma you could show an image, sketches, etc. of what you want to understand you better since what you point out can be interpreted in many ways. – eyllanesc Nov 06 '19 at 17:11
  • see https://stackoverflow.com/questions/52615115/how-to-create-collapsible-box-in-pyqt#52617714 – S. Nick Nov 06 '19 at 17:13
  • reference images are added. Thank you – vaasu varma Nov 06 '19 at 17:46

1 Answers1

0

The logic is to detect the desired event and show the widget, in the following example the click on the QGraphicsView is detected and then the QDockWidget that was initially hidden is shown.

from PyQt5 import QtCore, QtGui, QtWidgets


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

        self.dock_widget = QtWidgets.QDockWidget()

        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dock_widget)

        list_widgets = QtWidgets.QListWidget()
        list_widgets.addItems(["item{}".format(i) for i in range(100)])

        self.dock_widget.setWidget(list_widgets)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.view = QtWidgets.QGraphicsView(self.scene)
        it = self.scene.addRect(QtCore.QRectF(0, 0, 300, 400))
        it.setBrush(QtGui.QColor("white"))

        self.view.viewport().installEventFilter(self)

        self.setCentralWidget(self.view)

        self.dock_widget.hide()

        self.resize(640, 480)

        for i in range(4):
            self.menuBar().addAction("Action{}".format(i))

    def eventFilter(self, obj, event):
        if obj is self.view.viewport():
            if event.type() == QtCore.QEvent.MouseButtonPress:
                self.dock_widget.show()

        return super().eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241