![]() While the answer provided by works, it is incomplete: Now, resizing to allow less vertical space: This is how the widget appears as soon as it's shown (which is almost the same as the horizontal flow): HeightForWidth += item.sizeHint().height() + spaceYĬolumnWidth = max(columnWidth, item.sizeHint().width()) If nextY - spaceY > rect.bottom() and columnWidth > 0: NextY = y + item.sizeHint().height() + spaceY LineHeight = max(lineHeight, item.sizeHint().height()) If nextX - spaceX > rect.right() and lineHeight > 0: NextX = x + item.sizeHint().width() + spaceX SpaceY = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical) SpaceX = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal) LineHeight = columnWidth = heightForWidth = 0 That said, a partial solution is to still return a height for width, but position the widgets vertically instead of horizontally. ![]() Even when being very careful about their implementation, you might end up in recursive calls to size hints, policies etc. How do I properly implement a vertical flow layout?Īs you already found out, Qt layouts don't support widthForHeight, and, in general, these kinds of layouts are discouraged, mostly because they tend to behave erratically in complex situation with nested layouts and mixed widget size policies. However, the vertical layout does work properly if you manually resize the window. When using the vertical variant, this is not the case. When using the horizontal variant of the layout, the window is automatically appropriately sized to contain all the items.I implemented the latter two methods regardless, but I doubt they're ever actually getting called. QLayout has the hasHeightForWidth and heightForWidth methods, but not their inverses hasWidthForHeight and widthForHeight.This implementation has 2 (likely related) problems when handling vertical layouts: #flowLayout = FlowLayout(orientation=Qt.Horizontal)įlowLayout = FlowLayout(orientation=Qt.Vertical)įlowLayout.addWidget(QPushButton("Short"))įlowLayout.addWidget(QPushButton("Longer"))įlowLayout.addWidget(QPushButton("Different text"))įlowLayout.addWidget(QPushButton("More text"))įlowLayout.addWidget(QPushButton("Even longer button text")) Return y + offset - rect.y() if horizontal else x + offset - rect.x() Offset = max(offset, item.sizeHint().width()) Offset = max(offset, item.sizeHint().height()) tGeometry(QRect(QPoint(x, y), item.sizeHint())) If next - spaceY > rect.bottom() and offset > 0: Next = y + item.sizeHint().height() + spaceY Next = x + item.sizeHint().width() + spaceX If index >= 0 and index = 0 and index rect.right() and offset > 0: tContentsMargins(margin, margin, margin, margin) This is my current implementation: from PyQt5.QtWidgets import *ĭef _init_(self, orientation=Qt.Horizontal, parent=None, margin=0, spacing=-1): I'm trying to adapt this PyQt implementation of FlowLayout to allow vertical flow as well as horizontal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |