Search code examples
pythonlayoutresizepyqt4adjustment

How adjust QHBoxLayout and QGraphicsView size dynamically with the mouse ? (Python, Pyqt4)


I'm struggling to find a solution to an issue. I'm trying to modify the space allocated to two graphicscene in a window. To be precise. I have a QHBoxLayout where I place two QGraphicsViews to plot something in them. Those two scenes are placed side by side (vertically). What I would like to do is click, with the mouse, between the two scenes to adjust the space of each view. If I move the mouse up, then the scene on the top get a smaller height and the bottom scene get a larger height.

By now I have the below code :

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from PyQt4 import QtGui
import sys

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()

        # General Window
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QHBoxLayout()
        # list group view side
        self.plotview = QtGui.QGroupBox("Group of views")
        self.layout_plotview = QtGui.QVBoxLayout()
        # views 
        self.mascenegrid = QtGui.QGraphicsView(self) 
        self.mascenelfp = QtGui.QGraphicsView(self)
        # add widjets
        self.layout_plotview.addWidget(self.mascenegrid)
        self.layout_plotview.addWidget(self.mascenelfp)
        # set layout 
        self.plotview.setLayout(self.layout_plotview)
        self.mainHBOX_param_scene.addWidget(self.plotview)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app) 
    ex.show()
    sys.exit(app.exec_())

So I get this result.

code result

What I would like to do is click and drag the grey bar in the middle up or down according to which view need more space. If somebody has a solution for that It will be nice. Have a nice day!


Solution

  • Use a QSplitter:

        # add widgets
        self.splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
        self.splitter.addWidget(self.mascenegrid)
        self.splitter.addWidget(self.mascenelfp)
        self.layout_plotview.addWidget(self.splitter)