user3065039
user3065039

Reputation: 41

Creating another network thread in qt

I am new to qt and trying to create a qt application. As of now , the application has 1 thread which runs libevent event loop . I wanted to create another thread (which would be a permanent networking qt thread in charge of ssl requests).

I have created WorkerObject that subclasses QOBject. In addition i have a MyThread that subclasses QThread which runs event loop inside the run method. I create this thread inside main and kill it when application ends.

All of network operations i wanted to inside WorkerObject (including post request). How and where do i create appropriately WorkerObject so all of its signals and signals are processed inside event loop of MyThread. Because for instance I need to call method (which sends the post/get request) whenever user wants me to sends it.

Thanks in advance for answers.

Upvotes: 2

Views: 930

Answers (2)

TheDarkKnight
TheDarkKnight

Reputation: 27611

Unless you're creating a server that's going to be handling a huge amount of simultaneous requests, I recommend that you don't use threads in this situation. Qt's network classes are asynchronous, so you can quite easily continue to use them on the main thread. Take a look at QSsLSocket, which is derived from QTcpSocket. There are examples of how to create the server and clients with Ssl and how to use them.

Upvotes: 0

UmNyobe
UmNyobe

Reputation: 22890

The condition on using objects inside QThread is that they have to communicate only via signals and slots once the thread is started. So event if workerobject need to start working, this has to be triggered via a signal.

Take a look at this answer on a similar question, it is the recommended way of doing it.

Basically, you connect the started signal of Qthread to workerobject starthandlingSSLrequest. And then start your thread where you would have called starthandlingSSLrequest object if it was not multithreaded,.

Whenever the user want you to do something, you just trigger the signal in the main loop and all slots which are connected will be later exectuded.

Upvotes: 1

Related Questions