问题:
RTTI symbol not found for class ‘QWidget’
现象:
关闭主窗口提示段错误
部分源码:
Destroyed类的定义
#include "destroyed.h"
#include <QDebug>
Destroyed::Destroyed(QWidget *parent) : QWidget(parent)
{
this->setAttribute(Qt::WA_DeleteOnClose);
}
Destroyed::~Destroyed()
{
qDebug()<<"Destroyed::~Destroyed()";
}
Destroyed类的声明
#ifndef DESTROYED_H
#define DESTROYED_H
#include <QWidget>
#include <QMainWindow>
class Destroyed : public QWidget
{
Q_OBJECT
public:
explicit Destroyed(QWidget *parent = nullptr);
~Destroyed();
signals:
};
#endif // DESTROYED_H
主函数调用:
#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include "destroyed.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Destroyed d;
d.show();
d.setWindowTitle(u8"f反对");
return a.exec();
}
分析:
首先,导致这个问题的原因是找不到QWidget符号,在QWidget派生类里设置this->setAttribute(Qt::WA_DeleteOnClose);
属性,WA_DeleteOnClose的作用是在窗口关闭时,调用析构函数销毁此窗(QWidget关闭时默认是隐藏的),所以,我先关闭d窗口,实际的d窗口已经被销毁,然后我在关闭主窗口,主窗口会在调用d窗口的销毁函数,但是此时d窗口已经被销毁,所以会提示找不到QWidget
修改:
采用如下方式调用:
#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include "destroyed.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Destroyed* d = new Destroyed;
d->show();
d->setWindowTitle(u8"f反对");
qDebug()<<"currentDateTime = "<<QDateTime::currentDateTime();
qDebug()<<"currentDateTimeUtc = "<<QDateTime::currentDateTimeUtc();
return a.exec();
}
这样,主窗口在关闭时不会再调用d的析构函数。这里面涉及到一个知识点就是,在父窗口的对象,当父窗口销毁时,其子窗口也会销毁