Адаптация количества виджетов под ширину экрана. Инструментами QGridLayout
Как можно сделать так, чтобы количество виджетов в ряду менялось от размера окна с помощью инструментов PyQt5.
Например:
При уменьшении экрана
Предварительно мой код:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, \
QPushButton, QLineEdit, QRadioButton, QGridLayout, QLabel, QHBoxLayout, QVBoxLayout, QMessageBox, QFileDialog
from PyQt5.QtWidgets import QAction, QScrollArea
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWork(QMainWindow):
def __init__(self):
super().__init__()
# self.setupUi(self)
self.initUI()
def initUI(self):
# self.widget_fast_start
self.widget1 = QWidget(self)
self.setCentralWidget(self.widget1)
self.GridLayout = QGridLayout(self.widget1)
for i in range(4):
self.push = QPushButton(self.widget1)
self.push.setText(f"Кнопка{i}")
self.push.setObjectName(f"push{i}")
self.GridLayout.addWidget(self.push, 0, i)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWork()
ex.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Резиновая лента.
import sys
from PyQt5.Qt import *
class FlowLayout(QLayout):
def __init__(self, orientation=Qt.Horizontal, parent=None, margin=0, spacing=-1):
super().__init__(parent)
self.orientation = orientation
if parent is not None:
self.setContentsMargins(margin, margin, margin, margin)
self.setSpacing(spacing)
self.itemList = []
def __del__(self):
item = self.takeAt(0)
while item:
item = self.takeAt(0)
def addItem(self, item):
self.itemList.append(item)
def count(self):
return len(self.itemList)
def itemAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList[index]
return None
def takeAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList.pop(index)
return None
def expandingDirections(self):
return Qt.Orientations(Qt.Orientation(0))
def hasHeightForWidth(self):
return self.orientation == Qt.Horizontal
def heightForWidth(self, width):
return self.doLayout(QRect(0, 0, width, 0), True)
def hasWidthForHeight(self):
return self.orientation == Qt.Vertical
def widthForHeight(self, height):
return self.doLayout(QRect(0, 0, 0, height), True)
def setGeometry(self, rect):
super().setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QSize()
for item in self.itemList:
size = size.expandedTo(item.minimumSize())
margin, _, _, _ = self.getContentsMargins()
size += QSize(2 * margin, 2 * margin)
return size
def doLayout(self, rect, testOnly):
x = rect.x()
y = rect.y()
lineHeight = columnWidth = heightForWidth = 0
for item in self.itemList:
wid = item.widget()
spaceX = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal)
spaceY = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical)
if self.orientation == Qt.Horizontal:
nextX = x + item.sizeHint().width() + spaceX
if nextX - spaceX > rect.right() and lineHeight > 0:
x = rect.x()
y = y + lineHeight + spaceY
nextX = x + item.sizeHint().width() + spaceX
lineHeight = 0
if not testOnly:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = nextX
lineHeight = max(lineHeight, item.sizeHint().height())
else:
nextY = y + item.sizeHint().height() + spaceY
if nextY - spaceY > rect.bottom() and columnWidth > 0:
x = x + columnWidth + spaceX
y = rect.y()
nextY = y + item.sizeHint().height() + spaceY
columnWidth = 0
heightForWidth += item.sizeHint().height() + spaceY
if not testOnly:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
y = nextY
columnWidth = max(columnWidth, item.sizeHint().width())
if self.orientation == Qt.Horizontal:
return y + lineHeight - rect.y()
else:
return heightForWidth - rect.y()
class MainWork(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.widget1 = QWidget(self)
self.setCentralWidget(self.widget1)
flowLayout = FlowLayout(orientation=Qt.Horizontal)
self.widget1.setLayout(flowLayout)
for i in range(4):
push = QPushButton(f"Кнопка{i}")
flowLayout.addWidget(push)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWork()
ex.show()
sys.exit(app.exec_())



