欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > PPT文档下载
     

    NOKIA_QT培训资料.ppt

    • 资源ID:3028894       资源大小:1.89MB        全文页数:60页
    • 资源格式: PPT        下载积分:8
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要8
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    NOKIA_QT培训资料.ppt

    .,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,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(“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;,使用模式,使用 this指针指向最高层父对象 在栈上分配父对象空间,void Widget:showDialog() Dialog dialog; if (dialog.exec() = QDialog:Accepted) . ,Dialog:Dialog(QWidget *parent) : QDialog(parent) QGroupBox *box = QGroupBox(this); QPushButton *button = QPushButton(this); QRadioButton *option1 = QRadioButton(box); QRadioButton *option2 = QRadioButton(box); .,堆(Heap),当使用 new 和 delete时, 内存在堆中分配。 堆内存空间必须通过 delete 完全释放,以防止内存泄漏。 只要有需要,分配在堆上的对象可以一直存活下去。,new,delete,构造Construction,析构Destruction,栈(Stack),局部变量在栈上分配。 栈变量超过作用范围时会自动释放。 分配在栈中的对象在超出作用范围时总是会被析构。,int a,构造Construction,析构Destruction,堆 和 栈,想要自动内存管理,只有父对象需要在栈上分配。,MyMainWindow,QApplication,int main(int argc, char *argv) QApplication a(argc, argv); MyMainWindow w; w.show(); return a.exec(); ,MyMainWindow:MyMainWindow(. new QLabel(this); new . ,改变所有者,QObject可以修改它所属的父对象。 父对象知道何时子对象被删除 一系列函数实现返回指针,从其所有者“拿走”释放的数据,把它留给拿取者处理,obj-setParent(newParent);,delete listWidget-item(0); / 删除第一个item(不安全),QLayoutItem *QLayout:takeAt(int); QListWidgetItem *QListWidget:takeItem(int); / Safe alternative QListWidgetItem *item = listWidget-takeItem(0); if (item) delete item; ,构造规范,几乎所有的 QObject 都有一个默认为空值的父对象。 Qwidget 的父对象是其它 QWidget 类为了方便倾向于提供多种构造(包括只带有父对象的一种) 父对象通常是带缺省值的第一个参数。,QLabel(const QString ,QObject(QObject *parent=0);,QPushButton(QWidget *parent=0); QPushButton(const QString ,构造规范,当创建自己的 Qobject时, 需考虑 总是允许父对象 parent 为 0 (null) 有一个只接受父对象的构造函数 parent 是带默认值的第一个参数 提供几种构造函数,避免空值、无效值(e.g. QString()作为参数。,休息,信号(signal)和槽(slot),通过反馈的方式动态地或松散地将事件和状态变化联系起来。 是什么使 Qt 运作?,动作中的信号和槽,emit clicked();,动作中的信号和槽,private slots: void on_addButton_clicked(); void on_deleteButton_clicked();,connect(clearButton,SIGNAL(clicked(),listWidget,SLOT(clear();,connect(addButton,SIGNAL(clicked(),this,SLOT(.);,2x,clear();,动作中的信号和槽, . emit clicked(); . , . emit clicked(); . , . emit clicked(); . , QString newText = QInputDialog:getText(this, “Enter text“, “Text:“); if( !newText.isEmpty() ) ui-listWidget-addItem(newText); , foreach (QListWidgetItem *item, ui-listWidget-selectedItems() delete item; ,clear();,信号和槽 vs 回调,回调(callback)是一个函数指针,当一个事件发生时被调用,任何函数都可以被安排作为回调。 没有类型安全 总是以直接调用方式工作 信号和槽的方式更加动态 一个更通用的机制 更容易互连两个已存在的类 相关类之间涉及更少的知识共享,什么是槽?,槽在各种槽段(section)中定义。 槽可以返回值,但并不是通过联接。 任何数量的信号可以关联到一个槽。 它以一个普通的函数实现。 它可以作为普通函数被调用。,public slots: void aPublicSlot(); protected slots: void aProtectedSlot(); private slots: void aPrivateSlot();,connect(src, SIGNAL(sig(), dest, SLOT(slt();,什么是信号?,信号在信号段(section)中定义 信号总是返回空 信号总是不必实现 由moc来提供实现 信号可以关联到任意数量的槽上 通常产生一个直接调用,但是可以在线程之间作为事件来传递,甚至可以用在套接字之间(使用第三方类) 槽能以任意次序被激发 信号使用emit 关键字发射出去。,signals: void aSignal();,emit aSignal();,建立关联,QObject:connect( src, SIGNAL( signature ), dest, SLOT( signature ) );, ( . ),clicked() toggled(bool) setText(QString) textChanged(QString) rangeChanged(int,int),setTitle(QString text) setValue(42),签名由函数名和参数类型组成。不允许有变量名或值。,自定义类型降低了可重用性,setItem(ItemClass),建立关联,Qt 参数可以忽略,但不能无中生有。,Signals rangeChanged(int,int) rangeChanged(int,int) rangeChanged(int,int) valueChanged(int) valueChanged(int) valueChanged(int) textChanged(QString) clicked() clicked(),Slots setRange(int,int) setValue(int) updateDialog() setRange(int,int) setValue(int) updateDialog() setValue(int) setValue(int) updateDialog(),自动关联,使用Designer,它很便捷地在接口和用户代码之间提供自动关联。 通过调用QMetaObject:connectSlotsByName触发 当命名时考虑重用性 比较 on_widget_signal 和 updatePageMargins,on_ object name _ signal name ( signal parameters ) on_addButton_clicked(); on_deleteButton_clicked(); on_listWidget_currentItemChanged(QListWidgetItem*,QListWidgetItem*),值同步,双向连接 无限循环必须停止 没有信号被发射,除非发生实际的变化。,void QDial:setValue(int v) if(v=m_value) return; .,自定义信号和槽,class AngleObject : public QObject Q_OBJECT Q_PROPERTY(qreal angle READ angle WRITE setAngle NOTIFY angleChanged) public: AngleObject(qreal angle, QObject *parent = 0); qreal angle() const; public slots: void setAngle(qreal); signals: void angleChanged(qreal); private: qreal m_angle; ;,setter实现细节,void AngleObject:setAngle(qreal angle) if(m_angle = angle) return; m_angle = angle; emit angleChanged(m_angle); ,温度转换器,使用 TempConverter 类实现在摄氏与华氏之间的转换 当温度改变时发射信号。,温度转换器,对话窗口(dialog window)包含以下对象 一个 TempConverter 实例 两个 QGroupBox 部件(widget), 每一个包含 一个 QDial 部件 一个 QLCDNumber 部件,温度转换器,class TempConverter : public QObject Q_OBJECT public: TempConverter(int tempCelsius, QObject *parent = 0); int tempCelsius() const; int tempFahrenheit() const; public slots: void setTempCelsius(int); void setTempFahrenheit(int); signals: void tempCelsiusChanged(int); void tempFahrenheitChanged(int); private: int m_tempCelsius; ;,温度转换器,void TempConverter:setTempCelsius(int tempCelsius) if(m_tempCelsius = tempCelsius) return; m_tempCelsius = tempCelsius; emit tempCelsiusChanged(m_tempCelsius); emit tempFahrenheitChanged(tempFahrenheit(); void TempConverter:setTempFahrenheit(int tempFahrenheit) int tempCelsius = (5.0/9.0)*(tempFahrenheit-32); setTempCelsius(tempCelsius); ,setTempCelsius槽: setTempFahrenheit槽:,温度转换器,表盘通过 TempConverter 联系起来 LCD 显示直接受表盘来驱动。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,温度转换器,用户调节摄氏度表盘。,connect(celsiusDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempCelsius(int); connect(celsiusDial, SIGNAL(valueChanged(int), celsiusLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempCelsiusChanged(int), celsiusDial, SLOT(setValue(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), tempConverter, SLOT(setTempFahrenheit(int); connect(fahrenheitDial, SIGNAL(valueChanged(int), fahrenheitLcd, SLOT(display(int); connect(tempConverter, SIGNAL(tempFahrenheitChanged(int), fahrenheitDial, SLOT(setValue(int);,与值关联?,一种常见情况是,希望在关联声明中传递一个值。 例如, 键盘实例 这不是有效的 -它将不会关联。,connect(key, SIGNAL(clicked(), this, SLOT(keyPressed(1);,与值关联?,解决方法 #1: 多个槽, . public slots: void key1Pressed(); void key2Pressed(); void key3Pressed(); void key4Pressed(); void key5Pressed(); void key6Pressed(); void key7Pressed(); void key8Pressed(); void key9Pressed(); void key0Pressed(); . ,与值关联?,解决方法 #2: 子类发射器和增加信号,QPushButton,QIntPushButton, . signals: void clicked(int); . , QIntPushButton *b; b=new QIntPushButton(1); connect(b, SIGNAL(clicked(int), this, SLOT(keyPressed(int); b=new QIntPushButton(2); connect(b, SIGNAL(clicked(int), this, SLOT(keyP

    注意事项

    本文(NOKIA_QT培训资料.ppt)为本站会员(本田雅阁)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    经营许可证编号:宁ICP备18001539号-1

    三一文库
    收起
    展开