'PyQt6/PySide6: QStackedLayout aligns the size of nested widgets
I created a QStacedLayout
and nested 2 widgets in it.
However, as I understand it, if one widget is larger than the other, then QStackedLayout
seems to be trying to equalize the size of the larger one to the smaller one.
Because of this, part of the content of the 2nd widget is "eaten" and appears only after I move the window.
How can this be fixed?
Large Widget Code:
from PySide6.QtWidgets import QFrame, QWidget, QLabel
from PySide6.QtWidgets import QVBoxLayout, QPushButton
from PySide6.QtCore import Qt
import tech
class AboutMe(QFrame):
def __init__(self):
super(AboutMe, self).__init__()
with open(tech.resource_path("ui\\assets\\styles\\AboutMe.qss"), "r", encoding="utf-8") as f:
self.style = f.read()
self.setStyleSheet(self.style)
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignTop)
self.layout.setContentsMargins(5, 5, 5, 5)
self.layout.setSpacing(3)
self.text = QLabel("text "*800)
self.text.setWordWrap(True)
self.layout.addWidget(self.text)
self.setLayout(self.layout)
class AboutPage(QWidget):
def __init__(self):
super(AboutPage, self).__init__()
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(3)
self.aboutme = AboutMe()
self.layout.addWidget(self.aboutme)
self.setLayout(self.layout)
Main Widget Code:
class MainWidget(QWidget):
def __init__(self, bot, parent):
super(MainWidget, self).__init__()
self.page_layout = QStackedLayout()
self.page_layout.setSpacing(0)
self.page_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout = QVBoxLayout()
self.main_layout.setSpacing(3)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_page = MainPage(bot)
self.about_page = AboutPage()
self.page_layout.addWidget(self.main_page)
self.page_layout.addWidget(self.about_page)
self.page_layout.setCurrentIndex(0)
self.bar = CustomBar(parent)
self.main_layout.addWidget(self.bar)
self.main_layout.addLayout(self.page_layout)
self.setLayout(self.main_layout)
Result: First Widget:
Second (larger):
When I switched to at least a pixel:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|