flow layout
Streaming layout, also called waterfall layout, is a page layout often used in the web page, the principle of which is to fix the height, and then the width of the picture is adaptive, so that the loaded picture looks like a waterfall as neatly as the water flowing down.
pyqt streaming layout
So how do we use flow layout in pyqt5? pyqt doesn't have this control, we need to encapsulate it ourselves, here is the encapsulation code for flow layout.
class FlowLayout(QLayout): def __init__(self, parent=None, margin=0, spacing=-1): super(FlowLayout, self).__init__(parent) if parent is not None: (margin, margin, margin, margin) (spacing) = [] def __del__(self): item = (0) while item: item = (0) def addItem(self, item): (item) def count(self): return len() def itemAt(self, index): if index >= 0 and index < len(): return [index] return None def takeAt(self, index): if index >= 0 and index < len(): return (index) return None def expandingDirections(self): return ((0)) def hasHeightForWidth(self): return True def heightForWidth(self, width): height = (QRect(0, 0, width, 0), True) return height def setGeometry(self, rect): super(FlowLayout, self).setGeometry(rect) (rect, False) def sizeHint(self): return () def minimumSize(self): size = QSize() for item in : size = (()) margin, _, _, _ = () size += QSize(2 * margin, 2 * margin) return size def doLayout(self, rect, testOnly): x = () y = () lineHeight = 0 for item in : wid = () spaceX = () + ().layoutSpacing(, , ) spaceY = () + ().layoutSpacing(, , ) nextX = x + ().width() + spaceX if nextX - spaceX > () and lineHeight > 0: x = () y = y + lineHeight + spaceY nextX = x + ().width() + spaceX lineHeight = 0 if not testOnly: (QRect(QPoint(x, y), ())) x = nextX lineHeight = max(lineHeight, ().height()) return y + lineHeight - ()
Encapsulated flow layout class, we just pass in the appropriate layout, he will automatically calculate the page elements, to adapt to the width of the page.
Here's the code we wrote for a waterfall to display an image:
from import QPoint, QRect, QSize, Qt import os from PyQt5 import QtCore, QtGui, QtWidgets from import ( QApplication, QLayout, QPushButton, QSizePolicy, QWidget, QGridLayout) class Window(QWidget): def __init__(self): = 100 super(Window, self).__init__() (400, 300) flowLayout = FlowLayout() highlight_dir = "./" self.files_it = iter([(highlight_dir, file) for file in (highlight_dir)]) print() for file in iter(self.files_it): layout = QGridLayout() pixmap = (file) if not (): autoWidth = ()*/() label = (pixmap=pixmap) (True) () print(autoWidth) (autoWidth) #(100, 50) (label) widget = QWidget() (layout) (widget) (flowLayout) ("Flow Layout") class FlowLayout(QLayout): def __init__(self, parent=None, margin=0, spacing=-1): super(FlowLayout, self).__init__(parent) if parent is not None: (margin, margin, margin, margin) (spacing) = [] def __del__(self): item = (0) while item: item = (0) def addItem(self, item): (item) def count(self): return len() def itemAt(self, index): if index >= 0 and index < len(): return [index] return None def takeAt(self, index): if index >= 0 and index < len(): return (index) return None def expandingDirections(self): return ((0)) def hasHeightForWidth(self): return True def heightForWidth(self, width): height = (QRect(0, 0, width, 0), True) return height def setGeometry(self, rect): super(FlowLayout, self).setGeometry(rect) (rect, False) def sizeHint(self): return () def minimumSize(self): size = QSize() for item in : size = (()) margin, _, _, _ = () size += QSize(2 * margin, 2 * margin) return size def doLayout(self, rect, testOnly): x = () y = () lineHeight = 0 for item in : wid = () spaceX = () + ().layoutSpacing(, , ) spaceY = () + ().layoutSpacing(, , ) nextX = x + ().width() + spaceX if nextX - spaceX > () and lineHeight > 0: x = () y = y + lineHeight + spaceY nextX = x + ().width() + spaceX lineHeight = 0 if not testOnly: (QRect(QPoint(x, y), ())) x = nextX lineHeight = max(lineHeight, ().height()) return y + lineHeight - () if __name__ == '__main__': import sys app = QApplication() mainWin = Window() () (app.exec_())
to this article on the python GUI framework pyqt5 picture flow layout method (waterfall flowlayout) of the article is introduced to this, more related python pyqt5 picture flow layout content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!