PDA

View Full Version : Table Model/View Pre-allocate num of rows/cols



phil_n
16th December 2015, 16:11
I'm working on an attempt to implement a tableview/model. The snippet/example I am looking at pre-allocates the number of rows and columns by using them as limiting factors in the loops that load the items. Do I really have to worry about that or will the model simply expand as necessary? (Until I get to the end of the file.)

Thanks.

anda_skoa
16th December 2015, 19:05
A model is a form of standardized interface to data.

Whether that data is stored in the model or stored elsewhere and just accessed by the model is a decision the model developer makes.

If the data or the amount of data changes, then the model can signal the view(s) and they update themselves.

Cheers,
_

d_stranz
16th December 2015, 20:11
If you derive your own model class from QAbstractItemModel (or one of its derived classes), you will need to emit the columnsAboutToBeInserted() / columnsInserted() signals and/or the row equivalents as you dynamically change the model so that listeners (eg. views) will know to update their appearance. A sledgehammer approach is to emit the modelAboutToBeReset() signal, replace/update the contents of your model, then emit the modelReset() signal. This will result in views being completely updated under the assumption that the original model content has been completely changed. If the model content is small, this can be the simplest approach to implement.

In a derived model, you'll also need to implement rowCount(), columnCount() and other virtual methods so that when views update themselves, they know the model dimensions and can retrieve the data for display.

phil_n
16th December 2015, 20:19
I guess what I meant was, there might be one file with 1000 records and 15 fields and another with 500 records and 12 fields. Does the model (or the tableview?) need to know this every time a new file is opened or will it dynamically adjust to the number of records and fields that I feed it? This is my first project so I'm very wet behind the ears still. In other words....do I need to query the data source to get the exact number of rows and columns and set my loading loops up with those exact figures or can I just 'load the model' (or the table?) until end-of-file without having to know before hand how many rows and columns there are?

d_stranz
16th December 2015, 20:29
Your strategy in this case should probably be the sledgehammer approach. Note that I misspoke earlier when I said your model emits signals; you actually call the protected functions, which result in the base class emitting the signals for you.

- call beginModelReset()
- clear out your internal data structure you use to hold the cells
- read your file and reallocate your internal data structure accordingly
- call endModelReset()

QAbstractItemModel and the derived QAbstractTableModel have no predefined internal storage. They are simply definitions of interfaces to arbitrarily- and table-structured data, respectively. You have to back them up with some sort of internal structure, in your case maybe just a list of lists (i.e. a list of rows each containing a list of columns) or a vector of vectors. You implement the rowCount(), columnCount(), data(), and other virtual methods as needed to return values appropriate for the current size and content of your data structure.

QTableView listens for the signals emitted by the model and will automatically update its content (via calls to the model methods you reimplement).

phil_n
16th December 2015, 23:25
Thank you again. All good stuff to think about. I would say good stuff to know, but that's going to take a while.