PyQt5 滚动区域与图像按钮的 GridLayout blur/resize 滚动时

PyQt5 scrollArea with GridLayout of Image buttons blur/resize when scrolling

我是 PyQt5 或一般 GUI 设计的新手。 我正在尝试制作图像按钮的可滚动网格布局。当我尝试滚动时,图像按钮将调整大小并变得模糊。图片:blur scroll

这是我的代码:

from PyQt5.QtWidgets import (QApplication, QMainWindow, QAction,
                            QComboBox, QWidget, QAbstractButton,
                            QGridLayout, QHBoxLayout, QVBoxLayout, 
                            QScrollArea, QDockWidget, QLabel,
                            QLayout)
from PyQt5.QtGui import (QPixmap, QPainter)
from PyQt5.QtCore import (Qt, QSize,pyqtSignal, QRect)

class pic_button(QAbstractButton):
    def __init__(self, pixmap, parent=None):
        super(pic_button, self).__init__(parent)
        self.pixmap = QPixmap(pixmap)

    def sizeHint(self):
        return QSize(100, 200)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), self.pixmap)

    def pic_change(self, pixmap):
        self.pixmap = QPixmap(pixmap)

class gui(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui_init()

    def ui_init(self):
        self.gallery_widget = QWidget()
        self.gallery_layout = QGridLayout()
        self.gallery_layout.setSizeConstraint(QLayout.SizeConstraint.SetFixedSize)

        self.buttons = []
        i = 0
        j = 0
        arr = []
        for x in range(100):
            img_path = "index.png"
            button = pic_button(img_path)
            if j > 10:
                i = i + 1
                j = 0
            self.gallery_layout.addWidget(button, i, j)
            self.buttons.append(button)
            j = j + 1

        self.gallery_widget.setLayout(self.gallery_layout)

        self.scroll = QScrollArea()
        self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scroll.setWidgetResizable(False)
        self.scroll.setWidget(self.gallery_widget)

        self.setCentralWidget(self.scroll)

        self.setGeometry(600, 600, 600, 600)
        self.show()

if __name__ == '__main__':
    app = QApplication([])
    ui = gui()
    sys.exit(app.exec_())

我已经在谷歌上搜索了几个小时,试图找到解决方案。我想我需要使用 QGraphicsView 并为我的 pic_button() 更改我的 paintEvent。我希望它像一个图片库查看应用程序。

找到解决方案

接收到 event.Rect() 时的 paintEvent() 具有矩形的 x 和 y 并不总是为 0,因为它受到滚动的影响。当 QPainter 转到 drawPixmap() 时,会发生模糊。

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.width(), self.height(), self.pixmap)