Multi threading with Model View framework?
Please forgive me because this is my first application in GUI as such & after reading some suggestions on this forum & the articles I have now understood the concept of Model/View framework.
But I am unable to implement it in my multi threaded application because I am unable to decide which classes are good for multi threading. Let me tell something about my application:
Basically I am storing a 2D array of double variables for performing scientific calculations in my application. I was previously using QTableWidget for displaying this data to the user. It was fine till array size was 1000x1000 & single threaded. But when I multi threaded the initialisation of data from reading a file, it gave very bad performance. Hence moving to Model/View framework.
My usage is:
Opening the file in each thread, reading data from it & setting it to the GUI. Each thread has a pre-decided set of rows which it initialises. (EX: for 1000x1000, if there are say 4 threads then thread 1 will initialise 1-250, thread 2 will initialise 251 - 500, & so on). So far I am setting the data to a double array along with the table. If I comment out the QTableWidget code then I get a tremendous performance boost. This means that my existing multi threaded code is good & the problem is only when I set the data to QTableWidget & not the double array.
Any suggestions on how I should implement the Model/View framework?
You should look into making your own model (QStandardItemModel or QTableModel) and use a QTableView. You are currently loading what seems to be a lot of data then duplicate it in your QTableWidget which will impact on your performance.
Hope it helps
Can I add data to QStandardItemModel from multiple threads simultaneously?
If yes then is it fine if I keep a pointer to the model in each thread & then add data to it from each thread?
Also I think the QTableView should be in the GUI thread, but what about the QStandardItemModel? Is it ok to keep it in different thread without affecting performance?
All QObjects (including QStandardItemModel) are not thread-safe, so you cannot write to them simultaneously from multiple threads. Each object must only be accessed from one thread.
Keep your QStandardItemModel in the GUI thread, and implement a slot to update its data. When each thread has finished initializing the data, emit a signal to tell the slot to update the QStandardItemModel.