簡體   English   中英

PyQt5 scrollArea 與圖像按鈕的 GridLayout 滾動時模糊/調整大小

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

我是 PyQt5 或一般 GUI 設計的新手。 我正在嘗試制作圖像按鈕的可滾動網格布局。 當我嘗試滾動時,圖像按鈕會調整大小和模糊。 圖片:模糊滾動

這是我的代碼:

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 來 go 並為我的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM