I have a QPixmapCache question from PyQt.

Asked 1 years ago, Updated 1 years ago, 128 views

Currently, I am studying Python and pyqt by myself. If you don't know anything, I'm trying to implement Conway's Life Game by visiting the Internet, and I drew it again every time each cell is updated, and I modified the code to use QPixmapCache because using QPixmapCache has advantages in running speed and memory usage.

However, if you run the program to a certain extent, there is no significant difference in the execution speed, and you suddenly get an error saying that there is no overload of the drawPixmap function corresponding to the input value in the part shown below.

I think Key died because of lack of cache, but if there is an error due to lack of cache, shouldn't there be an error from the beginning? I wonder if just doing drawPixmap or QPixmapCache.find while running increases cache usage.

Also, I wonder if there is a way to solve this problem.

Key in #QPixmapCache
on_key = None
off_key = None

#width/height of each cell
CELL_W = 10
CELL_H = 10

#Cell's graphic class
class Cell(QWidget):
    def __init__(self, x, y, *args, **kwargs):
        super(Cell, self).__init__(*args, **kwargs)

        self.x = x
        self.y = y

    #What happens when you update in MainWindow
    def paintEvent(self, event):
        p = QPainter()
        p.begin(self)

        if is_map[self.y][self.x] is True:
            p.drawPixmap(self.rect(), QPixmapCache.find(on_key)) #<-This part
        else:
            p.drawPixmap(self.rect(), QPixmapCache.find(off_key))
        p.end()
    #...

#MainWindow class
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        #...

        self.pixmap_reset()

        self.show()

    #QPixmapThe part where you store the first Pixmap in Cache
    def pixmap_reset(self):
        p = QPainter()
        on_map = QPixmap(CELL_W, CELL_H)
        off_map = QPixmap(CELL_W, CELL_H)

        global on_key, off_key #Bringing global variables

        # Draw the shape of the cell on_map and save it to QPixmapCache
        p.begin(on_map)
        p.setPen(Qt.black)
        p.setBrush(ON_COLOR)
        p.drawRect(on_map.rect())
        on_key = QPixmapCache.Key(QPixmapCache.insert(on_map))
        p.end()

        # Draw the shape of the cell in off_map and save it in QPixmapCache
        p.begin(off_map)
        p.setPen(Qt.black)
        p.setBrush(OFF_COLOR)
        p.drawRect(off_map.rect())
        off_key = QPixmapCache.Key(QPixmapCache.insert(off_map))
        p.end()

python pyqt pyqt5

2022-09-22 08:14

1 Answers

To find out the cause of the error,

https://doc.qt.io/qtforpython/PySide2/QtGui/QPixmapCache.html

The QPixmapCache.found() static member function returns bool. QPixmap.DrawPixmap member function and any of the functions that are overloaded with the same name are error because no function receives bool.

The expression Key died due to lack of cache seems to have omitted the terms necessary for explaining the situation, but

Anyway, that's not the problem.

Regarding the effectiveness of the cache in your situation,

In the above circumstances, using cache won't make much of a profit. It's not a pixmap that's drawn by heavy image processing or a combination of many layers, it's just a simple color-filed-rect.

The target of using that cache is pixmap, which costs a lot to make as written in the official document. It doesn't help much in this situation.

Finally, it's about how to use it

The QPixmapCache.find(key) function does not return a pixmap and should not be written like the code you wrote.

If it's just meaningful to use the cache,

on_map = QPixmap(CELL_W, CELL_H)

p = QPainter()
p.begin(on_map)
p.setPen(Qt.black)
p.setBrush(ON_COLOR)
p.drawRect(on_map.rect())
p.end()

on_key = QPixmapCache.insert(on_map) # Returns the key anyway
# If you want to write a pixmap that has already been cached in another function,
pm = QPixmap()
QPixmapCache.find(on_key, pm)
painter.drawPixmap(0, 0, pm)


2022-09-22 08:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.