Results 1 to 6 of 6

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

  1. #1
    Join Date
    Mar 2014
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Table Model/View Pre-allocate num of rows/cols

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Table Model/View Pre-allocate num of rows/cols

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    phil_n (16th December 2015)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Table Model/View Pre-allocate num of rows/cols

    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.
    Last edited by d_stranz; 16th December 2015 at 20:22.

  5. #4
    Join Date
    Mar 2014
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Table Model/View Pre-allocate num of rows/cols

    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?

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Table Model/View Pre-allocate num of rows/cols

    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).
    Last edited by d_stranz; 16th December 2015 at 20:34.

  7. #6
    Join Date
    Mar 2014
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Table Model/View Pre-allocate num of rows/cols

    Thank you again. All good stuff to think about. I would say good stuff to know, but that's going to take a while.

Similar Threads

  1. Table view with collapsible rows
    By adith387 in forum Newbie
    Replies: 4
    Last Post: 16th September 2010, 15:07
  2. Refreshing one or more rows in a table view
    By William Wilson in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2009, 00:10
  3. How to set two rows in a table view Horizontal header?
    By sivollu in forum Qt Programming
    Replies: 11
    Last Post: 29th April 2009, 04:57
  4. Removing rows from table model
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 20:36
  5. Resize rows and cols in a Tablewidget
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2007, 12:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.