当前位置: 首页 > wzjs >正文

政府网站建设 重要性网站推广有哪些方式

政府网站建设 重要性,网站推广有哪些方式,网站信息报送制度建设,郑州网站制作公司一、概述 QGraphicsView 和 QGraphicsScene 是 Qt 图形视图框架的核心类,用于构建高性能、可交互的 2D 图形界面。 核心分工: QGraphicsScene:管理场景中的图形项(QGraphicsItem),处理事件和坐标系统。QG…
一、概述

QGraphicsViewQGraphicsScene 是 Qt 图形视图框架的核心类,用于构建高性能、可交互的 2D 图形界面。
核心分工

  • QGraphicsScene:管理场景中的图形项(QGraphicsItem),处理事件和坐标系统。
  • QGraphicsView:作为观察场景的视口,提供缩放、平移、旋转等视图变换功能。

适用场景

  • 复杂绘图(如 CAD 工具)
  • 游戏开发(2D 场景)
  • 数据可视化(图表、流程图)
  • 交互式图形界面(可拖拽、编辑的组件)

二、核心组件与关系
  1. 组件层级

    QGraphicsView (视图)└── QGraphicsScene (场景)└── QGraphicsItem (图形项:矩形、椭圆、文本、自定义项等)
    
  2. 坐标系差异

    • 场景坐标:场景的全局坐标系(原点在场景中心或自定义位置)。
    • 视图坐标:视图窗口的坐标系(原点在左上角)。
    • 项坐标:每个图形项自身的局部坐标系。

三、基础使用步骤
  1. 创建场景与视图

    from PyQt6.QtWidgets import QGraphicsView, QGraphicsScene, QApplication
    from PyQt6.QtCore import Qtscene = QGraphicsScene()  # 创建场景
    view = QGraphicsView(scene)  # 创建视图并绑定场景
    view.setRenderHint(QPainter.RenderHint.Antialiasing)  # 抗锯齿
    view.resize(800, 600)
    view.show()
    
  2. 添加图形项到场景

    # 添加矩形(位置、大小、颜色)
    rect = scene.addRect(0, 0, 100, 50, Qt.GlobalColor.red, Qt.GlobalColor.blue)# 添加文本
    text = scene.addText("Hello Graphics", QFont("Arial", 12))
    text.setPos(50, 50)# 添加椭圆
    ellipse = scene.addEllipse(200, 100, 80, 60, Qt.GlobalColor.green)
    

四、核心功能与实战案例
  1. 交互式图形项(拖拽、旋转)

    class MovableRect(QGraphicsRectItem):def __init__(self, x, y, w, h):super().__init__(x, y, w, h)self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable)  # 允许拖拽self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)  # 允许选中self.setBrush(Qt.GlobalColor.cyan)# 添加可移动矩形到场景
    movable_rect = MovableRect(300, 200, 80, 40)
    scene.addItem(movable_rect)
    
  2. 视图操作(缩放与平移)

    # 鼠标滚轮缩放
    def wheelEvent(self, event):factor = 1.2 if event.angleDelta().y() > 0 else 0.8self.scale(factor, factor)# 右键拖拽平移
    view.setDragMode(QGraphicsView.DragMode.ScrollHandDrag)  # 设置拖拽模式
    
  3. 自定义图形项(绘制箭头)

    class ArrowItem(QGraphicsItem):def boundingRect(self):return QRectF(-10, -5, 20, 10)  # 定义项边界def paint(self, painter, option, widget):painter.setPen(QPen(Qt.GlobalColor.black, 2))painter.drawLine(0, 0, 10, 0)  # 箭头主体painter.drawLine(10, 0, 5, -5)  # 箭头尖端painter.drawLine(10, 0, 5, 5)arrow = ArrowItem()
    arrow.setPos(400, 300)
    scene.addItem(arrow)
    
  4. 动画与图形项结合

    # 使用 QPropertyAnimation 移动图形项
    from PyQt6.QtCore import QPropertyAnimationanim = QPropertyAnimation(arrow, b"pos")
    anim.setDuration(2000)
    anim.setStartValue(QPointF(400, 300))
    anim.setEndValue(QPointF(500, 400))
    anim.setEasingCurve(QEasingCurve.Type.InOutQuad)
    anim.start()
    

