NOKIAQT培训资料.ppt

上传人:sccc 文档编号:5927318 上传时间:2023-09-05 格式:PPT 页数:60 大小:1.89MB
返回 下载 相关 举报
NOKIAQT培训资料.ppt_第1页
第1页 / 共60页
NOKIAQT培训资料.ppt_第2页
第2页 / 共60页
NOKIAQT培训资料.ppt_第3页
第3页 / 共60页
NOKIAQT培训资料.ppt_第4页
第4页 / 共60页
NOKIAQT培训资料.ppt_第5页
第5页 / 共60页
点击查看更多>>
资源描述

《NOKIAQT培训资料.ppt》由会员分享,可在线阅读,更多相关《NOKIAQT培训资料.ppt(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 countri

2、es 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 Mat

3、erials 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公司在芬兰和全球其他国家的注册

4、商标。该翻译版本由 华南理工大学广东省计算机网络重点实验室 创造。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

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

6、一个元对象元对象涉及:类名(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,header

7、s*.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_cas

8、t(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 QFra

9、me 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;updat

10、eMinimum();,属性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),使用属性,直接获取通过元信

11、息和属性系统在运行时发现属性,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/.,动态属性,在运行时给对象增加属性可以用来

12、“标识”对象,等等。,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;,创

13、建自定义属性,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 angleMod

14、e 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,chi

15、ld2,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:sho

16、wDialog()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时,内存在堆中分配。堆内存空间必须通过 delet

17、e 完全释放,以防止内存泄漏。只要有需要,分配在堆上的对象可以一直存活下去。,new,delete,构造Construction,析构Destruction,栈(Stack),局部变量在栈上分配。栈变量超过作用范围时会自动释放。分配在栈中的对象在超出作用范围时总是会被析构。,int a,构造Construction,析构Destruction,堆 和 栈,想要自动内存管理,只有父对象需要在栈上分配。,MyMainWindow,QApplication,int main(int argc,char*argv)QApplication a(argc,argv);MyMainWindow w;w.s

18、how();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 alter

19、nativeQListWidgetItem*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时,需考虑

20、总是允许父对象 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(),l

21、istWidget,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-listWid

22、get-selectedItems()delete item;,clear();,信号和槽 vs 回调,回调(callback)是一个函数指针,当一个事件发生时被调用,任何函数都可以被安排作为回调。没有类型安全总是以直接调用方式工作信号和槽的方式更加动态一个更通用的机制更容易互连两个已存在的类相关类之间涉及更少的知识共享,什么是槽?,槽在各种槽段(section)中定义。槽可以返回值,但并不是通过联接。任何数量的信号可以关联到一个槽。它以一个普通的函数实现。它可以作为普通函数被调用。,public slots:void aPublicSlot();protected slots:void aP

23、rotectedSlot();private slots:void aPrivateSlot();,connect(src,SIGNAL(sig(),dest,SLOT(slt();,什么是信号?,信号在信号段(section)中定义信号总是返回空信号总是不必实现由moc来提供实现信号可以关联到任意数量的槽上通常产生一个直接调用,但是可以在线程之间作为事件来传递,甚至可以用在套接字之间(使用第三方类)槽能以任意次序被激发信号使用emit 关键字发射出去。,signals:void aSignal();,emit aSignal();,建立关联,QObject:connect(src,SIGNA

24、L(signature),dest,SLOT(signature);,(.),clicked()toggled(bool)setText(QString)textChanged(QString)rangeChanged(int,int),setTitle(QString text)setValue(42),签名由函数名和参数类型组成。不允许有变量名或值。,自定义类型降低了可重用性,setItem(ItemClass),建立关联,Qt 参数可以忽略,但不能无中生有。,SignalsrangeChanged(int,int)rangeChanged(int,int)rangeChanged(int

25、,int)valueChanged(int)valueChanged(int)valueChanged(int)textChanged(QString)clicked()clicked(),SlotssetRange(int,int)setValue(int)updateDialog()setRange(int,int)setValue(int)updateDialog()setValue(int)setValue(int)updateDialog(),自动关联,使用Designer,它很便捷地在接口和用户代码之间提供自动关联。通过调用QMetaObject:connectSlotsByNam

26、e触发当命名时考虑重用性比较 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;.

27、,自定义信号和槽,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 AngleO

28、bject: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 p

29、ublic: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:setTempCel

30、sius(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);,set

31、TempCelsius槽: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

32、,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);,温度转换器,用户调节摄氏度表盘。,co

33、nnect(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),tempConverte

34、r,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

35、);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),fa

36、hrenheitLcd,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);conn

37、ect(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

38、(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);

39、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,SI

40、GNAL(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(setTempFahren

41、heit(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(celsiusDia

42、l,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(dis

43、play(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,SI

44、GNAL(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

45、,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 key8Pr

46、essed();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(keyPressed(

47、int);b=new QIntPushButton(3);connect(b,SIGNAL(clicked(int),this,SLOT(keyPressed(int);.,解决方案评价,#1:多个槽许多槽包含几乎相同的代码难于维护(一个小的变化影响所有槽)难于扩展(每次都要新建槽)#2:子类发射器和增加信号额外的专用类(难于重用)难于扩展(每个情况需新建子类),信号映射器,QSignalMapper 类解决了这个问题将每个值映射到每个发射器介于可重用类之间,QSignalMapper*m=QSignalMapper(this);QPushButton*b;b=new QPushButton(1);connect(b,SIGNAL(clicked(),m,SLOT(map();m-setMapping(b,1);.connect(m,SIGNAL(mapped(int),this,SLOT(keyPressed(int);,信号映射器,QSignalMapper,.public slots:void keyPressed();.,信号映射器把每一个按钮和值关联起来。这些值都被映射。当一个值被映射,映射器发出携带关联的值的映射信号(int)。,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号