Qt信号参数中使用QVariantList时编译问题

简介: 今天调试代码时遇到一个奇怪的问题,不过一般感觉比较奇怪的问题,最后查到原因时,原因都比较简单!

编译问题
先来看一下qt的编译错误,提示一堆错误:

In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qglobal.h:1173:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qnamespace.h:43,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qobjectdefs.h:48,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:46,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h: In instantiation of 'class QTypeInfo<QVariant>':
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:459:31:   required from 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QVariant]'
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:813:22:   required from 'QList<T>::QList(const QList<T>&) [with T = QVariant]'
temp\moc\moc_test.cpp:103:82:   required from here
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h:67:26: error: invalid application of 'sizeof' to incomplete type 'QVariant'
         isLarge = (sizeof(T)>sizeof(void*)),
                          ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h:69:24: error: invalid application of 'sizeof' to incomplete type 'QVariant'
         sizeOf = sizeof(T)
                        ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QVariant]':
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:813:22:   required from 'QList<T>::QList(const QList<T>&) [with T = QVariant]'
temp\moc\moc_test.cpp:103:82:   required from here
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:462:28: error: invalid use of incomplete type 'class QVariant'
                 current->v = new T(*reinterpret_cast<T*>(src->v));
                            ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
                 delete reinterpret_cast<T*>(current->v);
                 ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: warning: invalid use of incomplete type 'class QVariant'
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
                 delete reinterpret_cast<T*>(current->v);
                 ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:475:17: error: invalid use of incomplete type 'class QVariant'
                 new (current) T(*reinterpret_cast<T*>(src));
                 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:481:17: error: invalid use of incomplete type 'class QVariant'
                 (reinterpret_cast<T*>(current))->~T();
                 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_destruct(QList<T>::Node*, QList<T>::Node*) [with T = QVariant]':
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:865:18:   required from 'void QList<T>::dealloc(QListData::Data*) [with T = QVariant]'
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:827:16:   required from 'QList<T>::~QList() [with T = QVariant]'
temp\moc\moc_test.cpp:103:82:   required from here
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
         while(from != to) --to, delete reinterpret_cast<T*>(to->v);
                               ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: warning: invalid use of incomplete type 'class QVariant'
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
         while(from != to) --to, delete reinterpret_cast<T*>(to->v);
                               ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:496:32: error: invalid use of incomplete type 'class QVariant'
         while (from != to) --to, reinterpret_cast<T*>(to)->~T();
                                ^
In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
                 from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
                 from temp\moc\../../../test/module_function/test.h:4,
                 from temp\moc\moc_test.cpp:9:
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
     F(QVariant, 41, QVariant) \
                     ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
     class Name;
           ^
D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
 QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
 ^
Makefile.Debug:3709: recipe for target 'temp/obj/moc_test.o' failed
mingw32-make[1]: Leaving directory 'E:/code/test-Debug'
Makefile:36: recipe for target 'debug' failed
mingw32-make[1]: *** [temp/obj/moc_test.o] Error 1
mingw32-make: *** [debug] Error 2
18:04:40: 进程"D:\Qt\Qt5.10.0\Tools\mingw530_32\bin\mingw32-make.exe"退出,退出代码 2 。
Error while building/deploying project test (kit: Desktop Qt 5.10.0 MinGW 32bit)
When executing step "Make"

排查问题
从这里面可以看出,基本上是与QVariant类型的使用有关系,查了下代码,里面没有直接使用QVariant类型的地方,到是有一个信号的参数类型,使用的是QVariantList,怀疑有可能是这个信号引起的,把这信号注释掉之后,编译不在报错。然后类里面的成员变量里使用QVariantList定义的成员变量,则不会导致这个编译错误!

另外,通过查看编译错误,可以发现,是编译moc_test.cpp文件时开始报错的,也就是说,在编译moc文件时提示报错,moc文件里第9行是#include " temp\moc../../../test/module_function/test.h",引用的是测试类的头文件,也看不出什么错误。

只是尝试给test.h里加了个头文件引用:#include ,再次编译错误没有了,编译通过!

问题原因
使用QVariantList作为信号的参数时,一定引用头文件:

#include  <QVariantList>

否则会编译错误,如果不作为信号的参数,只是作为成员变量用,或者成员函数的参数,不引用其头文件也不会报错。

这两种情况编译是有点区别的,moc文件是qt编译器根据类源文件自动生成的代码。

在此记录一下,以免以后调试遇到同样的问题而浪费时间。

目录
相关文章
|
21天前
|
编译器
(9)Qt中信号与槽重载的解决方案
本文介绍了在Qt中处理信号与槽重载问题的三种解决方案:使用函数指针、Qt提供的QOverload类和Qt4的宏方式。
63 3
|
21天前
(8)Qt中的自定义信号
本文介绍了如何在Qt框架中创建和使用自定义信号,并通过一个父子窗口切换的示例来展示自定义信号的实现和应用。
48 3
(8)Qt中的自定义信号
|
2月前
|
C++
VS2019编译VTK-9.1.0+Qt5.15.2
本文介绍了在VS2019和Qt 5.15.2环境下编译VTK 9.1.0的过程,包括成功编译无警告和错误的截图、修改的pro文件内容,以及在QML中加入VTK的失败尝试和参考链接。
|
2月前
|
C语言 Android开发 C++
基于MTuner软件进行qt的mingw编译程序的内存泄漏检测
本文介绍了使用MTuner软件进行Qt MinGW编译程序的内存泄漏检测的方法,提供了MTuner的下载链接和测试代码示例,并通过将Debug程序拖入MTuner来定位内存泄漏问题。
基于MTuner软件进行qt的mingw编译程序的内存泄漏检测
|
2月前
|
C++
vtkdicom0.8_vtk9.2_dcmtk3.6.7_qt6.2编译OK
本文介绍了如何编译整合VTK 9.2、DICOM 0.8、DCMTK 3.6.7和Qt 6.2的步骤,包括安装Qt、CMake配置以及确认相关路径和版本设置。
vtkdicom0.8_vtk9.2_dcmtk3.6.7_qt6.2编译OK
|
2月前
|
Unix 网络虚拟化 C++
VS2022+Qt5.14.2成功编译MITK2022.10
使用VS2022和Qt5.14.2成功编译MITK2022.10的过程,包括编译结果的截图、遇到的编译问题的解决方法、两个重要的注意事项(patch文件格式的修改和ITK-gitclone-lastrun文件的存在),以及参考链接。文中详细描述了如何解决编译过程中遇到的错误C2220和警告C4819,以及如何修改文件编码和尾行格式。
107 1
VS2022+Qt5.14.2成功编译MITK2022.10
|
2月前
编译QCefView+VS2019+QT5.15.2
本文介绍了如何编译QCefView项目,并在VS2019和Qt 5.15.2环境下集成,包括编译结果、要点、cmake部署Qt的方法和相关参考链接。
105 1
编译QCefView+VS2019+QT5.15.2
|
2月前
|
人工智能
三战VS2019编译VTK7.1+Qt5.15.2
本文记录了作者在VS2019环境下编译VTK 7.1与Qt 5.15.2的历程,包括正确配置方法、遇到的编译错误以及解决过程。
|
2月前
|
人工智能 C++
初步编译QT5.15.2+VS2019+VTK8.2.0
本文介绍了在VS2019中编译QT 5.15.2和VTK 8.2.0的过程,包括编译结果、cmake库编译配置和cmake应用配置脚本,以及推荐的参考链接。
|
2月前
|
计算机视觉
vs2019_qt6.2.4_dcmtk3.6.7_vtk9.2.2_itk5.3_opencv4.6.0编译记录
这篇文章记录了使用VS2019编译Qt6.2.4、DCMTK3.6.7、VTK9.2.2、ITK5.3和OpenCV4.6.0的过程,包括下载和编译步骤,并提供了遇到编译错误时的解决方案和参考链接。
vs2019_qt6.2.4_dcmtk3.6.7_vtk9.2.2_itk5.3_opencv4.6.0编译记录