Results 1 to 3 of 3

Thread: How to update QTableView from SQL database after adding data to database?

  1. #1
    Join Date
    Apr 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to update QTableView from SQL database after adding data to database?

    I have 2 classes, one is a mainwindow (iherits from QMainWindow), and second is addwindow (iherits from QDialog). In mainwindow I have QTableView connected with database, and buttons like "add data", "remove data". In addwindow I put data and click "ok" button to add data to database. Now, how could I call "tablica->select()" to update QTableView after clicked "ok" button in addwindow?

    Problem is that QSqlRelationalTableModel associated with QTableView is included in mainwindow constructor, and method adddata is included in addwindow class.

    There are my header and c++ files.

    mainwindowh.pngmaincpp.jpgmaincpp2.pngaddwindowh.pngaddwindowcpp.jpg

    EDIT: I just found after hours, that thing I supposed to do was add connection in clicking "add data" slot in mainwindow class.
    Last edited by Voonter; 5th April 2014 at 21:48.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,630
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to update QTableView from SQL database after adding data to database?

    If you do the addition or modification through the same model (with addRows(), removeRows() or setData()), as the view is looking at then then the updates become available to the view. If you, or something else, updates the underlying database directly then you have no choice but to requery the database.

  3. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to update QTableView from SQL database after adding data to database?

    Or set table model with QSqlQueryModel and after adding data call a slot which contains model->setQuery(<your query>) which will update the view.

Similar Threads

  1. Adding new record to database from QDataWidgetMapper
    By naptizaN in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2012, 13:19
  2. How to update BLOB field in a SQLite database ?
    By aircraftstories in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 20:45
  3. QSqlTableModel the database does not update
    By Zander87 in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2011, 22:34
  4. i can't update database,
    By xiongxiongchuan in forum Qt Programming
    Replies: 7
    Last Post: 29th June 2010, 14:44
  5. Replies: 1
    Last Post: 7th June 2008, 18:29

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.