本文整理汇总了Python中AnyQt.QtGui.QPixmap.size方法的典型用法代码示例。如果您正苦于以下问题:Python QPixmap.size方法的具体用法?Python QPixmap.size怎么用?Python QPixmap.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AnyQt.QtGui.QPixmap
的用法示例。
在下文中一共展示了QPixmap.size方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GraphicsPixmapWidget
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import size [as 别名]
class GraphicsPixmapWidget(QGraphicsWidget):
"""
A QGraphicsWidget displaying a QPixmap
"""
def __init__(self, pixmap=None, parent=None):
super().__init__(parent)
self.setCacheMode(QGraphicsItem.ItemCoordinateCache)
self._pixmap = QPixmap(pixmap) if pixmap is not None else QPixmap()
self._keepAspect = True
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
def setPixmap(self, pixmap):
self._pixmap = QPixmap(pixmap)
self.updateGeometry()
self.update()
def pixmap(self):
return QPixmap(self._pixmap)
def setKeepAspectRatio(self, keep):
if self._keepAspect != keep:
self._keepAspect = bool(keep)
self.update()
def keepAspectRatio(self):
return self._keepAspect
def setGeometry(self, rect):
self.prepareGeometryChange()
super().setGeometry(rect)
def sizeHint(self, which, constraint=QSizeF()):
if which == Qt.PreferredSize:
return QSizeF(self._pixmap.size())
else:
return QGraphicsWidget.sizeHint(self, which, constraint)
def paint(self, painter, option, widget=0):
if self._pixmap.isNull():
return
rect = self.contentsRect()
pixsize = QSizeF(self._pixmap.size())
aspectmode = (Qt.KeepAspectRatio if self._keepAspect
else Qt.IgnoreAspectRatio)
pixsize.scale(rect.size(), aspectmode)
pixrect = QRectF(QPointF(0, 0), pixsize)
pixrect.moveCenter(rect.center())
painter.save()
painter.setPen(QPen(QColor(0, 0, 0, 50), 3))
painter.drawRoundedRect(pixrect, 2, 2)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
source = QRectF(QPointF(0, 0), QSizeF(self._pixmap.size()))
painter.drawPixmap(pixrect, self._pixmap, source)
painter.restore()
示例2: __startInternalDrag
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import size [as 别名]
def __startInternalDrag(self, frame, hotSpot=None):
drag = QDrag(self)
pixmap = QPixmap(frame.size())
frame.render(pixmap)
transparent = QPixmap(pixmap.size())
transparent.fill(Qt.transparent)
painter = QPainter(transparent)
painter.setOpacity(0.35)
painter.drawPixmap(0, 0, pixmap.width(), pixmap.height(), pixmap)
painter.end()
drag.setPixmap(transparent)
if hotSpot is not None:
drag.setHotSpot(hotSpot)
mime = QMimeData()
mime.setData("application/x-internal-move", b"")
drag.setMimeData(mime)
return drag.exec_(Qt.MoveAction)
示例3: Preview
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import size [as 别名]
class Preview(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__pixmap = QPixmap()
# Flag indicating if the widget was resized as a result of user
# initiated window resize. When false the widget will automatically
# resize/re-position based on pixmap size.
self.__hasExplicitSize = False
self.__inUpdateWindowGeometry = False
def setPixmap(self, pixmap):
if self.__pixmap != pixmap:
self.__pixmap = QPixmap(pixmap)
self.__updateWindowGeometry()
self.update()
self.updateGeometry()
def pixmap(self):
return QPixmap(self.__pixmap)
def resizeEvent(self, event):
super().resizeEvent(event)
if self.isVisible() and self.isWindow() and \
not self.__inUpdateWindowGeometry:
# mark that we have an explicit user provided size
self.__hasExplicitSize = True
def __updateWindowGeometry(self):
if not self.isWindow() or self.__hasExplicitSize:
return
def framemargins(widget):
frame, geom = widget.frameGeometry(), widget.geometry()
return QMargins(geom.left() - frame.left(),
geom.top() - frame.top(),
geom.right() - frame.right(),
geom.bottom() - frame.bottom())
def fitRect(rect, targetrect):
size = rect.size().boundedTo(targetgeom.size())
newrect = QRect(rect.topLeft(), size)
dx, dy = 0, 0
if newrect.left() < targetrect.left():
dx = targetrect.left() - newrect.left()
if newrect.top() < targetrect.top():
dy = targetrect.top() - newrect.top()
if newrect.right() > targetrect.right():
dx = targetrect.right() - newrect.right()
if newrect.bottom() > targetrect.bottom():
dy = targetrect.bottom() - newrect.bottom()
return newrect.translated(dx, dy)
margins = framemargins(self)
minsize = QSize(120, 120)
pixsize = self.__pixmap.size()
available = QApplication.desktop().availableGeometry(self)
available = available.adjusted(margins.left(), margins.top(),
-margins.right(), -margins.bottom())
# extra adjustment so the preview does not cover the whole desktop
available = available.adjusted(10, 10, -10, -10)
targetsize = pixsize.boundedTo(available.size()).expandedTo(minsize)
pixsize.scale(targetsize, Qt.KeepAspectRatio)
if not self.testAttribute(Qt.WA_WState_Created) or \
self.testAttribute(Qt.WA_WState_Hidden):
center = available.center()
else:
center = self.geometry().center()
targetgeom = QRect(QPoint(0, 0), pixsize)
targetgeom.moveCenter(center)
if not available.contains(targetgeom):
targetgeom = fitRect(targetgeom, available)
self.__inUpdateWindowGeometry = True
self.setGeometry(targetgeom)
self.__inUpdateWindowGeometry = False
def sizeHint(self):
return self.__pixmap.size()
def paintEvent(self, event):
if self.__pixmap.isNull():
return
sourcerect = QRect(QPoint(0, 0), self.__pixmap.size())
pixsize = QSizeF(self.__pixmap.size())
rect = self.contentsRect()
pixsize.scale(QSizeF(rect.size()), Qt.KeepAspectRatio)
targetrect = QRectF(QPointF(0, 0), pixsize)
targetrect.moveCenter(QPointF(rect.center()))
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.drawPixmap(targetrect, self.__pixmap, QRectF(sourcerect))
painter.end()
示例4: __init__
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import size [as 别名]
def __init__(self, parent=None, filename=None):
super(MainForm, self).__init__(parent)
self.view = GraphicsView()
background = QPixmap(filename)
# assume screnshots were taken on the same system stamper is being used on
# DPI check might be more robust, can be added if needed
background.setDevicePixelRatio(self.devicePixelRatioF())
self.filename = os.path.splitext(filename)[0]
if ("-%s" % ORG) in self.filename:
self.filename = self.filename[:-len(ORG)-5] + ".png"
# self.view.setBackgroundBrush(QBrush(background))
# self.view.setCacheMode(QGraphicsView.CacheBackground)
# self.view.setDragMode(QGraphicsView.ScrollHandDrag)
self.scene = QGraphicsScene(self)
self.scene.addPixmap(background)
global scene
scene = self.scene
self.view.dialog = self
self.view.setScene(self.scene)
self.prevPoint = QPoint()
self.lastStamp = -1
buttonLayout = QVBoxLayout()
for text, slot in (
("&Tag", self.addTag),
("Align &bottom", self.alignBottom),
("Align &left", self.alignLeft),
("&Save", self.save),
("&Quit", self.accept)):
button = QPushButton(text)
button.clicked.connect(slot)
if not MAC:
button.setFocusPolicy(Qt.NoFocus)
self.lineedit.returnPressed.connect(self.updateUi)
if text == "&Save":
buttonLayout.addStretch(5)
if text == "&Quit":
buttonLayout.addStretch(1)
buttonLayout.addWidget(button)
buttonLayout.addStretch()
size = background.size() / background.devicePixelRatioF()
self.view.resize(size.width(), size.height())
self.scene.setSceneRect(0, 0, size.width(), size.height())
self.view.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
layout = QHBoxLayout()
layout.addWidget(self.view, 1)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.setWindowTitle(AppName)
info_name = self.filename + "-" + TAG + ".txt"
if os.path.exists(info_name):
for tag, x, y in [line.strip().split("\t") for line in open(info_name, "rt").readlines()]:
self.addTag(int(tag), QPointF(int(x), int(y)), adjust_position=False)
global Dirty; Dirty=False
self.show()
self.raise_()
示例5: GraphicsPixmapWidget
# 需要导入模块: from AnyQt.QtGui import QPixmap [as 别名]
# 或者: from AnyQt.QtGui.QPixmap import size [as 别名]
class GraphicsPixmapWidget(QGraphicsWidget):
"""
A QGraphicsWidget displaying a QPixmap
"""
def __init__(self, pixmap=None, parent=None):
super().__init__(parent)
self.setCacheMode(QGraphicsItem.ItemCoordinateCache)
self._pixmap = QPixmap(pixmap) if pixmap is not None else QPixmap()
self._keepAspect = True
self._crop = False
self._subset = True
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
def setPixmap(self, pixmap):
self._pixmap = QPixmap(pixmap)
self.updateGeometry()
self.update()
def pixmap(self):
return QPixmap(self._pixmap)
def setKeepAspectRatio(self, keep):
if self._keepAspect != keep:
self._keepAspect = bool(keep)
self.update()
def keepAspectRatio(self):
return self._keepAspect
def crop(self):
return self._crop
def setCrop(self, crop):
if self._crop != crop:
self._crop = bool(crop)
self.update()
def setSubset(self, in_subset):
if self._subset != in_subset:
self._subset = bool(in_subset)
self.update()
def setGeometry(self, rect):
self.prepareGeometryChange()
super().setGeometry(rect)
def sizeHint(self, which, constraint=QSizeF()):
if which == Qt.PreferredSize:
return QSizeF(self._pixmap.size())
else:
return QGraphicsWidget.sizeHint(self, which, constraint)
def paint(self, painter, option, widget=0):
if self._pixmap.isNull():
return
rect = self.contentsRect()
pixsize = QSizeF(self._pixmap.size())
aspectmode = (Qt.KeepAspectRatio if self._keepAspect else Qt.IgnoreAspectRatio)
if self._crop:
height, width = pixsize.height(), pixsize.width()
diff = abs(height - width)
if height > width:
y, x = diff / 2, 0
h, w = height - diff, width
else:
x, y = diff / 2, 0
w, h = width - diff, height
source = QRectF(x, y, w, h)
pixrect = QRectF(QPointF(0, 0), rect.size())
else:
source = QRectF(QPointF(0, 0), pixsize)
pixsize.scale(rect.size(), aspectmode)
pixrect = QRectF(QPointF(0, 0), pixsize)
if self._subset:
painter.setOpacity(1.0)
else:
painter.setOpacity(0.35)
pixrect.moveCenter(rect.center())
painter.save()
painter.setPen(QPen(QColor(0, 0, 0, 50), 3))
painter.drawRoundedRect(pixrect, 2, 2)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.drawPixmap(pixrect, self._pixmap, source)
painter.restore()