Take the 2-minute tour ×

I have just started crawling in Qt. Installed qt4 on my Ubuntu machine and tried some basic examples. Now I am trying a develop a multithreaded application. Firstly I am confused whether to use pthreads or Qthreads. Can someone tell me which one is better to use.?

Further in my application, I will send some values from one thread to another thread. The 2nd thread will print those in a box. My query is, whether a thread other than main thread, can add a widget to the main window?

share|improve this question
You should post each of your questions as a separate question. –  sashoalm Oct 15 '12 at 9:26
add comment

2 Answers

You could use QApplication which has a static method topLevelWidgets() in which you can "find" one of your main widgets.

Like this:

#include <QApplication>   

foreach(QWidget *widget, QApplication::topLevelWidgets())
    if(widget->accessibleName() == "YourMainWidgetName")
        myMainTopWidget = widget;

When you send value from one thread to another, then use signals and slots. They are thread-safe and easy to use.

share|improve this answer
add comment
  1. Use QThread. It uses pthreads internally, but you don't rely on pthreads and can (potentially) run your program on other platforms and OSes just as well.
  2. You cannot create QWidget instances from worker threads. There is only one GUI thread in Qt: the thread which runs main(). Only this thread may create anything which communicates with the window manager (widgets and pixmaps).

So between threads, you communicate data only. The GUI thread manages the graphical user interface by itself, and just accepts data from worker threads. Have a look at the Qt documentation for how to implement multi-threaded applications.

share|improve this answer
add comment

Your Answer


