Results 1 to 3 of 3

Thread: Can't update data in QSqlTableModel when the pk Column hidden

  1. #1
    Join Date
    Jun 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Can't update data in QSqlTableModel when the pk Column hidden

    Hellow there;

    I'm trying to view data from a simple DB table to a QtableView.
    I used the Sample Code supplied in the QSqlTableModel's Documentation page with a minor modification.
    I have a STUDENT table with id pk and name
    Qt Code:
    1. -- the DB engine is sqlite:
    2. create table student (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(20) not null);
    To copy to clipboard, switch view to plain text mode 
    and i modified the code to be :
    Qt Code:
    1. model = new QSqlTableModel;
    2. model->setTable("student"); //that's the table name
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit); // whatever edit strategy, it still not working
    4. model->select();
    5. model->setHeaderData(0, Qt::Horizontal, tr("id"));
    6. model->removeColumn(0); // don't show the ID; <<-- here is the PROBLEM
    7. model->setHeaderData(1, Qt::Horizontal, tr("name"));
    8.  
    9. ui->tableView->setModel(model);
    10. ui->tableView->show();
    To copy to clipboard, switch view to plain text mode 
    data is shown and everything is fine. But when I try to update a recoed ' the name field for a given record'; I get a debug message saying :
    Qt Code:
    1. QSqlQuery::value: not positioned on a valid record
    To copy to clipboard, switch view to plain text mode 
    and the rows don't update
    If I just commented the removeColumn() line; data updates when I modify them and everything works fine.
    BUT I need to remove the ID column - i.e keep it hidden from users - ! it is the PRIMARY KEY and I cant just let users manipulate it

    Is there a way to hide the ID column and still be able to update 'the other column values that are NOT id' ???

    thanx alot in advance

  2. #2
    Join Date
    Oct 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't update data in QSqlTableModel when the pk Column hidden

    hi,
    try view->hideColumn(0) instead model-> removeColumn(0)

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can't update data in QSqlTableModel when the pk Column hidden

    Updates require a way to uniquely identify the row to be updated. Removing the unique id makes updates difficult.

    Hiding the column is a function of the view, not the model. So, for example,
    Qt Code:
    1. model = new QSqlTableModel;
    2. model->setTable("student"); //that's the table name
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->select();
    5. model->setHeaderData(0, Qt::Horizontal, tr("id")); // will not be shown in view below
    6. model->setHeaderData(1, Qt::Horizontal, tr("name"));
    7.  
    8. ui->tableView->setModel(model);
    9. ui->tableView->setColumnHidden(0, true); // hide the id from view
    10. ui->tableView->show();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Replies: 6
    Last Post: 6th August 2009, 17:18
  3. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 11:50
  4. setting data of buffer to QTreeWidget column
    By thomasjoy in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 17:12
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 08:00

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.