Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. (PyQt) How to make QSpacerItem *ignore* certain widgets? Or add their height to the spacer?
Forum Updated to NodeBB v4.3 + New Features

(PyQt) How to make QSpacerItem *ignore* certain widgets? Or add their height to the spacer?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.0k Views 2 Watching
  • 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.
  • W Offline
    W Offline
    wad11656
    wrote on last edited by
    #1

    I have a breadcrumb in the top-left and buttons horizontally and vertically centered below it. I want the buttons to be vertically-centered RELATIVE TO THE WINDOW SIZE, and ignore all other widgets in the window (i.e. the breadcrumb). Right now, the top spacer seems to be positioned BELOW the breadcrumb. I need to somehow accommodate for the height of the breadcrumb, either by making my spacers ignore it, or adding its height to my spacer calculations.

    Any ideas? I'm learning ChatGPT is terrible with GUI, and some StackOverflow answers I found for calculating the height of a widget are hard to adapt to my needs.

    946aceb4-88fe-42a8-a72f-56794510526e-image.png

    def __init__(self, collection_name=""):
            super().__init__()
            self.collection_name = collection_name
            self.datasourceButtons = []
            self.setWindowTitle('Configure Connections')
            self.setGeometry(100, 100, 800, 450)
            self.initUI()
            self.refreshDatasources()
    
        def initUI(self):
            self.centralWidget = QWidget(self)
            self.setCentralWidget(self.centralWidget)
    
            # Main vertical layout
            self.mainLayout = QVBoxLayout(self.centralWidget)
    
            # Breadcrumb at the top
            self.breadcrumb = QLabel(f'<a href="#">Collections</a> > {self.collection_name}')
            self.breadcrumb.setAlignment(Qt.AlignLeft | Qt.AlignTop)
            self.breadcrumb.setOpenExternalLinks(False)
            self.breadcrumb.linkActivated.connect(self.onCollectionsLinkClicked)
            self.mainLayout.addWidget(self.breadcrumb)
    
            # Spacer at the top (expands to push everything else to the center)
            self.mainLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
    
            # Button row layout (horizontal)
            self.buttonsLayout = QHBoxLayout()
            self.mainLayout.addLayout(self.buttonsLayout)
    
            # Initialize the "New Datasource" button
            base_dir = os.path.dirname(__file__)
            new_collection_image_path = os.path.join(base_dir, "resources", "new datasource.png")
            self.newCollectionButton = QPushButton()
            self.newCollectionButton.setIcon(QIcon(new_collection_image_path))
            self.newCollectionButton.setIconSize(QSize(200, 200))
            self.newCollectionButton.setMaximumSize(QSize(200, 200))
            self.newCollectionButton.clicked.connect(self.onOpenConnectionWindow)
            self.buttonsLayout.addWidget(self.newCollectionButton)
    
            # Spacer at the bottom (also expands)
            self.mainLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
    
    jsulmJ 1 Reply Last reply
    0
    • W wad11656

      I have a breadcrumb in the top-left and buttons horizontally and vertically centered below it. I want the buttons to be vertically-centered RELATIVE TO THE WINDOW SIZE, and ignore all other widgets in the window (i.e. the breadcrumb). Right now, the top spacer seems to be positioned BELOW the breadcrumb. I need to somehow accommodate for the height of the breadcrumb, either by making my spacers ignore it, or adding its height to my spacer calculations.

      Any ideas? I'm learning ChatGPT is terrible with GUI, and some StackOverflow answers I found for calculating the height of a widget are hard to adapt to my needs.

      946aceb4-88fe-42a8-a72f-56794510526e-image.png

      def __init__(self, collection_name=""):
              super().__init__()
              self.collection_name = collection_name
              self.datasourceButtons = []
              self.setWindowTitle('Configure Connections')
              self.setGeometry(100, 100, 800, 450)
              self.initUI()
              self.refreshDatasources()
      
          def initUI(self):
              self.centralWidget = QWidget(self)
              self.setCentralWidget(self.centralWidget)
      
              # Main vertical layout
              self.mainLayout = QVBoxLayout(self.centralWidget)
      
              # Breadcrumb at the top
              self.breadcrumb = QLabel(f'<a href="#">Collections</a> > {self.collection_name}')
              self.breadcrumb.setAlignment(Qt.AlignLeft | Qt.AlignTop)
              self.breadcrumb.setOpenExternalLinks(False)
              self.breadcrumb.linkActivated.connect(self.onCollectionsLinkClicked)
              self.mainLayout.addWidget(self.breadcrumb)
      
              # Spacer at the top (expands to push everything else to the center)
              self.mainLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
      
              # Button row layout (horizontal)
              self.buttonsLayout = QHBoxLayout()
              self.mainLayout.addLayout(self.buttonsLayout)
      
              # Initialize the "New Datasource" button
              base_dir = os.path.dirname(__file__)
              new_collection_image_path = os.path.join(base_dir, "resources", "new datasource.png")
              self.newCollectionButton = QPushButton()
              self.newCollectionButton.setIcon(QIcon(new_collection_image_path))
              self.newCollectionButton.setIconSize(QSize(200, 200))
              self.newCollectionButton.setMaximumSize(QSize(200, 200))
              self.newCollectionButton.clicked.connect(self.onOpenConnectionWindow)
              self.buttonsLayout.addWidget(self.newCollectionButton)
      
              # Spacer at the bottom (also expands)
              self.mainLayout.addSpacerItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @wad11656 You could use a vertical layout with 3 parts:

      • At the top the breadcrumb
      • At the bottom an empty widget with fixed height set to the same height as breadcrumb
      • In the middle your buttons

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      1
      • jsulmJ jsulm

        @wad11656 You could use a vertical layout with 3 parts:

        • At the top the breadcrumb
        • At the bottom an empty widget with fixed height set to the same height as breadcrumb
        • In the middle your buttons
        W Offline
        W Offline
        wad11656
        wrote on last edited by
        #3

        @jsulm i actually thought of that at some point, but was a bit reluctant to apply it because it felt hacky (and once I finally thought of it, I felt stupid for having wasted an entire afternoon before thinking of it...so some pride/stubbornness involved), but was also curious if there was a more "correct" way. But i'm sure it would work, thank you for the reply

        SGaistS 1 Reply Last reply
        0
        • W wad11656

          @jsulm i actually thought of that at some point, but was a bit reluctant to apply it because it felt hacky (and once I finally thought of it, I felt stupid for having wasted an entire afternoon before thinking of it...so some pride/stubbornness involved), but was also curious if there was a more "correct" way. But i'm sure it would work, thank you for the reply

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @wad11656 hi and welcome to devnet,

          Another option is to not put the breadcrumb in a layout and position it where you want it. The drawback is that you then also have to manage the resizing yourself.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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