这里面讲了Qt 的类图root: QObject的一些信息。

Wentao Sun.

Qt Object model

The standard C++ object model provides very efficient runtime support for the object paradigm. But its static nature is inflexibile in certain problem domains. Graphical user interface programming is a domain that requires both runtime efficiency and a high level of flexibility. Qt provides this, by combining the speed of C++ with the flexibility of the Qt Object Model.

Qt adds these features to C++:

  • a very powerful mechanism for seamless object communication called signals and slots
  • queryable and designable object properties
  • powerful events and event filters
  • contextual string translation for internationalization
  • sophisticated interval driven timers that make it possible to elegantly integrate many tasks in an event-driven GUI
  • hierarchical and queryable object trees that organize object ownership in a natural way
  • guarded pointers (QPointer) that are automatically set to 0 when the referenced object is destroyed, unlike normal C++ pointers which become dangling pointers when their objects are destroyed
  • a dynamic cast that works across library boundaries.

Many of these Qt features are implemented with standard C++ techniques, based on inheritance from QObject. Others, like the object communication mechanism and the dynamic property system, require the Meta-Object System provided by Qt's own Meta-Object Compiler (moc).

The meta-object system is a C++ extension that makes the language better suited to true component GUI programming. Although templates can be used to extend C++, the meta-object system provides benefits using standard C++ that cannot be achieved with templates.

Detailed Description

The QObject class is the base class of all Qt objects.

QObject is the heart of the Qt object model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect(). To avoid never ending notification loops you can temporarily block signals with blockSignals(). The protected functions connectNotify() and disconnectNotify() make it possible to track connections.

QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object i.e. it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().

Every object has an objectName() and its class name can be found via the corresponding metaObject() (see QMetaObject::className()). You can determine whether the object's class inherits another class in the QObject inheritance hierarchy by using the inherits() function.

When an object is deleted, it emits a destroyed() signal. You can catch this signal to avoid dangling references to QObjects.

QObjects can receive events through event() and filter the events of other objects. See installEventFilter() and eventFilter() for details. A convenience handler, childEvent(), can be reimplemented to catch child events.

Events are delivered in the thread in which the object was created; see Thread Support in Qt and thread() for details. Note that event processing is not done at all for QObjects with no thread affinity (thread() returns zero). Use the moveToThread() function to change the thread affinity for an object and its children (the object cannot be moved if it has a parent).

Last but not least, QObject provides the basic timer support in Qt; see QTimer for high-level support for timers.

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than qobject_cast<QWidget *>(obj) or obj->inherits("QWidget").

Some QObject functions, e.g. children(), return a QObjectList. QObjectList is a typedef for QList<QObject *>.

Implicitly Shared Classes

Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.

  • Overview
  • Implicit Sharing in Detail
  • List of Classes

Overview

A shared class consists of a pointer to a shared data block that contains a reference count and the data.

When a shared object is created, it sets the reference count to 1. The reference count is incremented whenever a new object references the shared data, and decremented when the object dereferences the shared data. The shared data is deleted when the reference count becomes zero.

When dealing with shared objects, there are two ways of copying an object. We usually speak about deep and shallow copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e. just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.

Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.

The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.

Implicit sharing takes place behind the scenes; the programmer does not need to worry about it. Even in multithreaded applications, implicit sharing takes place, as explained in Threads and Implicit Sharing.

Implicit Sharing in Detail

Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called copy-on-write or value semantics.)

An implicitly shared class has total control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data.

The QPen class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.

Code fragment:

 void QPen::setStyle(Qt::PenStyle style)
 {
     detach();           // detach from common data
     d->style = style;   // set the style member
 }
 void QPen::detach()
 {
     if (d->ref != 1) {
         ...             // perform a deep copy
     }
 } 

转载于:https://www.cnblogs.com/SunWentao/archive/2008/10/04/1303955.html

Qt ObjectModel (from Qt doc)相关推荐

  1. 为什么要用Qt开发(Qt跨平台应用开发)

    一.为什么选择qt 1.跨平台,一次写代码多个平台编译.使跨平台应用达到各个平台原生应用的体验与流畅度.相比与java或c#在开发图形计算方面的软件时有非常好的使用体验与流畅度. 2.使用C++编程虽 ...

  2. [QT操作XML]QT读写XML文件,QT修改XML文件

    [QT操作XML]QT读写XML文件 XML简介 QT操作XML,写入.读取.修改 XML效果演示 XML简介 概念:Extensible Markup Language 可扩展标记语言(可扩展:标签 ...

  3. 01-为什么要用Qt开发(Qt跨平台应用开发)

    一.为什么选择qt 跨平台,一次写代码多个平台编译.使跨平台应用达到各个平台原生应用的体验与流畅度.相比与java或c#在开发图形计算方面的软件时有非常好的使用体验与流畅度. 使用C++编程虽然没有j ...

  4. 嵌入式Qt | 如何交叉编译Qt模块

    在硬件板卡提供商提供的嵌入式linux平台下的Qt环境中,很多时候会不太适合实际的开发场景: (1)可能一些Qt的模块不支持.这时候要么找厂家,要么自己编译. (2)在厂家提供的平台开发环境中,支持的 ...

  5. qt 程序异常结束。_博声医疗:QT间期,QT间期正常范围

    QT间期(简称QT)包括心室除极和复极激动时间,代表心室去极化和复极化过程的总时程,为自QRS波的起点至T波的终点所占的时间,测定值随年龄和性别而变化.据博声医疗了解到,QT间期与心率快慢有密切关系, ...

  6. Qt 6的Qt 3D会是什么样?

    翻译自What about Qt 3D in Qt 6? 原文作者:Sean Harmer 在Qt 6中,我们希望可以在很多方面对Qt 3D进行改进,本文将着重介绍几个主要方向:渲染器工作缓存和现代图 ...

  7. Qt Creator调试Qt Quick示例应用程序

    Qt Creator调试Qt Quick示例应用程序 调试Qt Quick示例应用程序 调试Qt Quick示例应用程序 Same Game演示展示了如何使用JavaScript编写所有游戏逻辑的QM ...

  8. Qt Creator调试Qt Quick项目

    Qt Creator调试Qt Quick项目 调试Qt Quick项目 设置QML调试 混合C ++ / QML调试 启动QML调试 调试JavaScript函数 设置断点 检查项目 检查用户界面 执 ...

  9. Qt Creator使用Qt Quick工具栏

    Qt Creator使用Qt Quick工具栏 使用Qt Quick工具栏 预览影像 格式化文字 预览动画 编辑矩形 使用Qt Quick工具栏 当您在代码中选择QML类型并且工具栏可用时,将出现一个 ...

最新文章

  1. android的五大布局(layout)
  2. 数字图像缩放之最近邻插值与双线性插值处理效果对比
  3. linux服务器性能监控命令汇总(一)
  4. Java面试题库,mysql远程访问权限设置
  5. 4. 寻找两个有序数组的中位数
  6. LibLinear(SVM包)使用说明之(一)README
  7. STL:priority_queue
  8. imu与gps之间的时间戳_一个时间戳精度问题,引发了一个MySQL血案
  9. 使用 Tye 辅助开发 k8s 应用竟如此简单(三)
  10. 复习Python DB-API
  11. 买房一定要知道的购房误区 买涨不买跌的心态可能得改
  12. cuda linux 算力_华为AI再进化,CANN 3.0释放算力狂魔
  13. 小白转前端,学习哪些知识点才能不走弯路?
  14. linux服务器学习笔记:如何使用密钥认证机制远程登录linux?
  15. 分内外网,下载个东西真不方便,一肚子火
  16. tcpdump抓取无效TCP标志数据包表达式
  17. 2921年3月计算机二级office,国家二级计算机考试office怎么报名
  18. 梦网云科技的短信接口怎么样,真实体验
  19. 德怀特·艾森豪威尔(1890-1969)美国第34任总统,陆军五星上将。
  20. python远程聊天_python工具,微信聊天、自动回复、手机微信远程控制电脑

热门文章

  1. Qt C++ 检测优盘插入或拔出
  2. python使用matplotlib绘图sigmoid_使用matplotlib库绘制函数图
  3. 怎么设置某个用户生成hdfs文件的权限_HDFS简明入门教程
  4. 经济学中的定量分析python_(转)Python中的结构化数据分析利器-Pandas简介
  5. 一个域名可以绑定多个公众号_如何在同一个浏览器中同时登录多个公众号?
  6. Linux C : GDB调试命令汇总
  7. Cpp 11 / override 和 final 区别
  8. miui9Android8.0更新,MIUI9 暂停更新工作,或为 MIUI 10 将至?
  9. 了不起的开发者 丨 有奖征文活动来啦!
  10. HugeGraph图数据库获Apache TinkerPop官方认证