在 PySide6 中(与 PySide2 和 PyQt5 相同)
我想将 QMainWindow 分为四个部分,每个部分都有一个 QScrollArea 用于显示图像。这是最小的可重现示例。
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_())
但是当我运行它时,只有当图像足够大时,QScrollArea 才能填充 QGridLayout 的网格。如果图像小于网格,则 QScrollArea 变为与图像相同的大小(小于网格)。
如何使每个 QScrollArea 始终填充网格而不调整图像大小?(并不是说每个 QScrollArea 的大小都不同)