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. How to get actual size of item in QGridLayout?
QtWS25 Last Chance

How to get actual size of item in QGridLayout?

Scheduled Pinned Locked Moved Unsolved Qt for Python
qgridlayoutqwidgetsizesizepolicysizehint
2 Posts 2 Posters 602 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.
  • H Offline
    H Offline
    Hai Anh Luu
    wrote on 12 Jan 2024, 03:43 last edited by Hai Anh Luu 1 Dec 2024, 03:44
    #1

    As the name of the topic suggests, how can I get the actual size of the widgets I add to QGridLayout. Looking at my code, when I print the dimensions, they are always equal to (640, 480).

    5b7d2d27-4620-4940-beca-bbaa4544a4ac-image.png

    I understand that sizeHint() is just a hint for the size of the widget, then, the size hint doesn't consider the size policy. In fact, the size policy tells how the size hint will be used to the layout manager. So how do I get the actual size of the item, can someone help me?

    import sys
    
    from PySide6.QtWidgets import QApplication, QWidget, QStackedWidget, QVBoxLayout, QTextEdit, QGridLayout, QMainWindow
    
    
    class NewGrid(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.load_ui()
    
        def load_ui(self):
            self.showMaximized()
            self.central_widget = QWidget()
            self.central_layout = QVBoxLayout()
    
            grid_layout = QGridLayout()
            grid_layout.setSpacing(1)
    
            for row in range(0, 4):
                for col in range(0, 4):
                    item = QWidget()
                    item.setStyleSheet('background-color: lightblue;')
                    grid_layout.addWidget(item, row, col)
    
            for row in range(0, 4):
                for col in range(0, 4):
                    item = grid_layout.itemAtPosition(row, col)
                    if item is not None:
                        stacked_widget = item.widget()
                        if isinstance(stacked_widget, QWidget):
                            item_size = stacked_widget.size()
                            print(f"Size of item at position ({row}, {col}): {item_size.width()} x {item_size.height()}")
    
            self.central_layout.addLayout(grid_layout)
    
            self.central_widget.setLayout(self.central_layout)
            self.setCentralWidget(self.central_widget)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = NewGrid()
        window.show()
        sys.exit(app.exec())
    
    

    This is my ui:
    7c8cff2a-780a-4ae0-8cdc-95140cb6a5ff-image.png

    C 1 Reply Last reply 12 Jan 2024, 04:36
    0
    • H Hai Anh Luu
      12 Jan 2024, 03:43

      As the name of the topic suggests, how can I get the actual size of the widgets I add to QGridLayout. Looking at my code, when I print the dimensions, they are always equal to (640, 480).

      5b7d2d27-4620-4940-beca-bbaa4544a4ac-image.png

      I understand that sizeHint() is just a hint for the size of the widget, then, the size hint doesn't consider the size policy. In fact, the size policy tells how the size hint will be used to the layout manager. So how do I get the actual size of the item, can someone help me?

      import sys
      
      from PySide6.QtWidgets import QApplication, QWidget, QStackedWidget, QVBoxLayout, QTextEdit, QGridLayout, QMainWindow
      
      
      class NewGrid(QMainWindow):
          def __init__(self):
              QMainWindow.__init__(self)
              self.load_ui()
      
          def load_ui(self):
              self.showMaximized()
              self.central_widget = QWidget()
              self.central_layout = QVBoxLayout()
      
              grid_layout = QGridLayout()
              grid_layout.setSpacing(1)
      
              for row in range(0, 4):
                  for col in range(0, 4):
                      item = QWidget()
                      item.setStyleSheet('background-color: lightblue;')
                      grid_layout.addWidget(item, row, col)
      
              for row in range(0, 4):
                  for col in range(0, 4):
                      item = grid_layout.itemAtPosition(row, col)
                      if item is not None:
                          stacked_widget = item.widget()
                          if isinstance(stacked_widget, QWidget):
                              item_size = stacked_widget.size()
                              print(f"Size of item at position ({row}, {col}): {item_size.width()} x {item_size.height()}")
      
              self.central_layout.addLayout(grid_layout)
      
              self.central_widget.setLayout(self.central_layout)
              self.setCentralWidget(self.central_widget)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          window = NewGrid()
          window.show()
          sys.exit(app.exec())
      
      

      This is my ui:
      7c8cff2a-780a-4ae0-8cdc-95140cb6a5ff-image.png

      C Offline
      C Offline
      ChrisW67
      wrote on 12 Jan 2024, 04:36 last edited by
      #2

      @Hai-Anh-Luu Welcome to the forum.

      Until the widget is displayed its size, and the size of any child widget in a layout, is unknown. In the widget class constructor^^ the widget is not yet shown so you get either the size hint from the widget concerned or some other value.

      ^^ That is when executing this line window = NewGrid()

      One way to see the size that is initially allocated is to do so in a slot connected to a zero-length (or very short) single-shot QTimer. This will fire when the event loop is reached, after the UI is shown. That is when app.exec() is running.

      1 Reply Last reply
      2
      • S SGaist moved this topic from General and Desktop on 12 Jan 2024, 21:20

      1/2

      12 Jan 2024, 03:43

      • Login

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