Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt and resizing windows when widgets shrink - minimal example included
Forum Updated to NodeBB v4.3 + New Features

PyQt and resizing windows when widgets shrink - minimal example included

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 612 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikstravaganza
    wrote on 2 Apr 2025, 07:03 last edited by
    #1

    Hi Qt forum experts - I'm very new to development in PyQt and am coming across what is probably a very simple widget/layout sizing issue that I just can't wrap my head around. I'm doing this all in code to better understand what's going on behind the scenes before trying to dip into PyQt Designer. (I'm using version 6.8.1)

    So far my application (basic CRUD with SQLite backend) uses a QMainWindow subclass, two "sections" subclassing QWidget, and several QAbstractTableModel implementations. I've put a minimal reproducible example in the link at the bottom of my post. Some widgets are replaced with dummy items to keep better parity with the actual layout of my application, but virtually all details / functionality are removed.

    In the example, my bottom section includes a QComboBox that is present in my actual application, but here I've repurposed it to simulating driving state changes. Also in the bottom section of my layout, there is a QTableView() associated to a QAbstractTableModel. However, the catch is that I want the QTableView() to never have any "empty rows" and resize its height to fit its data for a cleaner look.

    In this example code, changing the state (by changing the QComboBox index) shifts the table model from 2 rows to 10 rows, which expands the main window. (The size of the table is capped at 6 rows high to avoid the table expanding indefinitely, with others visible via scrolling.) Changing the state again reduces the model, and table, to two rows - but the window stays at its expanded size and other widgets expand to take up the size. The desired behavior is to shrink back to only the size needed for 2 rows in the table.

    I have tried what feels like endless combinations of setting sizing policies and stretch factors on widgets, setting constraints on layouts, calling .update() and .updateGeometry(), etc. to let the layout manager processes handle this automatically to no avail. Instead, I generally get the behavior as shown in the sample code: the table reduces in size, but other widgets expand to take up the space. I assume I could bludgeon the layout into obeying by manually setting its size parameters in some way, but this feels like it should be doable with layout native tools. (I also don't understand the sizing methods well enough to guess at how to do that.)

    https://pastebin.com/cFeMAVMa

    Two questions for you experts:

    • How should I implement the desired behavior?
    • Can anyone recommend a more visual, comprehensive guide to understanding layout management (e.g. something that's not https://doc.qt.io/qtforpython-6/overviews/qtwidgets-layout.html#layout-management)? Ideally something that dumbs it down for people like me: "Here's the attributes that you can change / methods you can call on a widget that affects its placement and sizing, and how they work (with pictures). Here's what you can use on a layout that affects placement of its child widgets, and how that works (with pictures)." (At this point I've gotten lost in the official documentation as all the various layout methods and attributes are distributed across the class pages, and it's just abstract enough that it's a little over my head.)
    1 Reply Last reply
    0
    • B Offline
      B Offline
      berry36
      wrote on 9 Apr 2025, 00:28 last edited by
      #2

      Hello, I haven't taken a complete look at your example, but here's what worked for me in other programs, you can try these out first:

      Subclassing QTableView to override its sizeHint() with the height of 2 columns. Then set its vertical size policy to Fixed.

      clss MyTableView(QTableView):
          def __init__(self):
              super().__init__()
              self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
      
          def sizeHint(self):
              p = super().sizeHint()
              return QSize(p.width(), 60)
      
      # In your code, replace QTableView with MyTableView
      

      Note: setSizePolicy does not require subclassing to set.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        berry36
        wrote on 9 Apr 2025, 00:43 last edited by berry36 4 Sept 2025, 00:44
        #3

        Sorry, I misread your needs, if it is not the table but other widgets expanding, I suggest that you wrap the other widgets in a subclassed QWidget with fixed vertical sizeHint and fixed vertical sizePolicy like above.

        Or try setting other widgets' sizePolicy to Preferred, if they were Expanding.

        I will take a more thorough look later.

        In my experience, it is hard to control sizes like this by only using layout methods, even with Qt Designer. If you stumble on this, do not spend too much time.

        1 Reply Last reply
        0

        1/3

        2 Apr 2025, 07:03

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved