Important: Please read the Qt Code of Conduct -

What is thread-safe?

  • I've seen some references to thread-safety in various places, but I can't find the documentation in the docs for 4.7.
    I'm starting here and looking for thread-safety topics with no luck.
    I expected to see a "best practices" note about threads or something.
    Can someone help me out starting here:

    I'm doing a file management program that is building content in a QTreeWidget and I'd like to build the QTreeWidgetItems in another thread and add them to the widget there while the rest of the GUI is alive. That would be the best case. I've seen that "Containers are thread-safe" but I don't want to make unwarranted assumptions about the GUI without reviewing the docs.


  • Th Qt UI stuff MUST be used inside the main thread, Only data classes are (some of them) thread safe. If you are doing some file management, I would suggest using a model view approach and not a QTreeWidget approach. It'smuch more optimizable...

  • Agreed with Gerolf, model-view will give you much more flexible solution. Also maybe it will be ok to you to use QFileSystemModel instead of writing your own.

  • About documentation, this "link": is pure gold ;) in my opinion
    You have there links to both: Model-View and Threading + other Qt concepts

  • Thanks. My application is not exactly a FileSystemModel, but I'll have another look at the Model View stuff.

    Well shucks. Bummer that the containers of the View / widget are not thread-safe.


  • You can create class that will inherit FSM and change something you needed there. Maybe it will save you some time in developing your app.

  • It's perfectly ok to have some worker threads in the background, that send their data via signal/slot connections to the model. That's the way the Trolls do it in the QFileSystemModel - have a look in the sources (start with src/gui/dialogs/qfileinfogatherer.cpp).

Log in to reply