我的PyQt代码不能显示超过1024张图片

时间:2019-06-07 19:17:49

标签: python pyqt pyqt4 qlabel qmovie

我正在尝试创建一些带有gif预览的文件的浏览器。一切正常,除了我的pyQt代码在1000个gif后不显示任何gif文件。

我已经在pyQt5和pyQt4上对其进行了测试。

这是完整的代码:

from PyQt4 import QtCore, QtGui

import datetime

# flow layout from qt example
class FlowLayout(QtGui.QLayout):
    def __init__(self, parent=None, margin=0, spacing=-1):
        super(FlowLayout, self).__init__(parent)

        if parent is not None:
            self.setMargin(margin)

        self.setSpacing(spacing)

        self.itemList = []

        self.isDoLayoutIsActive = False

    def __del__(self):
        item = self.takeAt(0)
        while item:
            item = self.takeAt(0)

    def addItem(self, item):
        self.itemList.append(item)

    def count(self):
        return len(self.itemList)

    def itemAt(self, index):
        if index >= 0 and index < len(self.itemList):
            return self.itemList[index]

        return None

    def takeAt(self, index):
        if index >= 0 and index < len(self.itemList):
            return self.itemList.pop(index)

        return None

    def expandingDirections(self):
        return QtCore.Qt.Orientations(QtCore.Qt.Orientation(0))

    def hasHeightForWidth(self):
        return True

    def heightForWidth(self, width):
        height = self._doLayout(QtCore.QRect(0, 0, width, 0), True)
        return height

    def setGeometry(self, rect):
        # super(FlowLayout, self).setGeometry(rect)
        self._doLayout(rect, False)

    def sizeHint(self):
        return self.minimumSize()

    def minimumSize(self):
        size = QtCore.QSize()

        for item in self.itemList:
            size = size.expandedTo(item.minimumSize())

        size += QtCore.QSize(2 * self.margin(), 2 * self.margin())
        return size

    def _doLayout(self, rect, testOnly):
        x = rect.x()
        y = rect.y()
        lineHeight = 0

        if (self.isDoLayoutIsActive):
            return
        else:
            self.isDoLayoutIsActive = True

        for item in self.itemList:
            wid = item.widget()
            spaceX = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton,QtGui.QSizePolicy.PushButton,QtCore.Qt.Horizontal)
            spaceY = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton,QtGui.QSizePolicy.PushButton,QtCore.Qt.Vertical)

            nextX = x + item.sizeHint().width() + spaceX
            if nextX - spaceX > rect.right() and lineHeight > 0:
                x = rect.x()
                y = y + lineHeight + spaceY
                nextX = x + item.sizeHint().width() + spaceX
                lineHeight = 0

            if not testOnly:
                item.setGeometry(QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))

            x = nextX
            lineHeight = max(lineHeight, item.sizeHint().height())


        self.isDoLayoutIsActive = False
        print(str(datetime.datetime.now()))

        return y + lineHeight - rect.y()


if __name__ == '__main__':

    import sys

    class Window(QtGui.QWidget):
        def __init__(self):
            super(Window, self).__init__()

            vbox = QtGui.QVBoxLayout()

            butt = QtGui.QPushButton("Render!")
            butt.clicked.connect(self.rend)
            vbox.addWidget(butt)

            vboxResultTab = QtGui.QVBoxLayout()
            self.flowLayout = FlowLayout()
            resultItemsWidget = QtGui.QWidget()
            resultItemsWidget.setLayout(self.flowLayout)
            scrollResultArea=QtGui.QScrollArea()
            scrollResultArea.setWidgetResizable(True)
            vboxResultTab.addWidget(scrollResultArea)
            scrollResultArea.setWidget(resultItemsWidget)
            vboxResultTab.addWidget(scrollResultArea)

            self.vboxWidgetWithArea = QtGui.QWidget()
            self.vboxWidgetWithArea.setLayout(vboxResultTab)
            vbox.addWidget(self.vboxWidgetWithArea)

            self.setLayout(vbox)
            self.setWindowTitle("Flow Layout")
            self.xx = 0

        def rend(self):
            # print(str(datetime.datetime.now()))
            self.vboxWidgetWithArea.hide()
            for x in xrange(1,1500):
                gif = QtGui.QLabel("")
                gif.setMovie(QtGui.QMovie("/gifs/placeholder.gif"))
                gif.movie().start()
                gif.movie().stop()
                gif.setMaximumSize(QtCore.QSize(300, 200))
                gif.setMinimumSize(QtCore.QSize(300, 200))
                self.xx = self.xx + 1
                butt1 = QtGui.QPushButton(str(self.xx))
                butt1.clicked.connect(lambda: self.printNum(self.xx))

                self.flowLayout.addWidget(gif)
                self.flowLayout.addWidget(butt1)

            print(len(self.flowLayout.itemList))
            self.vboxWidgetWithArea.show()

        def printNum(self, num):
            print(num)

    app = QtGui.QApplication(sys.argv)
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())

这是上传Gif文件的部分:

    for x in xrange(1,1500):
        gif = QtGui.QLabel("")
        gif.setMovie(QtGui.QMovie("/gifs/placeholder.gif"))
        gif.movie().start()
        gif.movie().stop()
        gif.setMaximumSize(QtCore.QSize(300, 200))
        gif.setMinimumSize(QtCore.QSize(300, 200))
        self.flowLayout.addWidget(gif)
        self.flowLayout.addWidget(butt1)

如何解决我的应用显示超过1000个gif /图像文件的问题?

0 个答案:

没有答案