0

I have a QGroupBox containing multiple tables. At some point, the cummulative tables height exceed the screen height so they get squashed, and a scrollarea is added to each.

I want to have the tables keep their natural size, and make the entire QGroupBox scrollable.

Tried the following: PyQt: How to make widget scrollable. It did not help. The scrollarea does not appear, it seems the Qt logic always prefers to squash the tables instead of expanding the QGroupBox.

I can make it appear by adding something like myGroupBox.setMinimumHeight(3000). But this, other than being a ugly patch, also deforms the tables, making them stretch beyond their natural size, and leaving a blank space at the bottom.

How can I fix this? Thanks. I am using Python3.9, PyQt5 over Windows.

Code example:

import random

from PyQt5 import QtWidgets


class SomeTable(QtWidgets.QTableWidget):
    def __init__(self):
        super().__init__()
        header = ['A', 'B', 'C', 'D', 'E', 'F']
        self.setColumnCount(len(header))
        self.setHorizontalHeaderLabels(header)
        for i in range(5):
            self.insertRow(0)
        for i in range(5):
            for j in range(6):
                self.setItem(i, j, QtWidgets.QTableWidgetItem(str(random.randint(0, 100))))


class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        mygroupbox = QtWidgets.QGroupBox('this is my groupbox')
        myform = QtWidgets.QVBoxLayout()
        for i in range(10):
            myform.addWidget(SomeTable())
        mygroupbox.setLayout(myform)
        scroll = QtWidgets.QScrollArea()
        scroll.setWidget(mygroupbox)
        scroll.setWidgetResizable(True)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(scroll)


if __name__ == '__main__':
    app = QtWidgets.QApplication(['Test'])
    window = Window()
    window.showMaximized()
    app.exec_()
Elad Weiss
  • 3,662
  • 3
  • 22
  • 50
  • 2
    Well, that can be achieved, but it's also important to know if the contents of the tables are static or may change at runtime, and eventually how the tables should adjust themselves. Note that having nested scroll areas may not be ideal: the scrolling behavior may become extremely counterintuitive, whether it's done with the mouse wheel or by using the scroll bars (which are very close, horizontally), and you should probably consider a different overall layout. Also, you probably want to add the scroll area *into* the groupbox, not the opposite. – musicamante Aug 09 '23 at 21:52
  • @musicamante Thanks! 1. The tables are fixed size. 2. Indeed, my exact goal is to NOT have scrolling on each table table hence no nested scrolling. 3. Not sure what you mean by having the scroll inside, would be glad if you can explain in answer... I also don't really care about a groupbox, just need some scrollable container for all the tables, instead of each table getting a scroll. I just copied the groupbox example from the answer link posted in the question. – Elad Weiss Aug 12 '23 at 21:26

2 Answers2

0

I could not find a property that tells the tables to stay at their natural size.

So the solution for me was to calculate the table height and apply setMinimumHeight().

Elad Weiss
  • 3,662
  • 3
  • 22
  • 50
  • 1
    Please address the initial notes on my comment above so that we can eventually provide a valid answer depending on the context. – musicamante Aug 10 '23 at 23:23
0

You could use a combination of vertical size policy and size adjust policy:

    self.setSizePolicy(
        QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
    self.setSizeAdjustPolicy(self.AdjustToContents)
musicamante
  • 41,230
  • 6
  • 33
  • 58