From 39fe35e309b4b45e7ae072462366c8d21150744f Mon Sep 17 00:00:00 2001 From: Irony <892768447@qq.com> Date: Thu, 26 Jul 2018 14:28:16 +0800 Subject: [PATCH] QGraphicsItem --- .settings/org.eclipse.core.resources.prefs | 2 + .../QGraphicsItem/Item拖拽改变大小.py | 100 ++++++++++++++++++ QGraphicsView练习/QGraphicsItem/Item移动.py | 63 +++++++++++ 3 files changed, 165 insertions(+) create mode 100644 QGraphicsView练习/QGraphicsItem/Item拖拽改变大小.py create mode 100644 QGraphicsView练习/QGraphicsItem/Item移动.py diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index cba2de7..3c35dea 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -11,6 +11,8 @@ encoding//PyQtChart\u7EC3\u4E60/test/LineChart\u81EA\u5B9A\u4E49xy\u8F74.py=utf- encoding//PyQtChart\u7EC3\u4E60/test/ToolTip.py=utf-8 encoding//PyQtChart\u7EC3\u4E60/test/ToolTip2.py=utf-8 encoding//QGraphicsView\u7EC3\u4E60/ImageView.py=utf-8 +encoding//QGraphicsView\u7EC3\u4E60/QGraphicsItem/Item\u62D6\u62FD\u6539\u53D8\u5927\u5C0F.py=utf-8 +encoding//QGraphicsView\u7EC3\u4E60/QGraphicsItem/Item\u79FB\u52A8.py=utf-8 encoding//QGraphicsView\u7EC3\u4E60/\u4E16\u754C\u5730\u56FE/WorldMap.py=utf-8 encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8 encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImagePs.py=utf-8 diff --git a/QGraphicsView练习/QGraphicsItem/Item拖拽改变大小.py b/QGraphicsView练习/QGraphicsItem/Item拖拽改变大小.py new file mode 100644 index 0000000..cbfcf82 --- /dev/null +++ b/QGraphicsView练习/QGraphicsItem/Item拖拽改变大小.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年7月26日 +@author: Irony +@site: https://github.com/892768447 +@email: 892768447@qq.com +@file: QGraphicsView练习.QGraphicsItem.Item移动 +@description: +""" +from PyQt5.QtCore import Qt, QLineF +from PyQt5.QtGui import QPainter, QColor, QPen +from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsView, QGraphicsScene,\ + QStyle + + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = "Copyright (c) 2018 Irony" +__Version__ = "Version 1.0" + + +class MoveableItem(QGraphicsRectItem): + + def __init__(self, *args, **kwargs): + super(MoveableItem, self).__init__(*args, **kwargs) + # 可移动,可选择,有焦点 + self.setFlags(self.ItemIsMovable | + self.ItemIsSelectable | self.ItemIsFocusable) + # 设置接收悬停事件 + self.setAcceptHoverEvents(True) + self.setBrush(QColor(247, 160, 57)) # 设置背景颜色 + + # 是否在调整大小的状态 + self.isResizing = False + + def paint(self, painter, option, widget): + super(MoveableItem, self).paint(painter, option, widget) + # 当鼠标选中后在边缘绘制边框 + if option.state & QStyle.State_Selected: + rect = self.boundingRect() + painter.setRenderHint(QPainter.Antialiasing, True) # 抗锯齿 + if option.state & QStyle.State_HasFocus: # 有焦点 + painter.setPen(QPen(Qt.red, 2)) # 设置红色画笔 + painter.drawLines( # 在左上、左下、右上、右下画8条小线段 + QLineF(rect.x(), rect.y(), rect.x()+10, rect.y()), # 左上顶点向右 + QLineF(rect.x(), rect.y(), rect.x(), rect.y()+10), # 左上顶点向下 + + QLineF(rect.x(), rect.y()+rect.height(), rect.x()+10, rect.y()+rect.height()), # 左下顶点向右 + QLineF(rect.x(), rect.y()+rect.height(), rect.x(), rect.y()+rect.height() - 10), # 左下顶点向上 + + QLineF(rect.width(), rect.y(), rect.width() - 10, rect.y()), # 右上顶点向左 + QLineF(rect.width(), rect.y(), rect.width(), rect.y()+10), # 右上顶点向下 + + QLineF(rect.width(), rect.height(), + rect.width() - 10, rect.height()), # 右下顶点向左 + QLineF(rect.width(), rect.height(), rect.width(), + rect.height() - 10) # 右下顶点向上 + ) + + def hoverMoveEvent(self, event): + super(MoveableItem, self).hoverMoveEvent(event) + + +class ImageWidget(QGraphicsView): + + def __init__(self, *args, **kwargs): + super(ImageWidget, self).__init__(*args, **kwargs) + self.resize(800, 600) + # 设置背景颜色 + self.setBackgroundBrush(QColor(31, 31, 47)) + # 去掉滚动条 +# self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) +# self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + # 设置变换中心为鼠标所在位置 + self.setTransformationAnchor(self.AnchorUnderMouse) + # 不保证painter的状态 + self.setOptimizationFlags(self.DontSavePainterState) + self.setViewportUpdateMode(self.SmartViewportUpdate) + self.setRenderHints(QPainter.Antialiasing | + QPainter.SmoothPixmapTransform) + + # 场景 + self._scene = QGraphicsScene(0, 0, self.width(), self.height()) + self.setScene(self._scene) + + self._scene.addItem(MoveableItem(100, 100, 200, 200)) + + +if __name__ == '__main__': + import sys + import cgitb + sys.excepthook = cgitb.Hook(1, None, 5, sys.stderr, 'text') + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = ImageWidget() + w.show() + sys.exit(app.exec_()) diff --git a/QGraphicsView练习/QGraphicsItem/Item移动.py b/QGraphicsView练习/QGraphicsItem/Item移动.py new file mode 100644 index 0000000..07fbc84 --- /dev/null +++ b/QGraphicsView练习/QGraphicsItem/Item移动.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2018年7月26日 +@author: Irony +@site: https://github.com/892768447 +@email: 892768447@qq.com +@file: QGraphicsView练习.QGraphicsItem.Item移动 +@description: +""" +from PyQt5.QtGui import QPainter, QColor +from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsView, QGraphicsScene + + +__Author__ = """By: Irony +QQ: 892768447 +Email: 892768447@qq.com""" +__Copyright__ = "Copyright (c) 2018 Irony" +__Version__ = "Version 1.0" + + +class MoveableItem(QGraphicsRectItem): + + def __init__(self, *args, **kwargs): + super(MoveableItem, self).__init__(*args, **kwargs) + self.setFlag(self.ItemIsMovable) # 设置为可以动 + self.setBrush(QColor(247, 160, 57)) # 设置背景颜色 + + +class ImageWidget(QGraphicsView): + + def __init__(self, *args, **kwargs): + super(ImageWidget, self).__init__(*args, **kwargs) + self.resize(800, 600) + # 设置背景颜色 + self.setBackgroundBrush(QColor(31, 31, 47)) + # 去掉滚动条 +# self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) +# self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + # 设置变换中心为鼠标所在位置 + self.setTransformationAnchor(self.AnchorUnderMouse) + # 不保证painter的状态 + self.setOptimizationFlags(self.DontSavePainterState) + self.setViewportUpdateMode(self.SmartViewportUpdate) + self.setRenderHints(QPainter.Antialiasing | + QPainter.SmoothPixmapTransform) + + # 场景 + self._scene = QGraphicsScene() + self.setScene(self._scene) + self.setSceneRect(0, 0, self.width(), self.height()) + + self._scene.addItem(MoveableItem(100, 100, 200, 200)) + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = ImageWidget() + w.show() + sys.exit(app.exec_())