Results 1 to 7 of 7

Thread: QTableView, add new record?

  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow QTableView, add new record?

    In most database applications that I've seen, the widget that displays the table data has an extra (blank) row at the bottom that the user can use to insert/append a new record into the database.

    How can I do that with a QTableView?

  2. #2
    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: QTableView, add new record?

    What did you already try?

  3. #3
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView, add new record?

    I haven't tried anything... I don't know what to try, that's why I'm asking.

    - Gordon E.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView, add new record?

    One option could be to subclass one of QSqlXxxModels, fake an extra row by reimplementing rowCount() and setting index widgets or opening persistent editors at the bottom row. Depending on types of editors, you could for example connect to QLineEdit::returnPressed() and insert the row on demand.
    J-P Nurmi

  5. #5
    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: QTableView, add new record?

    In my opinion you don't really need to do anything - all that is needed is to call insertRow() on the model and edit the row (either automatically or letting the user start the editor). That's why I asked the question in the first place.

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

    grellsworth (5th July 2007)

  7. #6
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView, add new record?

    Quote Originally Posted by wysota View Post
    In my opinion you don't really need to do anything - all that is needed is to call insertRow() on the model and edit the row (either automatically or letting the user start the editor). That's why I asked the question in the first place.
    OK, I've tried adding a new row into my model, this looks like it might work. I'll let you know. I'm just concerned that after a new row is inserted, that a new blank line will appear.

    Thank you for your help. I'll keep you posted on how it's going.

    Sincerely,

    Gordon E.

    ===== UPDATE =====

    Here's what I'm doing right now:

    Qt Code:
    1. void SQLiteDB::addRow(QTableView *tableView)
    2. {
    3. QAbstractItemModel *model = tableView->model();
    4.  
    5. model->insertRow(model->rowCount());
    6. }
    To copy to clipboard, switch view to plain text mode 

    This works fine for adding the initial empty row at the bottom of the table, but now I have to find a way to make sure that when the user successfully enters a new record, a new empty row will be added at the end.

    Suggestions?

    Sincerely,

    Gordon E.
    Last edited by grellsworth; 5th July 2007 at 13:50.

  8. #7
    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: QTableView, add new record?

    Use signals and slots to connect to some signals where you will check whether the row has been successfully edited and create a new row.

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

    grellsworth (5th July 2007)

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  4. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

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.