资源预览内容
第1页 / 共60页
第2页 / 共60页
第3页 / 共60页
第4页 / 共60页
第5页 / 共60页
第6页 / 共60页
第7页 / 共60页
第8页 / 共60页
第9页 / 共60页
第10页 / 共60页
亲,该文档总共60页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
.,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 countries 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 Training Materials are provided under the Creative Commons Attribution-Non-Commercial-Share Alike 2.5 License Agreement. The full license text is available here: http:/creativecommons.org/licenses/by-nc-sa/2.5/legalcode.,此文档内容是由诺基亚公司发布的原创Qt教育培训文档的中文翻译: 2010诺基亚公司及其附属公司。Nokia (诺基亚),Qt以及Nokia与Qt商标是Nokia公司在芬兰和全球其他国家的注册商标。 该翻译版本由 华南理工大学广东省计算机网络重点实验室 创造。 2010 华南理工大学广东省计算机网络重点实验室本Qt 教育培训材料依照署名-非商业性使用-相同方式共享 2.5许可协议(Creative Commons Attribution-Non-Commercial-Share Alike 2.5 License Agreement)发布。完整的许可证文本可以在这里找到:http:/creativecommons.org/licenses/by-nc-sa/2.5/legalcode。,QObject类,QObject是几乎所有Qt类和所有部件(widget)的基类。它包含很多组成Qt的机制事件信号和槽属性内存管理,QObject类,QObject 是大部分Qt 类的基类例外的例子是:类需要作为轻量级的类,例如图元(graphical primitives)。数据容器(QString, QList, QChar等)需要可复制的类,因为QObject类是无法被复制的。,QObject类,它们可以拥有一个名字 (QObject:objectName)它们被放置在QObject实例的一个层次上它们可以有到其他 QObject 实例的联接例子: 在运行时复制一个部件有意义吗?,“QObject 的实例是单独的!”,元数据(Meta data),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*.cpp,executables,object files*.o,headers*.h,generatedmoc_*.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(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函数属性命名策略: 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(Ignoring 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 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();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 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:setAngle(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 *child1 = 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);QPushButton *button = new QPushButton(parent);QRadioButton *option1 = new QRadioButton(box);QRadioButton *option2 = new QRadioButton(box);delete parent;,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号