1

在 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 的大小都不同)

4

1 回答 1

2

如果您希望每个 QScrollArea 占用相同的空间,则在每行和每列中设置相同的拉伸因子:

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_())
于 2020-12-29T06:24:55.597 回答