Results 1 to 6 of 6

Thread: why does QSqlTableModel not allow editing?

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default why does QSqlTableModel not allow editing?

    hi, i have a tableview showing a QSqlTableModel. I had thought that this would allow me to just click into any cell and change the contents? now i can click in the cell and change, but as soon as i leave the cell the old value is there again. What am i missing?
    Qt Code:
    1. QSqlTableModel* modDetails;
    2. modDetails->setTable("x"+ka->getShortText());
    3. modDetails->setEditStrategy(QSqlTableModel::OnFieldChange);
    4. modDetails->select();
    5. modDetails->removeColumn(0); // don't show the ID
    6. modDetails->setHeaderData(0, Qt::Horizontal, tr("Abk."));
    7. modDetails->setHeaderData(1, Qt::Horizontal, tr("Bezeichnung"));
    8. // tableDetail is a tableview created in Designer
    9. tableDetail->setModel(modDetails);
    10. tableDetail->show();
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: why does QSqlTableModel not allow editing?

    What happens if you change the model manually using setData()? Maybe you provide incorrect values and the database rejects it?

  3. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why does QSqlTableModel not allow editing?

    i will try this later.
    but it shouldn't be only this: i have tried with one textfield which allready has text and where i only changed a few letters - that should not make it illegal.

    btw: i also don't understand how the user would be able to add a new line? Do i assume right, that i add a pushbutton that triggers this or is there any built-in trick which should work as soon as editing works as well?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: why does QSqlTableModel not allow editing?

    Hello Mikro,

    Does it work when you omit the line :
    modDetails->removeColumn(0); // don't show the ID

    AFAIR removeColumn removes the given column from the model. But it's necessary for the database to keep track of ID 's.

    try :
    QTableView::setColumnHidden
    to hide the column with the ID 's.

    Cheers

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

    mikro (18th July 2006)

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

    Default Re: why does QSqlTableModel not allow editing?

    Quote Originally Posted by mikro
    btw: i also don't understand how the user would be able to add a new line? Do i assume right, that i add a pushbutton that triggers this or is there any built-in trick which should work as soon as editing works as well?
    You have to provide code which operates on the model, you add new items using QSqlTableModel::insertRecord().

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

    mikro (18th July 2006)

  8. #6
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why does QSqlTableModel not allow editing?

    thank you all. i found the problem: the column that i was hiding was an id that was suppossed to autoincrement - unfortunately i had used the sql from MySQL on a SQLite-DB - so what looked like a autoincrement field to me was understood as a normal int by SQLite so it didn't accept the new record without this column.

Similar Threads

  1. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 11:03
  2. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56
  3. QSqlTableModel
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:35
  4. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07:58
  5. Resizing QSqlTableModel
    By teS in forum Newbie
    Replies: 6
    Last Post: 19th January 2006, 21:08

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.