Qt的对象模型与信号槽的概念

上传人:876****10 文档编号:147297441 上传时间:2020-10-08 格式:PPT 页数:60 大小:1.69MB
返回 下载 相关 举报
Qt的对象模型与信号槽的概念_第1页
第1页 / 共60页
Qt的对象模型与信号槽的概念_第2页
第2页 / 共60页
Qt的对象模型与信号槽的概念_第3页
第3页 / 共60页
Qt的对象模型与信号槽的概念_第4页
第4页 / 共60页
Qt的对象模型与信号槽的概念_第5页
第5页 / 共60页
点击查看更多>>
资源描述

《Qt的对象模型与信号槽的概念》由会员分享,可在线阅读,更多相关《Qt的对象模型与信号槽的概念(60页珍藏版)》请在金锄头文库上搜索。

1、.,Qt的对象模型 和信号槽 的概念,Qt in Education,This work is a Chinese translation of the original Qt Educational Training Materials published by Nokia: 2010 Nokia Corporation and its Subsidiary(-ies). Nokia, Qt and the Nokia and Qt logos are the registered trademarks of Nokia Corporation in Finland and other co

2、untries worldwide. This translation was created by Communication and Computer Network Laboratory of Guangdong Province, South China University of Technology. 2010 Communication and Computer Network Laboratory of Guangdong Province, South China University of Technology. The enclosed Qt Educational Tr

3、aining Materials are provided under the Creative Commons Attribution-Non-Commercial-Share Alike 2.5 License Agreement. The full license text is available here: .creativecommons.org/licenses/by-nc-sa/2.5/legalcode.,此文档内容是由诺基亚公司发布的原创Qt教育培训文档的中文翻译: 2010诺基亚公司及其附属公司。 Nokia (诺基亚),Qt以及Nokia与Qt商标是Nokia公司在芬兰

4、和全球其他国家的注册商标。 该翻译版本由 华南理工大学广东省计算机网络重点实验室 创造。 2010 华南理工大学广东省计算机网络重点实验室 本Qt 教育培训材料依照署名-非商业性使用-相同方式共享 2.5许可协议(Creative Commons Attribution-Non-Commercial-Share Alike 2.5 License Agreement)发布。 完整的许可证文本可以在这里找到:.creativecommons.org/licenses/by-nc-sa/2.5/legalcode。,QObject类,QObject是几乎所有Qt类和所有部件(widget)的基类。

5、 它包含很多组成Qt的机制 事件 信号和槽 属性 内存管理,QObject类,QObject 是大部分Qt 类的基类 例外的例子是: 类需要作为轻量级的类,例如图元(graphical primitives)。 数据容器(QString, QList, QChar等) 需要可复制的类,因为QObject类是无法被复制的。,QObject类,它们可以拥有一个名字 (QObject:objectName) 它们被放置在QObject实例的一个层次上 它们可以有到其他 QObject 实例的联接 例子: 在运行时复制一个部件有意义吗?,“QObject 的实例是单独的!”,元数据(Meta data

6、),Qt用C+实现内省 每一个 QObject 都有一个元对象 元对象涉及: 类名 (QObject:className) 继承 (QObject:inherits) 属性 信号和槽 普通信息(QObject:classInfo),元数据,元数据通过元对象编译器(moc)在编译时组合在一起。,sources *.cpp,executables,object files *.o,headers *.h,普通的C+生成过程,includes,compiles,links,元数据Meta data,元数据通过元对象编译器(moc)在编译时组合在一起。 moc从头文件里面获得数据。,sources *

7、.cpp,executables,object files *.o,headers *.h,generated moc_*.cpp,Qt C+ 生成过程,includes,compiles,links,compiles,mocs,元数据,moc 找什么?,class MyClass : public QObject Q_OBJECT Q_CLASSINFO(author, John Doe) public: MyClass(const Foo ,内省(Introspection),类在运行时了解它们自己的信息 对实现脚本和动态语言的绑定 有很好的支持。,if (object-inherits(

8、QAbstractItemView) QAbstractItemView *view = static_cast(widget); view-. enum CapitalsEnum Oslo, Helsinki, Stockholm, Copenhagen ; int index = object-metaObject()-indexOfEnumerator(CapitalsEnum); object-metaObject()-enumerator(index)-key(object-capital();,属性(Properties),QObject有getter和setter函数属性 命名策

9、略: color, setColor 对于布尔: isEnabled, setEnabled,class QLabel : public QFrame Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) public: QString text() const; public slots: void setText(const QString ,属性,为什么使用setter 函数? 可以验证设置 对可能的变化作出反应,void setMin( int newMin ) if( newMin m_max ) qWarning(Ign

10、oring setMin(%d) as min max., newMin); return; .,void setMin( int newMin ) . m_min = newMin; updateMinimum(); ,属性Properties,为什么使用getter 函数? 间接的属性,QSize size() const return m_size; int width() const return m_size.width(); ,属性,Q_PROPERTY(type name READ getFunction WRITE setFunction RESET resetFunction

11、 NOTIFY notifySignal DESIGNABLE bool SCRIPTABLE bool STORED bool USER bool CONSTANT FINAL),使用属性,直接获取 通过元信息和属性系统 在运行时发现属性,QString text = label-text(); label-setText(Hello World!);,QString text = object-property(text).toString(); object-setProperty(text, Hello World);,int QMetaObject:propertyCount();

12、QMetaProperty QMetaObject:property(i); QMetaProperty:name/isConstant/isDesignable/read/write/.,动态属性,在运行时给对象增加属性 可以用来“标识”对象,等等。,bool ret = object-setProperty(name, value);,QObject:dynamicPropertyNames() const,创建自定义属性,class AngleObject : public QObject Q_OBJECT Q_PROPERTY(qreal angle READ angle WRITE

13、setAngle) public: AngleObject(qreal angle, QObject *parent = 0); qreal angle() const; void setAngle(qreal); private: qreal m_angle; ;,创建自定义属性,AngleObject:AngleObject(qreal angle, QObject *parent) : QObject(parent), m_angle(angle) qreal AngleObject:angle() const return m_angle; void AngleObject:setAn

14、gle(qreal angle) m_angle = angle; doSomething(); ,自定义属性 - 枚举,class AngleObject : public QObject Q_OBJECT Q_ENUMS(AngleMode) Q_PROPERTY(AngleMode angleMode READ .) public: enum AngleMode Radians, Degrees; . ;,内存管理,QObject 可以有父对象和子对象 当一个父对象被删除,它的子对象也同样被删除。,QObject *parent = new QObject(); QObject *chi

15、ld1 = new QObject(parent); QObject *child2 = new QObject(parent); QObject *child1_1 = new QObject(child1); QObject *child1_2 = new QObject(child1); delete parent;,parent,child1,child2,child1_1,child1_2,内存管理,当需要实现视觉层级时使用到它。,QDialog *parent = new QDialog(); QGroupBox *box = new QGroupBox(parent); QPus

16、hButton *button = new QPushButton(parent); QRadioButton *option1 = new QRadioButton(box); QRadioButton *option2 = new QRadioButton(box); delete parent;,使用模式,使用 this指针指向最高层父对象 在栈上分配父对象空间,void Widget:showDialog() Dialog dialog; if (dialog.exec() = QDialog:Accepted) . ,Dialog:Dialog(QWidget *parent) : QDialog(parent) QGroupBox *box = QGroupB

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号