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

qt QGraphicsEllipseItem详解

1、概述

QGraphicsEllipseItem是Qt框架中QGraphicsItem的一个子类,它提供了一个可以添加到QGraphicsScene中的椭圆项。QGraphicsEllipseItem表示一个带有填充和轮廓的椭圆,也可以用于表示椭圆段(通过startAngle()和spanAngle()方法)。它允许你设置椭圆的几何形状、颜色、边框等属性,并可以响应各种事件,如鼠标点击、移动等。QGraphicsEllipseItem是Qt图形视图框架(Graphics View Framework)的一部分,该框架提供了一个用于2D图形项的高效视图和场景架构。

2、重要方法

QGraphicsEllipseItem提供了一系列方法来设置和控制椭圆的属性。以下是一些重要的方法:

  • QGraphicsEllipseItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr):构造函数,创建一个椭圆项,指定其外接矩形的左上角坐标(x, y)和宽度(width)及高度(height)。
  • QGraphicsEllipseItem(const QRectF &rect, QGraphicsItem *parent = nullptr):构造函数,创建一个椭圆项,指定其外接矩形rect。
  • QGraphicsEllipseItem(QGraphicsItem *parent = nullptr):构造函数,创建一个椭圆项,不指定其外接矩形,可以在之后通过setRect()方法设置。
  • void setRect(const QRectF &rect):设置椭圆的外接矩形。
  • void setRect(qreal x, qreal y, qreal width, qreal height):设置椭圆的外接矩形的左上角坐标和尺寸。
  • QRectF rect() const:返回椭圆的外接矩形。
  • void setSpanAngle(int angle):设置椭圆段的扫过角度。
  • int spanAngle() const:返回椭圆段的扫过角度。
  • void setStartAngle(int angle):设置椭圆段的起始角度。
  • int startAngle() const:返回椭圆段的起始角度。
  • void setPen(const QPen &pen):设置椭圆的边框。
  • void setBrush(const QBrush &brush):设置椭圆的填充颜色或图案。

此外,QGraphicsEllipseItem还继承了QGraphicsItem的一些方法,如boundingRect()、shape()、contains()、paint()等,这些方法用于定义椭圆的边界、形状、包含关系以及绘制方式。

3、重要信号

QGraphicsEllipseItem本身并没有定义特定的信号,但它继承了QGraphicsItem的信号。这些信号通常与项目的交互和状态变化相关,如鼠标事件(mousePressEvent、mouseMoveEvent、mouseReleaseEvent等)、键盘事件(keyPressEvent、keyReleaseEvent等)、选择状态变化(selectionChanged)等。你可以通过连接这些信号到相应的槽函数来响应这些事件。

4、常用枚举类型

QGraphicsEllipseItem并没有定义自己的枚举类型,但它继承了QGraphicsItem的一些枚举类型。这些枚举类型通常用于控制项目的行为,如项目的可见性(QGraphicsItem::ItemIsVisible)、项目的可移动性(QGraphicsItem::ItemIsMovable)、项目的可选择性(QGraphicsItem::ItemIsSelectable)等。你可以通过调用QGraphicsItem的setFlag()方法来设置这些标志。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsEllipseItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Create a scene
    QGraphicsScene *scene = new QGraphicsScene();
    
    // Create an ellipse item
    QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem();
    ellipse->setRect(0, 0, 100, 60);  // Set the ellipse dimensions (x, y, width, height)
    ellipse->setBrush(QBrush(Qt::blue));  // Set fill color
    ellipse->setPen(QPen(Qt::red, 2));    // Set border color and width
    
    // Add the ellipse to the scene
    scene->addItem(ellipse);
    
    // Create a view to display the scene
    QGraphicsView *view = new QGraphicsView(scene);
    view->setRenderHint(QPainter::Antialiasing);  // Enable antialiasing for smoother rendering
    view->setSceneRect(-200, -200, 400, 400);     // Set the visible area
    view->resize(400, 400);                       // Set the window size
    
    // Show the view
    view->show();
    
    return app.exec();
}

觉得有帮助的话,打赏一下呗。。

           

需要商务合作(定制程序)的欢迎私信!! 


http://www.kler.cn/a/417548.html

相关文章:

  • 如何理解多态,以及由此引出的抽象类和纯虚函数
  • 【重生之学习C语言----杨辉三角篇】
  • 网络爬虫js逆向之某音乐平台案例
  • 每日Attention学习18——Grouped Attention Gate
  • ASP.NET Core 中使用依赖注入 (DI) 容器获取并执行自定义服务
  • neo4j-在Linux中安装neo4j
  • 电气火灾式故障电弧探测器在某医院照明回路中的应用
  • 第七课 Unity编辑器创建的资源优化_UI篇(UGUI)
  • Java中TimedCache缓存对象的详细使用
  • 力扣--LCR 149.彩灯装饰记录I
  • RAG数据拆分之PDF
  • Java Stream reduce 函数,聚合数据
  • html 中的 <code>标签
  • uniapp的video组件截图(抓拍)功能,解决截后为黑图bug
  • MySQL中的锁与MVCC
  • 【Ansible】自动化运维工具
  • Kafka知识体系
  • Python面向对象编程与模块化设计练习
  • 【Linux】管道
  • RAT:融合RAG和CoT的高效多步推理任务策略
  • 【ROS2】ROS2 Hello World (C++实现)
  • Keras构建卷积神经网络
  • 48-基于单片机的LCD12864时间调控和串口抱站
  • Oracle对比表与表之间的结构
  • 【Lucene】单个cpu 每秒能支持多少个bm25公式的计算
  • Apache Flink从Kafka中消费商品数据,并进行商品分类的数量统计题