2

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.

  • 1
    In general, dont subclass Qthread unless. This is a recurring topic [which has been answered](http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/) – UmNyobe Dec 05 '13 at 10:00

2 Answers2

1

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.

Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

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.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • I already have an event loop (from libevent library) already in my main thread. That is why i wanted an event loop in another thread for my post requests – user3065039 Dec 05 '13 at 14:07
  • Qt is a multiplatform development environment that provides an event loop with QCoreApplication or QApplication. Why are you using the libevent library instead? – TheDarkKnight Dec 05 '13 at 14:12
  • It is code that was used previously and we don't want to throw it away. – user3065039 Dec 05 '13 at 14:16
  • I would suggest you do move to Qt, but regardless, to answer your question more directly, you'd create the workerObject in the main thread, set its connections for signals and slots and then move it to the thread with WorkerObject->moveToThead(MyThread); Then use signals and slots to communicate between the main thread and the new MyThread. – TheDarkKnight Dec 05 '13 at 14:49