0

I have some newbie question. I'm not sure if I understand Model-View-Controller design pattern correctly.

Maybe I will start from describing my problem. I'm reading some data from file. This can take let's say 10 seconds and after that I present those data in the table. The question is how should I store those data? Besides QStandardItemModel should I have another container to which I will read data from file and use it in other threads? For example, I have QStandardItemModel in Gui thread and I create another container let's say QVector. I load data from file to QVector and then I move data from QVector to QStandardItemModel?

I can't use QStandardItemModel in other thread and read data directly from file to QStandardItemModel because GUI freeze as I know because QStandardItemModel emits signal to update view after appendRow.

Or maybe I should create custom model and add method to update view only when whole file is loaded into QStandardItemModel? QTableView has own container for data and it's not synchronized with the model? Is it possible/correct way?

What with situation when I read a new file?

Thanks for any help.

Kirill Chernikov
  • 1,387
  • 8
  • 21
Cherubim
  • 55
  • 2
  • 8
  • You want to load data from file only one time or some times? – Kirill Chernikov Aug 28 '16 at 15:07
  • Yeah, more than one. In case of only one then I think I can share QStandardItemModel between GUI and read file thread. When I read new file I would like to fill model only with new data. I was thinking about setting new model for every time I read file but I think it's not nice and efficient solution. btw. in that case how to remove memory from old QStandardItemModel? – Cherubim Aug 28 '16 at 15:22
  • if you take *10 seconds* to read the file into the model, I guess you have a **very big** number of rows/columns. I would suggest not loading them at once in a `QStandardItemModel`, and do some lazy loading using `QAbstractItemModel`, [`canFetchMore`](https://doc.qt.io/qt-5/qabstractitemmodel.html#canFetchMore), [`fetchMore`](https://doc.qt.io/qt-5/qabstractitemmodel.html#fetchMore). note that this is the strategy used internally in [`QSqlQueryModel`](https://code.qt.io/cgit/qt/qtbase.git/tree/src/sql/models/qsqlquerymodel.cpp#n190) when loading *large* models. – Mike Aug 28 '16 at 19:37
  • Take a look at [Fetch More Example](https://doc.qt.io/qt-5/qtwidgets-itemviews-fetchmore-example.html) from Qt. – Mike Aug 28 '16 at 19:38

1 Answers1

0

I think you can do such steps for resolve your problem:

  1. When you need to load new data from file, set model for your QTableView in nullptr.
  2. Fill QStandardItemModel (this model was the model for your QTableView) in non-main (non-GUI) thread with data from file.
  3. Set model with data for your QTableView in main (GUI) thread.

Of course, the simplest way to solve your problem also may be to show for user something like this: "Data loading, please wait...", while you load data in QStandardItemModel in main (GUI) thread.

Kirill Chernikov
  • 1,387
  • 8
  • 21