• Qt 重载QGraphicsItem的type()函数


    一、前言

      在QGraphicsScene 上绘制图形时,经常会使用items()这种便利函数,获取当前画布上所有的图形列表;因为绘制的时候,可能会绘制不同的图形,在得到所有的图形列表之后,通常需要对其中的 QGraphicsItem 进行类型检测,确定实际item的类型,然后对其进行类型转换得到正确的item的类型。这样既可以访问标准 item也可以 访问自定义 item。

      实际的运用案例:

     1  //获取画布上的所有图元
     2     QList<QGraphicsItem *> items = scene->items();
     3     //遍历画布上的所有图元
     4     for (int i = 0; i < items.count(); i++)
     5     {
     6         //获取图元的类型
     7         int type=items.at(i)->type();
     8  
     9         //矩形图元
    10         if(type==2)
    11         {
    12             qDebug()<<"矩形图元设置颜色";
    13  
    14              //转换成正确类型
    15             BRectangle *item=static_cast<BRectangle *>(CurrentSelectedItem_stu.item);
    16             QPen pen = item->pen();
    17             pen.setColor(text_color);
    18             item->setPen(pen);
    19             item->SetFrameColor(text_color);
    20         }
    21         //圆形图元
    22         else if(type==3)
    23         {
    24             qDebug()<<"圆形图元设置颜色";
    25  
    26             //转换成正确类型
    27             BEllipse *item=static_cast<BEllipse *>(CurrentSelectedItem_stu.item);
    28             QPen pen = item->pen();
    29             pen.setColor(text_color);
    30             item->setPen(pen);
    31             item->SetFrameColor(text_color);
    32         }
    33     }

      强制转换类型时要使用static_cast语法,常规C语言的强转语法QT会报一堆警告。

    1 语法:  static_cast<强转的目标类型>(待强转的变量);
    2  
    3 CTextItem *p=static_cast<CTextItem *>(CurrentSelectedItem_stu.item);

     

    二、 QGraphicsItem::Type介绍

      QGraphicsItem::Type 是标准 item 类中 virtual type() 函数返回的类型值。所有标准 item 与唯一的 Type 值相关联。

      QGraphicsItem::UserType 是自定义 itemQGraphicsItem 或任何标准 item 的子类的最小允许类型值。该值与 QGraphicsItem::type() 的重新实现结合使用并声明一个 Type 枚举值。

      QGraphicsItem里是这样定义的:

     1 class Q_WIDGETS_EXPORT QGraphicsItem
     2 {
     3 public:
     4  
     5   ......
     6   
     7   enum 
     8   {
     9     Type = 1,
    10     UserType = 65536
    11   };
    12   virtual int type() const;
    13  
    14   .......
    15 }

    三、重载 type()函数

      一般自己开发时,都会重载QGraphicsItem类,实现自己的类,完成对图形的统一管理,为了方便区分图形类型,就可以自己重载type()函数,完成自己的类型定义与区分。

    示例:

     1 // 自定义图元 - 基础类
     2 class BGraphicsItem : public QObject, public QAbstractGraphicsShapeItem
     3 {
     4     Q_OBJECT
     5  
     6 public:
     7     enum ItemType {
     8         Circle = 1,         //
     9         Ellipse,            // 椭圆
    10         Concentric_Circle,  // 同心圆
    11         Pie,                //
    12         Chord,              // 和弦
    13         Rectangle,          // 矩形
    14         Square,             // 正方形
    15         Polygon,            // 多边形
    16         Round_End_Rectangle,// 圆端矩形
    17         Rounded_Rectangle   // 圆角矩形
    18     };
    19  
    20     QPointF getCenter() { return m_center; }
    21     void setCenter(QPointF p) { m_center = p; }
    22  
    23     QPointF getEdge() { return m_edge; }
    24     void setEdge(QPointF p) { m_edge = p; }
    25  
    26     //获取类型
    27     ItemType getType() { return m_type; }
    28  
    29     //c++11中引入了override关键字,被override修饰的函数其派生类必须重载。
    30     //修饰需要被重写
    31     virtual int type() const override
    32     {
    33         return m_type;
    34     }
    35  
    36     //设置边框的颜色
    37     void SetFrameColor(QColor c);
    38 protected:
    39     BGraphicsItem(QPointF center, QPointF edge, ItemType type);
    40  
    41     virtual void focusInEvent(QFocusEvent *event) override;
    42     virtual void focusOutEvent(QFocusEvent *event) override;
    43  
    44  
    45 protected:
    46     QPointF m_center;
    47     QPointF m_edge;
    48     ItemType m_type;
    49     BPointItemList m_pointList;
    50  
    51     QPen m_pen_isSelected;
    52     QPen m_pen_noSelected;
    53 };

    重写type()函数时,需要加上override关键字,不然会有警告。

    warning: 'type' overrides a member function but is not marked 'override'

    note: overridden virtual function is here

    c++11中引入了override关键字,被override修饰的函数其派生类必须重载。

  • 相关阅读:
    Java批量文件打包下载
    Java Swing
    空白文章
    linux 用户创建、管理、权限分配
    在虚拟机下安装hadoop集成环境(centos7+hadoop-2.6.4+jdk-7u79)
    《转载》POI导出excel日期格式
    java导出生成word(类似简历导出)
    《sqoop实现hdfs中的数据导出至mysql数据库》
    c# winform 自动关闭messagebox 模拟回车
    Ubuntu下启动/重启/停止apache服务器
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/16613288.html
Copyright © 2020-2023  润新知