QT的错误记录:同文件的的两个QThread子类信号槽关联
珍贵的记录
原本很简单的一个小测试代码,我原来把两个QThread子类放在一个文件里:结果报错
-1: error: ld.lld: error: undefined symbol: vtable for RecvThread
:-1: error: ld.lld: error: undefined symbol: vtable for SendThread
也没搞明白为什么,分开存放就OK了,先记录一下
一文不值的代码
RecvThread.h
#ifndef RECVTHREAD_H
#define RECVTHREAD_H
#include <QObject>
#include <QThread>
#include <QDebug>
class RecvThread : public QThread
{
Q_OBJECT
public:
void run() override {
qDebug() << "RecvThread start";
QThread::sleep(3);
qDebug() << "RecvThread exit";
}
public slots:
void recv_msg(QString str) {
qDebug() << "Setting string in OutputThread:" << str;
}
};
#endif // RECVTHREAD_H
SendThread.h
#ifndef SENDTHREAD_H
#define SENDTHREAD_H
#include <QObject>
#include <QThread>
#include <QDebug>
class SendThread : public QThread
{
Q_OBJECT
public:
void run() override {
qDebug() << "SendThread start";
QString s("hello friend");
emit send_msg(s);
QThread::sleep(1);
qDebug() << "SendThread exit";
}
signals:
void send_msg(QString str);
};
#endif // SENDTHREAD_H
main.cpp
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <QString>
#include "SendThread.h"
#include "RecvThread.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SendThread send_thread;
RecvThread recv_thread;
QObject::connect(&send_thread, SIGNAL(send_msg(QString)), &recv_thread, SLOT(recv_msg(QString)));
qDebug() << "connect finish";
emit send_thread.send_msg("hello one");
send_thread.start();
recv_thread.start();
send_thread.wait();
recv_thread.wait();
return a.exec();
}
测试结果: