本文整理汇总了Python中AnyQt.QtGui.QPixmap.copy方法的典型用法代码示例。如果您正苦于以下问题:Python QPixmap.copy方法的具体用法?Python QPixmap.copy怎么用?Python QPixmap.copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AnyQt.QtGui.QPixmap
的用法示例。
在下文中一共展示了QPixmap.copy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Screenshot
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import copy [as 别名]
class Screenshot(QDialog):
def __init__(self, parent=None):
super().__init__(parent, Qt.CustomizeWindowHint | Qt.FramelessWindowHint | Qt.Window |
Qt.WindowStaysOnTopHint | Qt.X11BypassWindowManagerHint)
self.setAttribute(Qt.WA_DeleteOnClose)
self.installEventFilter(self)
self.setMouseTracking(True)
self._band = QRubberBand(QRubberBand.Rectangle, self)
self._resize_origin = None
self._drag_mask = 0
self._origin = None
self.selected_image = None
# Window background
desktop = QApplication.desktop()
if is_qt5():
g = desktop.geometry()
self._snapshot = QPixmap(g.width(), g.height())
painter = QPainter(self._snapshot)
for screen in QApplication.screens():
g = screen.geometry()
painter.drawPixmap(g, screen.grabWindow(0, g.x(), g.y(), g.width(), g.height()))
painter.end()
else:
self._snapshot = QPixmap.grabWindow(desktop.winId(), 0, 0, desktop.width(), desktop.height())
self.setGeometry(desktop.geometry())
self._darken = self._snapshot.copy()
painter = QPainter(self._darken)
brush = QBrush(QColor(0, 0, 0, 128))
painter.setBrush(brush)
painter.drawRect(self._darken.rect())
painter.end()
# Buttons
self._buttons = QWidget(self)
self._button_layout = QHBoxLayout(self._buttons)
self._button_layout.setSpacing(0)
self._button_layout.setContentsMargins(0, 0, 0, 0)
self._button_layout.setContentsMargins(0, 0, 0, 0)
self.save_as = QPushButton(self.tr('Save As'))
self.save_as.pressed.connect(self.save_image_as)
self.save_as.setCursor(Qt.ArrowCursor)
self._button_layout.addWidget(self.save_as)
self.copy = QPushButton(self.tr('Copy'))
self.copy.pressed.connect(self.copy_to_clipboard)
self.copy.setCursor(Qt.ArrowCursor)
self._button_layout.addWidget(self.copy)
self.share = QPushButton(self.tr('Share'))
self.share.pressed.connect(self.share_selection)
self.share.setCursor(Qt.ArrowCursor)
self._button_layout.addWidget(self.share)
self._buttons.hide()
def paintEvent(self, _):
painter = QPainter(self)
painter.drawPixmap(0, 0, self._darken)
if self._band.isVisible():
br = self._band.geometry()
r = QRect(br.topLeft(), br.bottomRight())
painter.drawPixmap(r, self._snapshot.copy(r))
def get_selection(self):
return self._snapshot.copy(self._band.geometry())
def save_image_as(self):
img = self.get_selection().toImage()
if img.isNull():
QMessageBox.critical(self, self.tr('Error'), self.tr('No image was selected!'))
return
self.hide()
formats = {
self.tr('Portable Network Graphics (*.png)'): 'png',
self.tr('Joint Photographic Experts Group (*.jpg *.jpeg)'): 'jpg',
self.tr('Graphics Interchange Format (*.gif)'): 'gif',
self.tr('Bitmap (*.bmp)'): 'bmp',
self.tr('All Images (*.png *.jpg *.gif *.bmp)'): 'all'
}
file_format = None
destination = QFileDialog.getSaveFileName(self, 'Save image', '', ';;'.join(formats.keys()))
if isinstance(destination, tuple):
destination, file_format = destination
file_format = formats[file_format]
if file_format == 'all':
file_format = None
if not file_format:
file_format = destination.rsplit('.', 1)[-1]
if destination:
if file_format not in formats.values():
file_format = 'png'
if not destination.endswith('.' + file_format):
destination += '.' + file_format
img.save(destination, file_format, 0 if file_format == 'png' else 90)
#.........这里部分代码省略.........