QT框架提供的强制类型转换方法qobject_cast qgraphicsitem_cast

0 1579
索鸟 2020-08-10
需要:0索币

在C++开发中经常要进行数据类型的强制转换。

刚开始学习的时候,直接对基本数据类型强制类型转换,如float fnum = 3.14; int num = (int)fnum;

随着C++标准的发展,又提供了dynamic_cast、const_cast 、static_cast、reinterpret_cast等高级安全的强制转换方法。

dynamic_cast: 通常在基类和派生类之间转换时使用,run-time cast。
const_cast: 主要针对const和volatile的转换。
static_cast: 一般的转换,no run-time check.通常,如果你不知道该用哪个,就用这个。
reinterpret_cast: 用于进行没有任何关联之间的转换,比如一个字符指针转换为一个整形数。

QT框架提供的强制类型转换方法:

qobject_cast  qobject_cast()函数的行为类似于标准C ++ dynamic_cast(),其优点是不需要RTTI支持,并且它可以跨动态库边界工作。

QObject *obj = new QTimer;          // QTimer inherits QObject

QTimer *timer = qobject_cast<QTimer *>(obj);

// timer == (QObject *)obj

QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);

// button == 0

qgraphicsitem_cast  场景视图Item类转换

QGraphicsScene和 QGraphicsItem 的大多数便利函数(例如:items(),selectedItems()、collidingItems()、childItems())返回一个 QList<QGraphicsItem *> 列表,在遍历列表的时候,通常需要对其中的 QGraphicsItem 进行类型检测与转换,以确定实际的 item。

以下代码出处:https://blog.csdn.net/liang19890820/article/details/53612446

QList<QGraphicsItem *> items = scene->items();

foreach (QGraphicsItem *item, items) {

    if (item->type() == QGraphicsRectItem::Type) {        // 矩形

        QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem*>(item);

        // 访问 QGraphicsRectItem 的成员

    } else if (item->type() == QGraphicsLineItem::Type) {      // 直线

        QGraphicsLineItem *line = qgraphicsitem_cast<QGraphicsLineItem*>(item);

        // 访问 QGraphicsLineItem 的成员

    } else if (item->type() == QGraphicsProxyWidget::Type) {    // 代理 Widget

        QGraphicsProxyWidget *proxyWidget = qgraphicsitem_cast<QGraphicsProxyWidget*>(item);

        QLabel *label = qobject_cast<QLabel *>(proxyWidget->widget());

        // 访问 QLabel 的成员

    } else if (item->type() == CustomItem::Type) {         // 自定义 Item

        CustomItem *customItem = qgraphicsitem_cast<CustomItem*>(item);

        // 访问 CustomItem 的成员

    } else {

        // 其他类型 item

    }

}

需要注意的是,为了使该函数正确使用自定义Item,需要在QGraphicsItem子类中重写type()函数才行。

class CustomItem : public QGraphicsItem

{

  public:

     enum { Type = UserType + 1 };

     int type() const override

     {

         // Enable the use of qgraphicsitem_cast with this item.

         return Type;

     }

     ...

};

qvariant_cast  QVariant类型转换为实际的类型

Returns the given value converted to the template type T.

This function is equivalent to QVariant::value().

回帖
  • 消灭零回复
相关主题
2020年最新最新Kubernetes视频教程(K8s)教程 2
程序员转型之制作网课变现,月入过万告别996 1
索鸟快传2.0发布啦 1
两个不同网络的电脑怎么实现文件的互相访问呢? 1
网盘多账号登录软件 1
Java实战闲云旅游项目基于vue+element-ui 1
单点登录技术解决方案基于OAuth2.0的网关鉴权RSA算法生成令牌 1
QT5获取剪贴板上文本信息QT设置剪贴板内容 1
springboot2实战在线购物系统电商系统 1
python web实战之爱家租房项目 1
windows COM实用入门教程 1
C++游戏开发之C++实现的水果忍者游戏 1
计算机视觉库opencv教程 1
node.js实战图书管理系统express框架实现 1
C++实战教程之远程桌面远程控制实战 1
相关主题
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0