This is the Qt way of updating GUI from another thread. Since yours does not work out, I think you can paste some code, and we'll analyse.
If you list is not too large, QListWidget is OK, but if your list is very big, then better use QListView, because for QListWidget, each item rendered in the list is itself a widget, which will take a lot of memory. But in QListView, it is the delegate of the view responsible for drawing all the items.
Creating multithread GUI application using Qt on Windows. How to use QListWidget?
-
07-12-2021 - |
Question
I want to make a find utility for Windows using Qt. I have created the same application in C# and .NET and I want to make a comparison. My application will have to use QDir::entryList
recursively to find the contents of the specified path and add them to a QListWidget
. Because the entryList maybe slow , it have to run in a QThread
. In addition the user should not await for a long time to see some results but the QListWidget
should be populated with some results.
I have done the following:
- I have created a class that does the searching , this class emits a message
sendItem(QString)
when it finds a file. - This class is run in another thread with
f.moveThread(&thread)
wheref
is an object of the find class. - Because QListWidget does not have a slot I have created another class
wrapper
that has a slotaddItem(QString)
an object of this class is executed in the GUI thread so when receives the messagesendItem
the slotaddItem
does the followingl.addItem(s)
wheres
is the data that is transmitted withsendItem
signal.
Is this the optimal way to update a QListWidget from another thread? Because it is very complicated, (and it does not work) but I do not know a better one. In C# I just created a delegate FillList
I put in a delegate object a method fillList
. I just gave a BeginInvoke
and from within the fillList
method I called this.Dispatcher.invoke
using as an argument the addItem
method of wpf listbox. You can see the C# application in sourceforge.net
And other question: Is it preferable to use the QListView
for this kind of application?
Thanks for the answer.
Solution