def paintEvent(self, event): """A custom paint event handler which draws the image according to the current size constraints. """ pixmap = self._pixmap if pixmap is None: super().paintEvent(event) return pm_size = pixmap.size() pm_width = pm_size.width() pm_height = pm_size.height() if pm_width == 0 or pm_height == 0: super().paintEvent(event) width = self.size().width() height = self.size().height() if not self._scaled_contents: # If the image isn't scaled, it is centered if possible. # Otherwise, it's painted at the origin and clipped. paint_x = max(0, int(width / 2.0 - pm_width / 2.0)) paint_y = max(0, int(height / 2.0 - pm_height / 2.0)) paint_width = pm_width paint_height = pm_height else: # If the image *is* scaled, it's scaled size depends on the # size of the paint area as well as the other scaling flags. if self._preserve_aspect_ratio: pm_ratio = float(pm_width) / pm_height ratio = float(width) / height if ratio >= pm_ratio: if self._allow_upscaling: paint_height = height else: paint_height = min(pm_height, height) paint_width = int(paint_height * pm_ratio) else: if self._allow_upscaling: paint_width = width else: paint_width = min(pm_width, width) paint_height = int(paint_width / pm_ratio) else: if self._allow_upscaling: paint_height = height paint_width = width else: paint_height = min(pm_height, height) paint_width = min(pm_width, width) # In all cases of scaling, we know that the scaled image is # no larger than the paint area, and can thus be centered. paint_x = int(width / 2.0 - paint_width / 2.0) paint_y = int(height / 2.0 - paint_height / 2.0) # Finally, draw the pixmap into the calculated rect. painter = QPainter(self) painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform) painter.drawPixmap(paint_x, paint_y, paint_width, paint_height, pixmap)
def paintEvent(self, event): """ A custom paint event handler which draws the image according to the current size constraints. """ pixmap = self._pixmap if pixmap is None: return pm_size = pixmap.size() pm_width = pm_size.width() pm_height = pm_size.height() if pm_width == 0 or pm_height == 0: return width = self.size().width() height = self.size().height() if not self._scaled_contents: # If the image isn't scaled, it is centered if possible. # Otherwise, it's painted at the origin and clipped. paint_x = max(0, int(width / 2. - pm_width / 2.)) paint_y = max(0, int(height / 2. - pm_height / 2.)) paint_width = pm_width paint_height = pm_height else: # If the image *is* scaled, it's scaled size depends on the # size of the paint area as well as the other scaling flags. if self._preserve_aspect_ratio: pm_ratio = float(pm_width) / pm_height ratio = float(width) / height if ratio >= pm_ratio: if self._allow_upscaling: paint_height = height else: paint_height = min(pm_height, height) paint_width = int(paint_height * pm_ratio) else: if self._allow_upscaling: paint_width = width else: paint_width = min(pm_width, width) paint_height = int(paint_width / pm_ratio) else: if self._allow_upscaling: paint_height = height paint_width = width else: paint_height = min(pm_height, height) paint_width = min(pm_width, width) # In all cases of scaling, we know that the scaled image is # no larger than the paint area, and can thus be centered. paint_x = int(width / 2. - paint_width / 2.) paint_y = int(height / 2. - paint_height / 2.) # Finally, draw the pixmap into the calculated rect. painter = QPainter(self) painter.setRenderHint(QPainter.SmoothPixmapTransform) painter.drawPixmap(paint_x, paint_y, paint_width, paint_height, pixmap)
def paintEvent(self, event): super(QMapWidget, self).paintEvent(event) qp = QPainter() qp.begin(self) for p in self._pix_maps: qp.drawPixmap(0, 0, p) qp.end()