Results 1 to 7 of 7

Thread: Adding to a QTableview directly through a row

  1. #1
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Question Adding to a QTableview directly through a row

    Hello,

    I am trying to understand QT 4.6 model/view architecture, so I decided to make a simple bookkeeping application. I've looked over and played with the QT examples but they don't handle row entry in the way I'd like to do it.

    Basically, I want a table that starts off with one empty row (columns in this case aren't relevant). If you edit that empty row and then you hit enter, it will attempt to validate the row. If validation succeeds, that row will be added to your data set and a new empty row will appear in the table beneath it. If validation fails, that rows background color will be set to red.

    In the examples I've looked at, adding a row was done through a button, but I want my application to be more like an excel spreadsheet (in that I've always got an empty row that is used to add entries.)

    Any suggestions/examples on how to accomplish this? I am using QTableView and subclass of QAbstractTableModel, and I cant seem to figure it out.

    Thanks.

  2. #2
    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: Adding to a QTableview directly through a row

    You are sort of asking for conflicting things here - you want your model to contain only valid data, but yet you want the table to contain both valid and invalid entries. The way the model / view architecture works, the view is the display of the contents of the model so conversely everything shown in the view must come from the model in some way.

    However, I think you can do a trick here. Make your model contain two parts: the first part is the collection of validated rows, and the second is the single row representing the current entry. The rowCount() is the number of valid rows plus one.

    It gets harder if you allow your user to edit in the middle of the set of valid rows, not just the last one. It is doable, but still boils down to keeping two collections, one of valid and one of invalid rows. The user is allowed to save the table only if there are no invalid rows except for the empty one.

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

    SpiceWeasel (9th February 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding to a QTableview directly through a row

    Another approach would be to use a proxy model to add the extra row to keep the original model clean. However that's probably not the wisest choice for a first date with model/view architecture.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    SpiceWeasel (9th February 2015)

  6. #4
    Join Date
    Feb 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding to a QTableview directly through a row

    The user is allowed to save the table only if there are no invalid rows except for the empty one.
    Last edited by wysota; 8th February 2015 at 23:10. Reason: removed URL

  7. #5
    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: Adding to a QTableview directly through a row

    That doesn't sounds so difficult on first sight.

    Your model reports a rowCount() that is one up from what you have actual data for, thus displaying an empty row at the end.

    In your row data structure you have a flag that marks that row as either valid or invalid.
    Your data() method uses this to return the respective background color.

    Cheers,
    _

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

    SpiceWeasel (9th February 2015)

  9. #6
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Adding to a QTableview directly through a row

    I am using rowCount() + 1 and that indeed provides an empty row to work with. But where in the code do I check for the enter key being pressed while on that row -- and how do I check for it? Is there a signal/slot mechanism that can handle this (in one of the classes I am using)? Do I have to subclass QTableView and add that functionality? Is it something the main window should handle?

  10. #7
    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: Adding to a QTableview directly through a row

    Your model needs to implement QAbstractItemModel::setData() and emit the dataChanged() signal as described in the docs. The default item delegate used by the table view should handle the return key and call this method with the new value. (In other words, the base QTableView will just work). You also need to implement the QAbstractItemModel::flags() method to add the Qt::ItemIsEditable flag to the defaults for thhose items the user is allowed to edit.
    Last edited by d_stranz; 10th February 2015 at 00:08.

  11. The following user says thank you to d_stranz for this useful post:

    SpiceWeasel (10th February 2015)

Similar Threads

  1. QTableView + QAbstractItemModel and adding rows
    By karlkar in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2016, 21:45
  2. Adding button to QTableview .
    By riarioriu3 in forum Newbie
    Replies: 9
    Last Post: 21st October 2013, 07:48
  3. Adding columns to qtableview
    By pcheng in forum Newbie
    Replies: 8
    Last Post: 11th July 2012, 08:58
  4. Adding a OK and Cancel button to QTableview
    By rex in forum Qt Programming
    Replies: 0
    Last Post: 24th July 2011, 17:32
  5. Problem with adding rows into QTableView
    By januszmk in forum Newbie
    Replies: 8
    Last Post: 14th July 2011, 09:20

Tags for this Thread

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.