五、高级功能
  1. 碰撞检测

    # 检测矩形与其他项的碰撞
    colliding_items = rect.collidingItems()
    for item in colliding_items:item.setBrush(Qt.GlobalColor.yellow)  # 高亮碰撞项
    
  2. 组合项(QGraphicsItemGroup)

    group = QGraphicsItemGroup()
    group.addToGroup(rect)
    group.addToGroup(text)
    group.setRotation(45)  # 整体旋转 45 度
    scene.addItem(group)
    
  3. 场景事件处理

    class CustomScene(QGraphicsScene):def mousePressEvent(self, event):if event.button() == Qt.MouseButton.LeftButton:print(f"Scene 点击位置:{event.scenePos()}")super().mousePressEvent(event)
    

六、注意事项
  1. 性能优化

    • 避免在场景中放置过多项(超过数千个)。
    • 使用 QGraphicsItem.ItemClipsToShapesetCacheMode 优化渲染。
  2. 坐标转换

    • 使用 mapToScene()mapFromScene() 在视图、场景、项之间转换坐标。
    # 将视图坐标 (100, 200) 转换为场景坐标
    scene_pos = view.mapToScene(100, 200)
    
  3. 内存管理

    • 删除图形项时需调用 removeItem(),避免内存泄漏。
    scene.removeItem(rect)
    del rect  # 显式删除对象
    

七、综合案例:简易绘图工具
class DrawingScene(QGraphicsScene):def __init__(self):super().__init__()self.current_item = Nonedef mousePressEvent(self, event):if event.button() == Qt.MouseButton.LeftButton:self.current_item = QGraphicsEllipseItem()self.current_item.setRect(event.scenePos().x(), event.scenePos().y(), 0, 0)self.addItem(self.current_item)def mouseMoveEvent(self, event):if self.current_item:start_pos = event.buttonDownScenePos(Qt.MouseButton.LeftButton)current_pos = event.scenePos()self.current_item.setRect(start_pos.x(), start_pos.y(),current_pos.x() - start_pos.x(),current_pos.y() - start_pos.y())def mouseReleaseEvent(self, event):self.current_item = None# 使用示例
app = QApplication([])
scene = DrawingScene()
view = QGraphicsView(scene)
view.show()
app.exec()

八、总结

QGraphicsView 和 QGraphicsScene 为复杂图形应用提供了强大支持,通过组合图形项、处理事件和优化渲染,可实现高度定制化的交互式界面。开发时需重点关注坐标系统、性能管理和用户交互逻辑。

http://www.dtcms.com/wzjs/481807.html

相关文章:

  • 做山西杂粮的网站优化师
  • 想做淘宝 网站怎么做外链发布平台大全
  • bootstrap响应式网站2021全国大学生营销大赛
  • 如何提升网站的权重个人如何做网络推广
  • 关于建设工程招标的网站流量平台有哪些
  • 代理分佣后台网站开发微信推广
  • 大家都在哪些网站做宣传站长之家权重
  • 一个网站如何产生流量网站优化网络推广seo
  • 工商网站官网查询网站建设技术托管
  • 广东网站设计工具站长工具是干嘛的
  • 怎么做百度联盟网站一键关键词优化
  • 河北提供网站制作公司哪家专业广告联盟推广
  • 个人做众筹网站合法吗网络营销比较好的企业
  • 公司网站后如何更新市场调研报告ppt模板
  • 沈阳网站建设报价抖音seo排名软件哪个好
  • 网站刷链接怎么做seo关键词首页排名代发
  • wordpress输出tags保定seo推广外包
  • 网站建设预付流程seo从0到1怎么做
  • 合肥设计网站济南网站推广
  • 广州b2b网站建设网络推广公司有哪些
  • 百度为何不收录你的网站产品页网站做优化
  • 北京市建筑工程装饰集团有限公司深圳网站seo地址
  • 门店到什么地步可以做网站班级优化大师免费下载学生版
  • 网站上传大文件百度关键词刷搜索量
  • 深达网站制作深圳公司最新网域查询入口
  • 青岛做网站公司排名广告营销推广方案
  • 沈阳微网站制作seo关键词有哪些类型
  • 网站建设吸引人的话语徐州seo代理计费
  • 河南汝州文明建设门户网站百度账号登录入口
  • 装饰行业做网站朋友圈广告