Qt-QBasicTimer can only be used with threads started with QThread原因探究

项目中遇到这样一个bug,程序关闭后,终端报warning:

QBasicTimer can only be used with threads started with QThread

查找资料后发现,报错的原因很可能是因为使用了static QObject造成的,在这篇文章中我已经提到过,我们不应该使用static的QObject,该篇文章中的引用1提到:

In general, creating QObjects before the QApplication is not supported and can lead to weird crashes on exit , depending on the platform. This means static instances of QObject are also not supported . A properly structured single or multi-threaded application should make the QApplication be the first created, and last destroyed QObject.

在项目中检查发现确实使用了一个static的QDialog,并且ui包含combobox的时候退出时会打印warning,如果没有combobox则不会。

在这个贴子中,提问者的现象和我一致,猜测的原因是QObject和QApplication的析构顺序造成的。

综上,解决方法就是不要使用static QObject,不论是否有报warning,否则可能出现各种奇怪的问题

引用:

  1. https://forum.qt.io/topic/143202/weird-connection-between-qcombobox-qbasictimer-and-static-object-initialization/2
  2. https://blog.csdn.net/mrbone11/article/details/123406432
相关推荐
charlie11451419125 分钟前
AwesomeQt:最小的Qt6系列迷你版本教程发布!
linux·c++·qt·c
CSCN新手听安1 小时前
【Qt】系统相关(一)内容简介,事件概念,事件的处理
开发语言·c++·qt
比特 GOK2 小时前
Qt项目ui文件中新添加的控件在代码中不识别的问题解决
开发语言·qt·ui
繁星蓝雨3 小时前
Qt多界面创建的优化问题(main函数或主界面中创建?)—————附带详细方法
c++·qt·架构·多界面管理
Cx330❀3 小时前
Qt 入门指南:从零搭建开发环境到第一个图形界面程序
xml·大数据·开发语言·网络·c++·人工智能·qt
十五年专注C++开发4 小时前
Qt程序设计涉及到的开发软件
开发语言·c++·qt
IT策士5 小时前
Python mcp研究:入门到精通
开发语言·python·qt
姆路6 小时前
Qt尺寸策略
c++·qt
csgo打的菜又爱玩1 天前
11.JobManager 启动流程总结
大数据·开发语言·qt·microsoft·flink
我在人间贩卖青春1 天前
重学Qt——Qt基础认知
qt