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();
}

测试结果:

小结 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值