2

I am looking for a way to create an infinite view on a model that is not initialized completely. I would like to create something similar to an Excel spreadsheet, and all I came in was to start with an initialized model (e.g. 100x100 empty cells, maybe working on a database that has empty values), and then just dynamically add next rows/columns (and update view) once we are close to the end of a scrollbar.

But I am wondering if it is the best solution - I think I would definitively benefit from a model that's filled in only partially - by that, I mean store information in the model only about filled cells, and let view handle showing 'empty cells' (which would have been created once we - for example - click them). I know it would be necessary to store XY positions and cell data (instead of only a 2D container with data), but I would like to try different solutions:

  • a) have a pointer-like container which would contain a list of filled cells with their positions on a 2D plane
  • b) have a 2D container with size (x,y), where x and y would mean the 'last filled cell' in a given dimension

And for both solutions, I would like to dynamically allocate more place once data is written.

So there is my question - how can it be achieved with QT model/view programming, if it is even possible to show 'ghost cells' without a model filled with empty data? It would be also nice if I could get a brief explanation of how it is done in apps like excel etc.

Maximouse
  • 4,170
  • 1
  • 14
  • 28
Lidbey
  • 251
  • 1
  • 12

1 Answers1

1

Well, your table will never be truly infinite unless you implement some indexing with numbers with infinite digit count and in that case, you will probably not be able to use Qt classes.

But I think you should choose some big enough number to define the maximum. It can be a really large number... if you are on a 64-bit machine, then your 'infinite' table can have 9,223,372,036,854,775,807 rows and the same number of columns. This big number happens to be the maximum of signed 64-bit int. And int is used for indexing with QModelIndex in QAbstractItemModel. So you can have a total of 8.5070592e+37 cells in your two-dimensional 'Excel' table. If this table size is not big enough for you then I do not know what is. Just for comparison, there are approximately 7e+27 atoms in the average human body, maybe a bit more after the covid lockdowns because people were eating instead of practicing sports. :) So you can count all atoms of all people on this planet (say there are a bit less than 10e+10 people altogether). But you will need to buy a bit bigger computer for this task.

So if you decide to go this way, then you can easily override QAbstractTableModel and display it in QTableView. Of course, you cannot save the underlying data in a two-dimensional array because you do not have enough memory. But you have to choose some other method. For example a QHash<QPoint, QString> where QPoint will represent the coordinates and QString the value (you can choose any other type instead of a string of course). Then when you will want to get the value for the given coordinates, you just look up the value in the hash table. The number of data points you will be able to hold depends only on your memory size. This solution is very simple, I guess it will be some 30 rows of code, not more.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • Qt also has a way to only populate the model as needed. See fetchMore and camFetchMore and this example at https://doc.qt.io/qt-5/qtwidgets-itemviews-fetchmore-example.html for more details – Ian4264 Jul 15 '21 at 20:30
  • I wanted to show it similiarly to QStandardItemModel, but more 'effective' - once I achieve 10k x 10k cells, it is working, but if I want to initialize a model with 100k x 10k standarditems, an app will not turn on. I believe a rectangle for example 1k x 100 initialized(by initialized I mean shown without fetching and with functionality as standard items) at once would be enough, but how to achieve it working this way? The point of my question is how to achieve it in QT, I have 10k x 10k = 100 millions of empty cells, I want to give an illusion of access to every without running out of memory – Lidbey Jul 15 '21 at 20:49
  • I understand the way of saving it as QPoint, QString, but how to actually only show standard items without actually them being saved as an object before they are clicked (or maybe I don't understood some part), by the way - thank you for a great answer :) – Lidbey Jul 15 '21 at 20:51
  • 1
    @Lidbey Forget about `QStandardItemModel`. Instead, inherit your table model class directly from `QAbstractTableModel`. This is what I advice for. This is something completely different than `QStandardItemModel`. `QStandardItemModel` may be good for beginners because it is easy to use but it will not allow you make your 'infinite' model. Have a look at this: https://doc.qt.io/qt-5/qabstracttablemodel.html#subclassing – HiFile.app - best file manager Jul 15 '21 at 21:09
  • 1
    Now you may want to ask "How the hell I subclass `QAbstractTableModel`?"... But this question has already been answered so many times. It should be easy to google for it. – HiFile.app - best file manager Jul 15 '21 at 21:11