记录使用 Qt5.x 遇到的错误
- 继承于QWidget的外部样式设置无效
- compiler is out of heap space
- run 中 new 要在线程中释放
- 枚举,字符串 互转
- Qt 共享库和Plugin不容易查的错误
- [Qt Creator的问题]Cannot retrieve debugging output
- setMouseGrabEnabled: Not setting mouse grab for invisible window QWidgetWindow/'QMenuClassWindow'
- 定时器.start()后内存无限涨再释放再涨
- error: undefined reference to `__imp_select'
- error: undefined reference to `__WSAFDIsSet'
- Qt 多子项目套子项目时
- QT g++: error: unrecognized command line option '-R'
- uint”: 不明确的符号
- WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed
- 挂了3天win7系统物理内存上涨,关闭软件内存依然没回收
- LNK4042:对象被多次指定;已忽略多余的指定
- Qt 会把.ui文件加入的快捷键作为翻译源
- 网友: webengine退出时偶有崩溃的问题
- 文件修改时间在未来 xxx 秒后
- Syntax error: word unexpected (expecting ")")
- enum {E_A,E_B}; 因为枚举大括号没有加分号;导致的错误
- 因为容器没有互锁访问导致崩溃的bug
- QSqlDatabase 类无法断开
- 无法解析的外部符号 "public: static struct QMetaObject
- Warning: Setting a new default format with a different version or profile after the global shared context is created may cause issues with context sharing.
- Warning: The name 'layoutWidget' (QWidget) is already in use, defaulting to 'layoutWidget1'
- Call to virtual method 'xx::xx' during construction bypasses virtual dispatch [clang-analyzer-optin.cplusplus.VirtualCall]
- LINK : fatal error LNK1158: cannot run 'rc.exe'
- QGraphicScene 移动 QGraphicItems 并删除会崩溃到 QGraphicsSceneFindItemBspTreeVisitor::visit
- QtPrivate::warnIfContainerIsNotShared”: 未找到匹配的重载函数
继承于QWidget的外部样式设置无效
在继承QWidget时,我们在设置样式表时,不会有效果。原因在于 QWidget的paintEvent函数未实现。
解决方法:
void BaseWidget::paintEvent(QPaintEvent *event)
{
QStylePainter painter(this);
//用style画背景 (会使用setstylesheet中的内容)
QStyleOption opt;
opt.initFrom(this);
opt.rect = rect();
painter.drawPrimitive(QStyle::PE_Widget, opt);
QWidget::paintEvent(event);
}
compiler is out of heap space
Qt5.7+vs2013 在windows平台下qrc_images.cpp(源文件images.qrc)的大小超过30M就报fatal error C1060: compiler is out of heap space错误。
解决方法:把一个qrc文件拆成多个qrc解决的,最终qrc_images.cpp 22.2M通过编译
Qt5.7 MinGW编译器则没有这个限制。
run 中 new 要在线程中释放
环境qt5.x MinGW ExcelThread线程 pExcelEngine一个读写Excel的类class ExcelEngine : public QObject 其中 pExcel = new QAxObject(“Excel.Application”);
如果把pExcelEngine = new ExcelEngine;放入init函数中则会报错(错误信息忘记记录了。。。)
,放到run()中则不会。 ----应该是该类不支持跨线程操作
bool ExcelThread::init(int quetypeid,
const QString xls)
{
bool isok = true;
queTypeId = quetypeid;
xlsName = xls;
return isok;
}
void ExcelThread::run()
{
int ret = -1;
if(pExcelEngine){
delete pExcelEngine;
}
pExcelEngine = new ExcelEngine;
connect(pExcelEngine,SIGNAL(sendExcelData(QuestionData*,int)),
this,SLOT(receiveExcelData(QuestionData*,int)));
pExcelEngine->setSXlsFile(xlsName);
pExcelEngine->setExcelType(queTypeId);
int sheet = 1;
bool isopen = pExcelEngine->Open(sheet,false);
......
}
成员变量在线程run()运行无法赋值,必须声明为static变量
.h 文件中 static QString _v;
.cpp文件构造函数上方QString 类名::_v = “”;
枚举,字符串 互转
Qt 共享库和Plugin不容易查的错误
–Plugin:找不到指定模块。1.路径错误 2.路径正确的情况下是否使用第三方库LIBS +=-L$$PWD/ -lxx
表示第三库的dll没有放入指定路径
–上图找不到BaseWidget.h 查看最后加载哪个.cpp文件图片:mainwindow.cpp
再在pro文件中加入缺失BaseClass/BaseClass.pri
下面的错误头文件明明已经加了,还提示找不到,查看提示是蓝色框框中的文件头文件没加pri(多项目编程最常见的错误)需要把当前的头文件路径加到蓝色框文件项目中。
[Qt Creator的问题]Cannot retrieve debugging output
,两个IDE(同一qt版本打开2次及以上)同时打开,后打开的会出现这个情况
解决方案:
只打开一个IDE。(一个qt版本只打开一次)
setMouseGrabEnabled: Not setting mouse grab for invisible window QWidgetWindow/‘QMenuClassWindow’
–
setMouseGrabEnabled: Not setting mouse grab for invisible window QWidgetWindow/‘QMenuClassWindow’
QMenu类中没有QAction时会报
定时器.start()后内存无限涨再释放再涨
–
原因:一个定时器周期内函数执行时间超过了定时器则.start()后无限涨内存。
解决方案:减少函数执行周期,提前new要使用的对象,优化执行时间.
error: undefined reference to `__imp_select’
error: undefined reference to `__WSAFDIsSet’
解决:pro文件中加入: LIBS += -lws2_32 【类似的错误百度缺少的库】
Qt 多子项目套子项目时
解决:缩短项目文件及文件夹名称【编译路径太长了】
QT g++: error: unrecognized command line option ‘-R’
qt:错误:无法识别的命令行选项“r”
解决:查找pro,发现应该是【LIBS += -lRo模块名】错写成【-Ro模块名】(可能是我的模块的首字母是R才报-R的)
uint”: 不明确的符号
出现这种错误是因为引用的第三方库也对uint进行了声明与qt中的uint声明引起编译器错误(编译器不知道编译时用哪个声明编译)
//以谷歌的protobuf引起这个错误为例
解决方式:查找using namespace google::protobuf;注释掉
使用下列方式指定
google::protobuf::RepeatedPtrField<Message_AdvancedCurve>
WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed
–
出现情况:vs2015+Qt5.9.4 使用QWebEngineView类,选择32bit编译时正常, 选择64bit编译出现上述错误,并崩溃
解决方法:在new QWebEngineView前 加上QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
挂了3天win7系统物理内存上涨,关闭软件内存依然没回收
解决方法:CloseHandle(HANDLE); //共享内存分配后不会自动回收
LNK4042:对象被多次指定;已忽略多余的指定
解决方法:查看报错的xxx.obj,查找xxx.cpp是否被多次加入.pro/.pri文件中
Qt 会把.ui文件加入的快捷键作为翻译源
解决方法:中英文翻译,在.ui文件设置某个按钮的快捷键,出现在了.ts中。。。一定注意
网友: webengine退出时偶有崩溃的问题
解决办法:
继承QWebEnginePage的构造函数中需要传入QWebEngineProfile,不传就偶尔有问题。
文件修改时间在未来 xxx 秒后
解决方法:sudo find 文件目录/* -exec touch {} +
Syntax error: word unexpected (expecting “)”)
解决方法:kits套件里的 gcc g++的套件被篡改了
enum {E_A,E_B}; 因为枚举大括号没有加分号;导致的错误
解决:enum {
…
};
因为容器没有互锁访问导致崩溃的bug
QGraphicsItem 中
遇到崩溃,断点调到此处 QList()<<QPoint()
发现内存什么的都问题, 发现是使用容器, shape(),boundRect(),paintevent()中都使用没有互斥导致的崩溃
QSqlDatabase 类无法断开
解决方法:注意里面的 {}大括号,QSqlDatabase dbTmp与 QSqlDatabase::removeDatabase(_connectionName)不能在同一作用域,否则无法删除连接
SqlClass::~SqlClass()
{
// //QSqlDatabasePrivate::removeDatabase: connection 'XXX' is still in use, all queries will cease to work
if(QSqlDatabase::contains(_connectionName)){
{
QSqlDatabase dbTmp = QSqlDatabase::database(_connectionName);
if(dbTmp.isValid()){
QSqlDatabase *db = &dbTmp;
if(db->isOpen())
db->close();
SCDebug<<"removeDatabase:"<<_connectionName;
}
}
QSqlDatabase::removeDatabase(_connectionName);
}
}
无法解析的外部符号 "public: static struct QMetaObject
MSVC编译器
1.如果这个错误是发生在lib库中的类,查看有无加 Q_OBJECT,如果加了,Rebuild这个模块,再运行
2.如果错误还未消除,查看调用这个类的模块是否在.pro中重复定义了lib中的导出宏,经实验如果lib库中的导出宏在别的模块中重复声明会导致它默认未导出
Warning: Setting a new default format with a different version or profile after the global shared context is created may cause issues with context sharing.
解决:检查 .exe 路径下的 Qt.dll 产品版本不一致
Warning: The name ‘layoutWidget’ (QWidget) is already in use, defaulting to ‘layoutWidget1’
解决: 用文本打开 .ui 文件, 搜索 “layoutWidget” 有重复的手动改成 “layoutWidget1” 即可
Call to virtual method ‘xx::xx’ during construction bypasses virtual dispatch [clang-analyzer-optin.cplusplus.VirtualCall]
描述:在构造期间调用虚拟方法’子类::虚函数’绕过虚拟调度[clang-analyzer-optin.cplusplus.VirtualCall]
解决方法: 需要在子类构造中写成:子类::虚函数
注意:基类的构造和析构中不能写虚函数
检查便准:https://clang.llvm.org/docs/analyzer/checkers.html#optin-cplusplus-virtualcall 1.1.5.2. optin.cplusplus.VirtualCall (C++)
LINK : fatal error LNK1158: cannot run ‘rc.exe’
把 vs2015 中的 rcdll.dll 和 rc.exe 拷贝到 H:\Qt\Qt5.12.12\5.12.12\msvc2015_64\bin 即可
QGraphicScene 移动 QGraphicItems 并删除会崩溃到 QGraphicsSceneFindItemBspTreeVisitor::visit
解决方法: QGraphicScene->setItemIndexMethod(QGraphicsScene::ItemIndexMethod::NoIndex);
QtPrivate::warnIfContainerIsNotShared”: 未找到匹配的重载函数
解决方式: 鼠标右击复制这个错误
D:\Qt\6.7.2\6.7.2\msvc2019_64\include\QtCore\qforeach.h:51: error: C2672: “QtPrivate::warnIfContainerIsNotShared”: 未找到匹配的重载函数
D:\Qt\6.7.2\6.7.2\msvc2019_64\include\QtCore/qforeach.h(51): error C2672: “QtPrivate::warnIfContainerIsNotShared”: 未找到匹配的重载函数
D:\coding\r3_q6\v3\Libs\CLibs\MapEditor\MapGraphics\MapCore\src\RoOpencv3xThread.cpp(119): note: 查看对正在编译的函数 模板 实例化“QtPrivate::QForeachContainer<std::vector<RO_R3Map::MapPoint,std::allocator<RO_R3Map::MapPoint>>> QtPrivate::qMakeForeachContainer<std::vector<RO_R3Map::MapPoint,std::allocator<RO_R3Map::MapPoint>>&>(T)”的引用
with
[
T=std::vector<RO_R3Map::MapPoint,std::allocator<RO_R3Map::MapPoint>> &
]
这种错误一般都是 auto 导致的, 但这个是 foreach 的问题,使用 for