Q_UNLIKELY标记分支预测,缓存命中,提高执行效率。 Q_UNREACHABLE执行到这里,程序会发生未定义的行为。常见的后果就是崩溃,用于标记永远不会执行到的语句,从而优化代码覆盖率。 Q_UNUSED没有用到部分参数或对象时,编译器会发出警告,使用此宏减少警告。 qAbs返回参数的绝对值 ...
- 对于不确定的平台行为,可以使用Q_UNLIKELY宏进行判断。 在本书的写作过程中,我们不断回顾和总结了这些实用的技巧与经验。希望读者在学习和实践的过程中,能够将这些技巧内化为自己的技能,从而在QT Widgets模块的开发中更加得心应手。 [QT界面美化视频课程](徐德华讲师的QSS在线课程培训-CSDN程序员研修院) [QT性能...
#define DECLARE_SINGLETON(Class) \ Q_DISABLE_COPY(Class) \ public: \ static Class* Instance() \ { \ static QMutex mutex; \ static QScopedPointer<Class> inst; \ if (Q_UNLIKELY(!inst)) { \ mutex.lock(); \ if (!inst) inst.reset(new Class); \ mutex.unlock(); \ } \ return ins...
1template <classT>2classSingleton3{4public:5staticT*Instance()6{7staticQMutex mutex;8staticQScopedPointer<T>inst;9if(Q_UNLIKELY(!inst)) {10mutex.lock();11if(!inst) {12inst.reset(newT);13}14mutex.unlock();15}16returninst.data();17}18}; 使用的时候直接这样—— MyClass* inst = Singl...
if(Q_UNLIKELY(!qobject_cast<QApplication*>(QCoreApplication::instance()))qFatal("QWidget: Cannot create a QWidget without QApplication"); 前面已经分析过,QCoreApplication::instance()返回的是QCoreApplication::self这个静态成员变量,如果在创建QWidget前没有创建QApplication,self值就为nullptr,满足条件...
1、QFunctionPointer 这是void (*)() 的 typedef,指向不带参数并返回 void 的函数指针。 2、QtMessageHandler 指向以下类型函数的指针的 typedef: void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &); 3、enumQtMsgType ...
Q_UNLIKELY( expr) void Q_UNREACHABLE() Q_UNUSED( name) foreach( variable, container) forever qCritical(const char * message, ...) qDebug(const char * message, ...) qFatal(const char * message, ...) const char * qPrintable(const QString & str) qWarning(const char * message, .....
QPixmap::QPixmap(constQString&fileName,...):QPaintDevice(){...if(!qt_pixmap_thread_test())return;...}staticboolqt_pixmap_thread_test(){if(Q_UNLIKELY(!QCoreApplication::instance())){qFatal("QPixmap: Must construct a QGuiApplication before a QPixmap");returnfalse;}...returntrue;} ...
5if (Q_UNLIKELY(!inst)) { 6 mutex.lock();7if (!inst) { 8 inst.reset(new MyClass);9 } 10 mutex.unlock();11 } 12return inst.data();13 } 既保证了线程安全⼜防⽌了内存泄漏,效率也没降低太多,简直完美。可惜每次都要重复这么⼏⾏实在⿇烦,于是写了⼀个模板类...
staticboolqt_pixmap_thread_test(){if(Q_UNLIKELY(!QCoreApplication::instance())){qFatal("QPixmap: Must construct a QGuiApplication before a QPixmap");returnfalse;}if(QGuiApplicationPrivate::instance()&&qApp->thread()!=QThread::currentThread()&&!QGuiApplicationPrivate::platformIntegration()->...