通过槽和信号在不同线程中的两个qt对象之间进行通信

Communicate between two qt objects in different threads through slots and signals

我想连接两个在不同线程中运行的 qt 对象。(download_webm 和播放器)

int main(int argc, char *argv[])
{


    QApplication app(argc, argv);

    DownloadWebm *download_webm;

    MyThread *DownloadWebm_Thread = new  MyThread(download_webm);

    DownloadWebm_Thread->start();

    LinuWebmPlayer *player = new LinuWebmPlayer(argv[1],0);

    QObject::connect(download_webm,SIGNAL(send_packege(Video_Bytes_Package)),player,SLOT(play()));

    player->show();





    return app.exec();
}

MyThread 头文件:

#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <qthread.h>
#include <downloadwebm.h>
class MyThread : public QThread
{
    Q_OBJECT

public:
    MyThread(DownloadWebm *&we);
    MyThread();


    DownloadWebm **getWebm() const;

protected:
    DownloadWebm **webm;
    void run();
};





#endif // MYTHREAD_H

和 cpp:

#include "mythread.h"

MyThread::MyThread()
{

}

DownloadWebm **MyThread::getWebm() const
{
    return webm;
}

MyThread::MyThread(DownloadWebm *&we)
{
    webm = &we;
}

void MyThread::run()
{
   *webm = new DownloadWebm("http://trilulilu.de.de/recstreamingsource?movie=3860","asd");
}

如果我从 main 注释 QObject::connect 行,一切正常,关于 qt 中线程之间的通信,我是否遗漏了什么?

................................................ .........

查看 Qt docs 我们可以看到 connect/disconnect:

Note: These functions are also thread-safe:

我们可以注意到的另一件事是 connect 接受 Qt::ConnectionType 它将告诉 Qt 如何管理连接。

请参阅 this 以避免意外。

问题在于 download_webm 程序将指针视为未初始化,所以我认为,但我在 mythread 构造函数中进行了初始化