本文整理匯總了Python中qtpy.QtGui.QPixmap.scaledToWidth方法的典型用法代碼示例。如果您正苦於以下問題:Python QPixmap.scaledToWidth方法的具體用法?Python QPixmap.scaledToWidth怎麽用?Python QPixmap.scaledToWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qtpy.QtGui.QPixmap
的用法示例。
在下文中一共展示了QPixmap.scaledToWidth方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: FigureCanvas
# 需要導入模塊: from qtpy.QtGui import QPixmap [as 別名]
# 或者: from qtpy.QtGui.QPixmap import scaledToWidth [as 別名]
#.........這裏部分代碼省略.........
self.fig = None
self.fmt = None
self.fwidth, self.fheight = 200, 200
self._blink_flag = False
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.context_menu_requested)
def context_menu_requested(self, event):
"""Popup context menu."""
if self.fig:
pos = QPoint(event.x(), event.y())
context_menu = QMenu(self)
context_menu.addAction(ima.icon('editcopy'), "Copy Image",
self.copy_figure,
QKeySequence(
get_shortcut('plots', 'copy')))
context_menu.popup(self.mapToGlobal(pos))
@Slot()
def copy_figure(self):
"""Copy figure to clipboard."""
if self.fmt in ['image/png', 'image/jpeg']:
qpixmap = QPixmap()
qpixmap.loadFromData(self.fig, self.fmt.upper())
QApplication.clipboard().setImage(qpixmap.toImage())
elif self.fmt == 'image/svg+xml':
svg_to_clipboard(self.fig)
else:
return
self.blink_figure()
def blink_figure(self):
"""Blink figure once."""
if self.fig:
self._blink_flag = not self._blink_flag
self.repaint()
if self._blink_flag:
timer = QTimer()
timer.singleShot(40, self.blink_figure)
def clear_canvas(self):
"""Clear the figure that was painted on the widget."""
self.fig = None
self.fmt = None
self._qpix_buffer = []
self.repaint()
def load_figure(self, fig, fmt):
"""
Load the figure from a png, jpg, or svg image, convert it in
a QPixmap, and force a repaint of the widget.
"""
self.fig = fig
self.fmt = fmt
if fmt in ['image/png', 'image/jpeg']:
self._qpix_orig = QPixmap()
self._qpix_orig.loadFromData(fig, fmt.upper())
elif fmt == 'image/svg+xml':
self._qpix_orig = QPixmap(svg_to_image(fig))
self._qpix_buffer = [self._qpix_orig]
self.fwidth = self._qpix_orig.width()
self.fheight = self._qpix_orig.height()
def paintEvent(self, event):
"""Qt method override to paint a custom image on the Widget."""
super(FigureCanvas, self).paintEvent(event)
# Prepare the rect on which the image is going to be painted :
fw = self.frameWidth()
rect = QRect(0 + fw, 0 + fw,
self.size().width() - 2 * fw,
self.size().height() - 2 * fw)
if self.fig is None or self._blink_flag:
return
# Check/update the qpixmap buffer :
qpix2paint = None
for qpix in self._qpix_buffer:
if qpix.size().width() == rect.width():
qpix2paint = qpix
break
else:
if self.fmt in ['image/png', 'image/jpeg']:
qpix2paint = self._qpix_orig.scaledToWidth(
rect.width(), mode=Qt.SmoothTransformation)
elif self.fmt == 'image/svg+xml':
qpix2paint = QPixmap(svg_to_image(self.fig, rect.size()))
self._qpix_buffer.append(qpix2paint)
if qpix2paint is not None:
# Paint the image on the widget :
qp = QPainter()
qp.begin(self)
qp.drawPixmap(rect, qpix2paint)
qp.end()
示例2: FigureCanvas
# 需要導入模塊: from qtpy.QtGui import QPixmap [as 別名]
# 或者: from qtpy.QtGui.QPixmap import scaledToWidth [as 別名]
class FigureCanvas(QFrame):
"""
A basic widget on which can be painted a custom png, jpg, or svg image.
"""
def __init__(self, parent=None):
super(FigureCanvas, self).__init__(parent)
self.setLineWidth(2)
self.setMidLineWidth(1)
self.setStyleSheet("background-color: white")
self.fig = None
self.fmt = None
self.fwidth, self.fheight = 200, 200
def clear_canvas(self):
"""Clear the figure that was painted on the widget."""
self.fig = None
self.fmt = None
self._qpix_buffer = []
self.repaint()
def load_figure(self, fig, fmt):
"""
Load the figure from a png, jpg, or svg image, convert it in
a QPixmap, and force a repaint of the widget.
"""
self.fig = fig
self.fmt = fmt
if fmt in ['image/png', 'image/jpeg']:
self._qpix_orig = QPixmap()
self._qpix_orig.loadFromData(fig, fmt.upper())
elif fmt == 'image/svg+xml':
self._qpix_orig = QPixmap(svg_to_image(fig))
self._qpix_buffer = [self._qpix_orig]
self.fwidth = self._qpix_orig.width()
self.fheight = self._qpix_orig.height()
def paintEvent(self, event):
"""Qt method override to paint a custom image on the Widget."""
super(FigureCanvas, self).paintEvent(event)
# Prepare the rect on which the image is going to be painted :
fw = self.frameWidth()
rect = QRect(0 + fw, 0 + fw,
self.size().width() - 2 * fw,
self.size().height() - 2 * fw)
if self.fig is None:
return
# Check/update the qpixmap buffer :
qpix2paint = None
for qpix in self._qpix_buffer:
if qpix.size().width() == rect.width():
qpix2paint = qpix
break
else:
if self.fmt in ['image/png', 'image/jpeg']:
qpix2paint = self._qpix_orig.scaledToWidth(
rect.width(), mode=Qt.SmoothTransformation)
elif self.fmt == 'image/svg+xml':
qpix2paint = QPixmap(svg_to_image(self.fig, rect.size()))
self._qpix_buffer.append(qpix2paint)
if qpix2paint is not None:
# Paint the image on the widget :
qp = QPainter()
qp.begin(self)
qp.drawPixmap(rect, qpix2paint)
qp.end()