Skip to content

FlowLayout: fixed margin calculation to regard all sides #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions examples/layouts/flowlayout.py
Original file line number Diff line number Diff line change
@@ -24,8 +24,7 @@ class FlowLayout(QtGui.QLayout):
def __init__(self, parent=None, margin=0, spacing=-1):
super(FlowLayout, self).__init__(parent)

if parent is not None:
self.setMargin(margin)
self.setContentsMargins(margin, margin, margin, margin)

self.setSpacing(spacing)

@@ -77,21 +76,22 @@ def minimumSize(self):
for item in self.itemList:
size = size.expandedTo(item.minimumSize())

size += QtCore.QSize(2 * self.contentsMargins().top(), 2 * self.contentsMargins().top())
size += QtCore.QSize(self.contentsMargins().top() + self.contentsMargins().bottom(),
self.contentsMargins().left() + self.contentsMargins().right())
return size

def doLayout(self, rect, testOnly):
x = rect.x()
y = rect.y()
x = rect.x() + self.contentsMargins().left()
y = rect.y() + self.contentsMargins().top()
lineHeight = 0

for item in self.itemList:
wid = item.widget()
spaceX = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton, QtGui.QSizePolicy.PushButton, QtCore.Qt.Horizontal)
spaceY = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton, QtGui.QSizePolicy.PushButton, QtCore.Qt.Vertical)
nextX = x + item.sizeHint().width() + spaceX
if nextX - spaceX > rect.right() and lineHeight > 0:
x = rect.x()
if nextX - spaceX > rect.right() - self.contentsMargins().right() and lineHeight > 0:
x = rect.x() + self.contentsMargins().left()
y = y + lineHeight + spaceY
nextX = x + item.sizeHint().width() + spaceX
lineHeight = 0
@@ -102,13 +102,11 @@ def doLayout(self, rect, testOnly):
x = nextX
lineHeight = max(lineHeight, item.sizeHint().height())

return y + lineHeight - rect.y()
return y + lineHeight - rect.y() + self.contentsMargins().bottom()


if __name__ == '__main__':

import sys

app = QtGui.QApplication(sys.argv)
mainWin = Window()
mainWin.show()