简体   繁体   English

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

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

I want to connect two qt object that runs in different threads.(download_webm and player)我想连接两个在不同线程中运行的 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 header file:我的线程头文件:

#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

and cpp:和 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");
}

If I comment the QObject:: connect line from the main everything works fine, is there something I miss regarding communication between threads in qt?如果我评论主要的 QObject::connect 行,一切正常,关于 qt 中线程之间的通信,我有什么想念的吗?

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

Looking at Qt docs we can see that connect / disconnect :查看Qt 文档我们可以看到connect / disconnect

Note: These functions are also thread-safe:注意:这些函数也是线程安全的:

and another thing that we can notice is that connect accepts Qt::ConnectionType which will tell Qt how to manage the connection.我们可以注意到的另一件事是connect接受Qt::ConnectionType ,它将告诉 Qt 如何管理连接。

Please see this to avoid surprises.请查看内容以避免意外。

The problem is with download_webm the program sees the pointer as uninitialized so I think, but I initialized in the mythread constructor问题是 download_webm 程序认为指针未初始化所以我认为,但我在 mythread 构造函数中初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM