QT 编译不过的另一个问题:
1. 新建一个console工程
QT -= gui CONFIG += c++11 console CONFIG -= app_bundle # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp baseclass.cpp subclass.cpp subclass2.cpp #grandclass.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += baseclass.h subclass.h subclass2.h #grandclass.h
2. 添加baseClass
baseclass.h
#ifndef BASECLASS_H #define BASECLASS_H #include "QObject" class baseClass: public QObject { Q_OBJECT public: baseClass(QObject * p = 0); virtual ~baseClass() {} virtual void vMethod(int) = 0; signals: void signal1(); }; #endif // BASECLASS_H
baseclass.cpp
#include "baseclass.h" baseClass::baseClass(QObject * p) : QObject(p) { }
3. 添加subClass
subclass.h
#ifndef SUBCLASS_H #define SUBCLASS_H #include "baseclass.h" class subClass: virtual public baseClass { Q_OBJECT public: subClass(QObject * p = 0); virtual ~subClass() {}; virtual void vMethod(int) = 0; signals: void signal2(); }; #endif // SUBCLASS_H
subclass.cpp
#include "subclass.h" subClass::subClass(QObject * p) : baseClass (p) { }
4. 添加subClass2
subclass2.h
#ifndef SUBCLASS2_H #define SUBCLASS2_H #include "baseclass.h" class subClass2 : virtual public baseClass { Q_OBJECT public: subClass2(QObject * p = 0); virtual void vMethod(int) = 0; }; #endif // SUBCLASS2_H
subclass2.cpp
#include "subclass2.h" subClass2::subClass2(QObject * p) : baseClass (p) { }
5 main.cpp
#include <QCoreApplication> #include "subclass.h" //#include "grandclass.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //grandClass * sub = new grandClass(); return a.exec(); }
编译报错:/build-testQtSignalBug-Qt5_9_4_zynq-Debug/moc_subclass.cpp:68: error: cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'
subClass *_t = static_cast<subClass *>(_o);
^
解决方法: 将subclass里的signals 删掉, 即不在subclass里声明信号量。
原因:不详。