Reputation: 21
In PySide6 (the same as PySide2 and PyQt5)
I wanted to divide the QMainWindow into four parts, each with a QScrollArea for displaying an image.Here is the minimal reproducible example.
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
import sys
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi()
def setupUi(self, *args,**kwargs):
self.mainWindow = MainWindow
self.resize(800,600)
self.load()
def load(self):
internalWidget = QWidget()
gridLayout = QGridLayout(internalWidget)
internalWidget.setLayout(gridLayout)
self.setCentralWidget(internalWidget)
# row, column, rowSpan, columnSpan
config = [
[0, 0, 1, 1],
[0, 1, 1, 1],
[1, 0, 1, 1],
[1, 1, 1, 1],
]
for row, column, rowSpan, columnSpan in config:
# create scrollArea
scrollArea = QScrollArea()
scrollArea.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
scrollArea.setViewportMargins(0,0,0,0)
scrollArea.setWidgetResizable(True)
gridLayout.setContentsMargins(0,0,0,0)
#########################random pixmap#####################################
arr = np.random.randint(0,255,(200+row*200,200+column*100))# random a image
if [row, column, rowSpan, columnSpan] == [0, 1, 1, 1]:
arr = np.random.randint(0,255,(1000,1000))
image = QImage(arr[:], arr.shape[1], arr.shape[0], arr.shape[1] * 3, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(image)
###########################################################################
label = QLabel()
label.setPixmap(pixmap)
scrollArea.setWidget(label)
scrollArea.setAlignment(Qt.AlignCenter)
label.show()
gridLayout.addWidget(scrollArea, row, column, rowSpan, columnSpan, alignment=Qt.AlignCenter)
scrollArea.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
But when I ran it, the QScrollArea could fill a grid of QGridLayout only if the image was large enough. If the image is smaller than a grid, the QScrollArea becomes the same size as the image (smaller than a grid).
How can I make each QScrollArea always fill a grid without resizing the image? (Not that each QScrollArea is different in size)
Upvotes: 1
Views: 949
Reputation: 243975
If you want each QScrollArea to occupy the same space then set the same stretch factor in each row and column:
import random
import sys
from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QColor, QPixmap
from PySide6.QtWidgets import (
QApplication,
QGridLayout,
QLabel,
QMainWindow,
QScrollArea,
QWidget,
)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi()
def setupUi(self):
self.resize(800, 600)
self.load()
def load(self):
internalWidget = QWidget()
gridLayout = QGridLayout(internalWidget)
gridLayout.setContentsMargins(0, 0, 0, 0)
self.setCentralWidget(internalWidget)
config = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]
for row, column in config:
scrollArea = QScrollArea(widgetResizable=True)
scrollArea.setViewportMargins(0, 0, 0, 0)
pixmap = QPixmap(QSize(*random.sample(range(0, 500), 2)))
pixmap.fill(QColor(*random.sample(range(0, 255), 3)))
label = QLabel(alignment=Qt.AlignCenter)
label.setPixmap(pixmap)
scrollArea.setWidget(label)
gridLayout.addWidget(scrollArea, row, column)
gridLayout.setColumnStretch(0, 1)
gridLayout.setColumnStretch(1, 1)
gridLayout.setRowStretch(0, 1)
gridLayout.setRowStretch(1, 1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Upvotes: 2