(PyQt) How to make QSpacerItem *ignore* certain widgets? Or add their height to the spacer?
-
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.
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))
-
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.
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))
-
@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
@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
-
@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