简体   繁体   中英

Using QSqlQueryModel in another thread and access it directly by pointer from main thread?

I've used a subclass of QSqlQueryModel to expose the data for a ListView in my QML project. I want to do both of these in the same time:

  1. Expose the model to QML using setContextProperty() with the model's pointer .

  2. Leave the model in another thread so when I call the model update() function (executing the last query to reflect the data changed in database), the model won't block the UI when it's requerying the database.

Is it possible? If not, what is the better solution?

My solution is: Subclass QThread , pass the QmlApplicationViewer pointer to its contructor and inside the new class DatabaseQueryThread , I set the context property for the viewer there. This way, Qt will allow me to provide access to the query model directly by pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM