2

Previously, I asked about running model and view in different threads and now, I understand how it poses a problem. To circumvent the problem, I was thinking of duplication of data. When data is modified by the worker thread, it simultaneously sends an update to GUI/Model thread to update the model with the same data (so there exist a exact copy of data with the GUI/model).

Does this sound like a plausible approach? Other method is to use direct widgets with default models like QTableWidget etc. but then I need to do a lot of things that are powered by QTableView by default.

Please suggest!

Link to other question: Design Pattern, Qt Model/View and multiple threads

Community
  • 1
  • 1
shiv chawla
  • 591
  • 1
  • 8
  • 20
  • Sounds good. You can use queued signal/slot connections for the updates. Unless you have huge amount of updates or the data is very expensive to copy, I don't see a problem. If the worker doesn't need to keep the existing data, it can just send updates and forget, to avoid duplicated RAM usage. – Frank Osterfeld Mar 20 '12 at 19:22

1 Answers1

0

There are 2 scenarios that are possible but in either case you will need at least 2 objects as follows:

Data Object

Contains all functions required for maintaining and manipulating data. Data is stored in Pointers to objects describing data. Data Structures for this I will leave up to you.

Model

Contains a vector of pointers to data that is currently being displayed

Scenario 1

Data in QTableView is display only in which case on a QTimer signal or a different signal the data storage is locked and vector for display model is cut and put into the model and reset() is called to have QTableView repainted with new data.

Scenario 2

You can manipulate data via custom ItemEditor in QTableView. In this case the top widget that has QTableView should have references to your data object and QTableView and a signal handler for the signal emitted by the ItemEditor when the edit is complete. This signal handler will then lock the data structure inside data object and update it, once done it may cut a new vector to display but more often then not it won't have to.

How you will set up the locking, searching, etc is entirely up to you.

Karlson
  • 2,958
  • 1
  • 21
  • 48