I'm afraid it is not a good example code as QSqlDatabase connection does not support multithreading. So when you want to use database in a thread you have to create new database connection in every thread. And remember that in QThread class only the body of run() method is in separate thread, body of the QThread constructor for example (so constructing member variables) is in the constructors caller thread. So for eaxample creating connection to database (so queries using that connection also) sould be done in run() method and they sould be run() method local variables or created on a heap in run() thread (then you can have member pointer variables).
Bookmarks