Search code examples
pythonlayoutscrollpositionpyqt

Using exact positions for PushButtons and still be able to scroll through them


First of all, check out this screenshot to see what I'm talking about.
Part A is much longer than shown (hundreds of rows - each row consisting of a label, 2 squares, another label and a checkbox). Now I want to add a scroll bar for part A, so all the rows can be viewed, while part B stays where it is. It seems the solution to this is using layouts.
I haven't used them so far, simply because I didn't know they existed and I'm still not sure how they work exactly. I'm new to Python and PyQt (and programming, for that matter).
The problem with using layouts is that the squares in part A (which are PushButtons, btw) need to be positioned exactly (by pixels), instead of arranging them along a grid.

  • Is there a way to add a scroll bar for part A without having to use layouts?
    -or-
  • Is there a way to give the squares an exact position within a layout?

Solution

  • I found a solution without having to use a layout. I put everthing in part A into a QWidget and put the QWidget into a QScrollArea. There may be better solutions, but this worked. The scroll bar doesn't have to be added manually. It appears when scrollContent is larger than scrollArea.

    scrollArea = QtGui.QScrollArea(MainWindow)
    scrollContent = QtGui.QWidget()
    scrollArea.setGeometry(0, 0, 700, 600)
    scrollContent.setGeometry(0, 0, 680, 25000)
    # Here I add all the content. E.g. label = QtGui.QLabel(scrollContent)
    scrollArea.setWidget(scrollContent)
    

    I hope this is helpful to someone.