def testWithKey(self): pm1 = QPixmap() ok = QPixmapCache.find(QPixmapCache.Key(), pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find(QPixmapCache.Key()), None) pm2 = QPixmap() key = QPixmapCache.insert(pm2) pm3 = QPixmap() ok = QPixmapCache.find(key, pm3) self.assertTrue(ok) self.assertEqual(QPixmapCache.find(key).toImage().bits(), pm3.toImage().bits())
def __paint_image_array( self, raw_data, painter: "QPainter", option: "QStyleOptionViewItem", index: "QModelIndex", ): """Paints a pixmap centered on the cell. Generated pixmaps are saved on cache by the name "dataset_row_col" """ # Load Qt pixap from array pix = QPixmap() pix_name = str(id(raw_data)) if not QPixmapCache.find(pix_name, pix): # Load pix from raw array pix = QPixmap.fromImage(qimage2ndarray.array2qimage(raw_data)) # Save pix on cache QPixmapCache.insert(pix_name, pix) pix = pix.scaled(option.rect.width(), option.rect.height(), Qt.KeepAspectRatio) # Calculate central position x_coord = option.rect.center().x() - pix.width() / 2 y_coord = option.rect.center().y() - pix.height() / 2 draw_rect = QRect(x_coord, y_coord, pix.width(), pix.height()) # Draw pixm painter.drawPixmap(draw_rect, pix)
def testWithString(self): pm1 = QPixmap() ok = QPixmapCache.find('img', pm1) self.assertFalse(ok) self.assertEqual(QPixmapCache.find('img'), None) pm2 = QPixmap() ok = QPixmapCache.insert('img', pm2) self.assertTrue(ok) pm3 = QPixmap() ok = QPixmapCache.find('img', pm3) self.assertTrue(ok) b1 = QPixmapCache.find('img').toImage().bits() b2 = pm3.toImage().bits() self.assertEqual(QPixmapCache.find('img').toImage().bits(), pm3.toImage().bits())