由Qt中qApp想到的(这是单例模式么???)
From: http://blog.csdn.net/qq575787460/article/details/7880972
学Qt时,发现只要包含头文件QApplication或者QCoreapplication,就用使用qApp,指向一个当前实例。
查看源码发现qApp是个宏:
QApplication中:#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))
QCoreapplication中:#define qApp QCoreApplication::instance()
所以可以肯定的是QCoreApplication有个静态函数instance()返回指向自己的指针。
- static QCoreApplication *instance() { return self; }
这个函数返回self,看大片self也定义为静态指针:
- static QCoreApplication *self;
在QCoreapplication.cpp中有如下代码:
- void QCoreApplication::init()
- {
- ......
- QCoreApplication::self = this;
- ......
- }
QCoreapplication构造函数中又调用init()
- QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
- : QObject(p, 0)
- {
- init();
- // note: it is the subclasses' job to call
- // QCoreApplicationPrivate::eventDispatcher->startingUp();
- }
所以就是:定义程序唯一的实例时,初始化self指向自身。qApp是一个宏,替换后为调用相应的instance()。
自己模仿的:
myApplication.h
- #ifndef MYAPPLICATION_H
- #define MYAPPLICATION_H
- #include <string>
- using std::string;
- #define myApp myApplication::instance()
- class myApplication
- {
- public:
- myApplication(string _appName);
- static myApplication* instance(){return self;}
- void printAppName();
- private:
- //指向自己的实例句柄
- static myApplication *self;
- string appName;
- };
- #endif // MYAPPLICATION_H
myApplication.cpp
- #include "myapplication.h"
- #include <iostream>
- using std::cout;
- using std::endl;
- myApplication::myApplication(string _appName):appName(_appName)
- {
- if(self!=0)
- return;
- else
- self=this;
- }
- void myApplication::printAppName()
- {
- cout<<appName<<endl;
- }
- myApplication* myApplication::self=0;
main.cpp
- #include "myapplication.h"
- extern void print1();
- extern void print2();
- int main()
- {
- print1();
- print2();
- myApplication app("实例测试");
- print1();
- print2();
- app.printAppName();
- myApp->printAppName();
- return 0;
- }
print1.cpp
- /*
- 测试函数,测试通过myApp输出唯一的实例名
- */
- #include "myapplication.h"
- #include <iostream>
- void print1()
- {
- if(myApp==0)
- std::cout<<"myApp没有实例化"<<std::endl;
- else
- myApp->printAppName();
- }
print2.cpp
- /*
- 测试函数,测试通过myApp输出唯一的实例名
- */
- #include "myapplication.h"
- #include <iostream>
- void print2()
- {
- if(myApp==0)
- std::cout<<"myApp没有实例化"<<std::endl;
- else
- myApp->printAppName();
- }
由Qt中qApp想到的(这是单例模式么???)相关推荐
- Qt中关于undefined reference to `vtable for故障总结
在Qt中,undefined reference to 'vtable for 通常出现在打开了宏 Q_OBJECT的程式当中.我遇到的情况是: 将类体及类体实现直接写到main.cpp里,没有包含 ...
- 用事件队列解决GUI的操作顺序问题(Qt中处理方法)
GUI操作顺序问题引发异常: 有时候我们使用写GUI程序的时候会遇到这样的问题:比如在程序中,建立了一个列表的GUI.这个列表是随着时间不断更新的,而且操作也会读取这个列表GUI的内容. 如果这个程序 ...
- Qt中事件循环机制详解
问题1:Qt中常见的事件有哪些? 答:鼠标事件(QMouseEvent).键盘事件(QKeyEvent).绘制事件(QPaintEvent).窗口尺寸改变(QResizeEvent).滚动事件(QSc ...
- QT中父子窗口事件传递与事件过滤器
处理监控系统的时候遇到问题,在MainWidget中创建多个子Widget的时候,原意是想鼠标点击先让MainWidget截获处理后再分派给子Widget去处理,但调试后发现如果子Widget重新实现 ...
- 关于QT中奇数个汉字出现newline in constant的错误
前面写了一篇关于QT中使用汉字的博客以为对汉字的使用了如指掌, 没想到今天把字符串一换, 换成偶数个没问题, 换成奇数个汉字, 哇擦, 报一个错误newline in constant, 瞬间意识到自 ...
- 如何在Qt中使用zlib
前言 环境:qt5.9.9 . zlib1.2.1. windows10.QtCreator4.11.0 本文介绍了在Qt中使用zlib的方式.使用的场景是在上位机软件中使用解压缩功能. 点击此处下载 ...
- 在Qt中使用OpenGL(四)
前言 在Qt中使用OpenGL(一) 在Qt中使用OpenGL(二) 在Qt中使用OpenGL(三) 在之前的文章中,我们通过一个最简单的例子完成了在Qt中使用OpenGL绘图的全过程,然后又使用了纹 ...
- QT中关闭应用程序和窗口的函数(quit(),exit()以及close()的区别)
QT中关闭应用程序和窗口的函数(quit(),exit()以及close()的区别) 使用QT编辑界面,其中带来很大方便的一点就是Qt中自带丰富的.种类齐全的类及其功能函数,程序员可以在编辑程序的过程 ...
- QT中使用fontawesome-webfont.ttf字体库
QT中使用fontawesome-webfont.ttf字体库 fontawesome-webfont.ttf https://fontawesome.com/ http://www.fontawes ...
最新文章
- 【割边缩点】解题报告:POJ - 3694 - Network(Tarjan割边缩点 + LCA + 并查集优化)
- 基于时序数据的微内核预警引擎架构设计
- jquery 常用属性和函数(part I)
- 网络协议栈11:Connect函数分解之TCP层
- postman+newman生成html报告
- java学习(75):GUL文本框和标签
- ndarray对象的建立
- Spring MVC小DEMO
- python高阶函数(三分钟读懂)
- Android Spinner(下拉菜单)常用属性与点击事件获取值
- CBoard数据分析实战
- DBeaver下载安装教程
- 戴尔游匣G15怎么样?真实体验不翻车
- 5.Python格式化输出
- 从消息推送来看,华为、小米做得最好
- 李泉老师《高级商务礼仪锻造》
- Andriod Studio 线性布局(LinearLayout)
- C++函数指针与成员函数指针
- pymysql模块安装后,import pymysql找不到?
- 三星 N9006 note3 成功root