2

I'm trying to set the width of a QScrollArea in PyQt5, but the only thing I found is setting the width through the

scroll.setFixedWidth(360)  

function.

The issue is when I put the scroll bar area in a QSplitter

self.splitter2 = QSplitter(Qt.Horizontal) 
self.splitter2.addWidget(scroll)

I cannot resize the scroll bar area width with the mouse any more.

What I would like to do is set the width of the scrollbar area (100 for instance) and be able to modify it afterwards when I click and drag the middle line of the QSplitter. For now it is one (set the width of the scrollbar area) or the other (modify the width with the mouse) - but not both at the same time.

Here's a minimal example:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys


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

        self.centralWidget= QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX = QHBoxLayout()
        self.setFixedWidth(200)
        self.setFixedHeight(200)

        label1 = QLabel('blablabalblablabalblablabalblablabalblablabalblablabalblablabalblablabalblablabal')
        label2 = QLabel('blablabal again')

        scroll = QScrollArea()
        scroll.setFixedWidth(100)
        scroll.setWidget(label1)
        scroll.setWidgetResizable(True)

        self.splitter2 = QSplitter(Qt.Horizontal)
        self.splitter2.addWidget(scroll)
        self.splitter2.addWidget(label2)

        self.mainHBOX.addWidget(self.splitter2)
        self.centralWidget.setLayout(self.mainHBOX)

def main():
    app = QApplication(sys.argv)
    ex = Viewer(app)
    ex.setWindowTitle('window')
    # ex.showMaximized()
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
ymmx
  • 4,769
  • 5
  • 32
  • 64
  • Yes its possible but you have to keep in mind that unfixed means that its size IS going to change -- that said to control its size beyond fixing you need to work with minsize and maxsize for that object – Dennis Jensen Jul 23 '19 at 13:34
  • When I'm trying to use `setMinimumWidth`, `setMaximumWidth` and `setMidLineWidth`, I'm not able to set the initial width to the value I want – ymmx Jul 23 '19 at 13:42
  • Okay first off you need to be a lot more precise about what you mean about not able to resize the scrollbar -- if you remove the `self.setFixedWidth(200)` line your QSplitter can be resized which resizes your scrollbar? So what is it that you are trying to do that you cannot do in more precise terms – Dennis Jensen Jul 23 '19 at 13:49
  • What I would like to do is set the width of the scrollbar area (100 for instance) and be able to modify it after when I click and drag the middle line of the QSplitter. For now it is one (set the width of the scrollbar area) or the other (modify the width with the mouse) but not both at the same time. – ymmx Jul 23 '19 at 13:59
  • It still might be not clear enough ... – ymmx Jul 23 '19 at 14:00
  • Okay so what you are actually wanting to do is set the left hand side of you QSplitter to a particular size when you start and then adjust it afterwards if that is the case then look into QSplitter it has parameters for setting its initial sides widths or proportional widths If you are not aware of it here is a link that might be quite useful https://doc.qt.io/qt-5/classes.html it covers all the Classes and Inherited from Classes its a reference tool I use a lot when working with pyqt5 – Dennis Jensen Jul 23 '19 at 14:01

1 Answers1

2

You should set the initial position of the splitter handle rather than setting the width of its child widgets. This can be done using QSplitter.setSizes:

leftwidth = 100
self.splitter2.setSizes([leftwidth, self.width() - leftwidth])

But note that the splitter won't allow moving its handle to a position less than the minimum width of a child widget. The default minimum width for a scroll-area is 100. So in the above example, setting leftwidth = 50 wouldn't work properly unless you explicitly reset the minimum width of the scroll-area accordingly.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336