aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-10-02 08:52:38 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-10-02 10:48:04 +0200
commitb27443ad62641468723010b9e8cdb72327ef1c94 (patch)
tree570cf1bda1d05753eab61615bb1f73d6bb86e156 /examples
parent62c21af778b7bff6c86e7f89ef03a87efa6c51cb (diff)
Polish the flow layout example
- Import by class - Rename variables and functions, - Fix long lines Task-number: PYSIDE-841 Change-Id: Icb4615e15d0b54d3e2893ffa7981ced7b7526952 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/widgets/layouts/flowlayout.py103
1 files changed, 54 insertions, 49 deletions
diff --git a/examples/widgets/layouts/flowlayout.py b/examples/widgets/layouts/flowlayout.py
index f63fd5549..970d5ac07 100644
--- a/examples/widgets/layouts/flowlayout.py
+++ b/examples/widgets/layouts/flowlayout.py
@@ -2,7 +2,7 @@
############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
-## Copyright (C) 2016 The Qt Company Ltd.
+## Copyright (C) 2020 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -42,34 +42,34 @@
"""PySide2 port of the widgets/layouts/flowlayout example from Qt v5.x"""
-from PySide2 import QtCore, QtWidgets
+import sys
+from PySide2.QtCore import Qt, QMargins, QPoint, QRect, QSize
+from PySide2.QtWidgets import (QApplication, QLayout, QPushButton,
+ QSizePolicy, QWidget)
-class Window(QtWidgets.QWidget):
+class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
- flowLayout = FlowLayout()
- flowLayout.addWidget(QtWidgets.QPushButton("Short"))
- flowLayout.addWidget(QtWidgets.QPushButton("Longer"))
- flowLayout.addWidget(QtWidgets.QPushButton("Different text"))
- flowLayout.addWidget(QtWidgets.QPushButton("More text"))
- flowLayout.addWidget(QtWidgets.QPushButton("Even longer button text"))
- self.setLayout(flowLayout)
+ flowLayout = FlowLayout(self)
+ flowLayout.addWidget(QPushButton("Short"))
+ flowLayout.addWidget(QPushButton("Longer"))
+ flowLayout.addWidget(QPushButton("Different text"))
+ flowLayout.addWidget(QPushButton("More text"))
+ flowLayout.addWidget(QPushButton("Even longer button text"))
self.setWindowTitle("Flow Layout")
-class FlowLayout(QtWidgets.QLayout):
- def __init__(self, parent=None, margin=0, spacing=-1):
+class FlowLayout(QLayout):
+ def __init__(self, parent=None):
super(FlowLayout, self).__init__(parent)
if parent is not None:
- self.setMargin(margin)
+ self.setContentsMargins(QMargins(0, 0, 0, 0))
- self.setSpacing(spacing)
-
- self.itemList = []
+ self._item_list = []
def __del__(self):
item = self.takeAt(0)
@@ -77,79 +77,84 @@ class FlowLayout(QtWidgets.QLayout):
item = self.takeAt(0)
def addItem(self, item):
- self.itemList.append(item)
+ self._item_list.append(item)
def count(self):
- return len(self.itemList)
+ return len(self._item_list)
def itemAt(self, index):
- if index >= 0 and index < len(self.itemList):
- return self.itemList[index]
+ if index >= 0 and index < len(self._item_list):
+ return self._item_list[index]
return None
def takeAt(self, index):
- if index >= 0 and index < len(self.itemList):
- return self.itemList.pop(index)
+ if index >= 0 and index < len(self._item_list):
+ return self._item_list.pop(index)
return None
def expandingDirections(self):
- return QtCore.Qt.Orientations(QtCore.Qt.Orientation(0))
+ return Qt.Orientations(Qt.Orientation(0))
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
- height = self.doLayout(QtCore.QRect(0, 0, width, 0), True)
+ height = self._do_layout(QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
- self.doLayout(rect, False)
+ self._do_layout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
- size = QtCore.QSize()
+ size = QSize()
- for item in self.itemList:
+ for item in self._item_list:
size = size.expandedTo(item.minimumSize())
- size += QtCore.QSize(2 * self.contentsMargins().top(), 2 * self.contentsMargins().top())
+ size += QSize(2 * self.contentsMargins().top(),
+ 2 * self.contentsMargins().top())
return size
- def doLayout(self, rect, testOnly):
+ def _do_layout(self, rect, test_only):
x = rect.x()
y = rect.y()
- lineHeight = 0
-
- for item in self.itemList:
- wid = item.widget()
- spaceX = self.spacing() + wid.style().layoutSpacing(QtWidgets.QSizePolicy.PushButton, QtWidgets.QSizePolicy.PushButton, QtCore.Qt.Horizontal)
- spaceY = self.spacing() + wid.style().layoutSpacing(QtWidgets.QSizePolicy.PushButton, QtWidgets.QSizePolicy.PushButton, QtCore.Qt.Vertical)
- nextX = x + item.sizeHint().width() + spaceX
- if nextX - spaceX > rect.right() and lineHeight > 0:
+ line_height = 0
+ spacing = self.spacing()
+
+ for item in self._item_list:
+ style = item.widget().style()
+ layout_spacing_x = style.layoutSpacing(QSizePolicy.PushButton,
+ QSizePolicy.PushButton,
+ Qt.Horizontal)
+ layout_spacing_y = style.layoutSpacing(QSizePolicy.PushButton,
+ QSizePolicy.PushButton,
+ Qt.Vertical)
+ space_x = spacing + layout_spacing_x
+ space_y = spacing + layout_spacing_y
+ next_x = x + item.sizeHint().width() + space_x
+ if next_x - space_x > rect.right() and line_height > 0:
x = rect.x()
- y = y + lineHeight + spaceY
- nextX = x + item.sizeHint().width() + spaceX
- lineHeight = 0
+ y = y + line_height + space_y
+ next_x = x + item.sizeHint().width() + space_x
+ line_height = 0
- if not testOnly:
- item.setGeometry(QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))
+ if not test_only:
+ item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
- x = nextX
- lineHeight = max(lineHeight, item.sizeHint().height())
+ x = next_x
+ line_height = max(line_height, item.sizeHint().height())
- return y + lineHeight - rect.y()
+ return y + line_height - rect.y()
if __name__ == '__main__':
-
- import sys
-
- app = QtWidgets.QApplication(sys.argv)
+ app = QApplication(sys.argv)
mainWin = Window()
mainWin.show()
sys.exit(app.exec_())