PDA

View Full Version : Why do some QWidgets create own threads?



donglebob
14th December 2008, 18:10
Hi,

I am working on a multi-threading program, which has the ability to create dynamically pages for the Tab-Widget.
These pages for the tab-widget are made of a self made page-template class.

In that template I have the whole widgets I need to create that page.
All of them are created on the Heap.

My theory is:
my_programs_core (5 Threads) + main_gui( 1 Thread)

But it is:
my theory(6 Threads) + QLineEdit of mainGUI(2 Threads) + QFileDialog for save and load (2 Threads)..

My question is:
Why do a QLineEdit located on my main-page (not on the pages for the Tab-Widget)
creates 2 Threads?

And in my template-class there are two QFileDialog instances for each page. (Load file / save file)
It means every time I create a new page with my template class I will get 2 Threads without a reason.

Can someone explain me that?

wysota
14th December 2008, 18:21
QFileDialog might be using a separate thread for populating QFileSystemModel. QLineEdit shouldn't be using separate threads unless you are using a completer with QFileSystemModel.

donglebob
14th December 2008, 19:04
Thanks,

you are right


QFileDialog *aaa = new QFileDialog(this);

somewhere in the code:
model = new QFileSystemModel(q);


But QLineEdit creates 2 Threads in my simple case.



QLineEdit *bbb = new QLineEdit(this);


Somewhere in the code in its init


q->setAcceptDrops(true); //here it creates 2 threads



:confused::confused:

donglebob
14th December 2008, 20:29
Am I the only one where QLineEdit creates threads?

wysota
15th December 2008, 02:19
QLineEdit is not using any threads, at least not by itself. Why did you think it was using threads?

Strix Code
6th July 2010, 18:01
donglebob is right. Instantiating QLineEdit creates threads of its own. On my system (Windows 7, Qt 4.6.2) it creates 4 threads! Actually, the threads are created inside QWidget::setAcceptDrops(), because QLineEdit calls this method inside the constructor. After a bit more debugging I've found out that the threads are created by a WinAPI function CoLockObjectExternal (http://msdn.microsoft.com/en-us/library/ms680592(VS.85).aspx)

Edit: I've found a way to prevent this behavior. One must call
QLineEdit::setAcceptDrops(false);before the widget is shown.