简体   繁体   中英

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)

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:

#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?

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

Looking at Qt docs we can see that 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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