Results 1 to 18 of 18

Thread: QSqlRelationalTableModel and OnManualSubmit oddness

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default QSqlRelationalTableModel and OnManualSubmit oddness

    Hi,

    I have following simple QSqlRelationalTableModel example which works for OnRowChange but not for OnManualSubmit. Any clue why?

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName(":memory:");
    10. if (!db.open())
    11. {
    12. qDebug() << db.lastError();
    13. return 0;
    14. }
    15.  
    16. QSqlQuery query;
    17. query.exec("create table employee(id int primary key, namex varchar(20), city int)");
    18. query.exec("insert into employee values(1, 'Espen', 5000)");
    19. query.exec("insert into employee values(2, 'Harald', 80000)");
    20. query.exec("insert into employee values(3, 'Sam', 100)");
    21.  
    22. query.exec("create table city(id int, name varchar(20))");
    23. query.exec("insert into city values(100, 'San Jose')");
    24. query.exec("insert into city values(5000, 'Oslo')");
    25. query.exec("insert into city values(80000, 'Munich')");
    26.  
    27. model.setTable("employee");
    28. model.setRelation(2, QSqlRelation("city", "id", "name"));
    29. // model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    30. model.setEditStrategy(QSqlTableModel::OnRowChange);
    31. model.select();
    32.  
    33. // Now add a new record
    34.  
    35. f.setName("id");
    36. f.setType(QVariant::Int);
    37. f.setValue(4);
    38. r.append(f);
    39.  
    40. f.setName("namex");
    41. f.setType(QVariant::String);
    42. f.setValue("Lykurg");
    43. r.append(f);
    44.  
    45. f.setName("city");
    46. f.setType(QVariant::Int);
    47. f.setValue(80000);
    48. r.append(f);
    49.  
    50. qDebug() << "insertRecord" << model.insertRecord(-1, r);
    51. qDebug() << " -->" << model.lastError();
    52. qDebug() << "submitAll" << model.submitAll();
    53. qDebug() << " -->" << model.lastError();
    54.  
    55. query.exec("SELECT id, namex, city FROM employee WHERE id = 4");
    56. query.next();
    57. qDebug() << "id:" << query.value(0).toString();
    58. qDebug() << "name:" << query.value(1).toString();
    59. qDebug() << "city:" << query.value(2).toString();
    60.  
    61. v.setModel(&model);
    62. v.show();
    63.  
    64. return a.exec();
    65. }
    To copy to clipboard, switch view to plain text mode 

    Debug for OnRowChange:
    insertRecord true
    --> QSqlError(-1, "", "")
    submitAll false
    --> QSqlError(-1, "No Fields to update", "")
    id: "4"
    name: "Lykurg"
    city: "80000"
    And for OnManualSubmit:
    insertRecord false
    --> QSqlError(-1, "", "")
    submitAll true
    --> QSqlError(-1, "", "")
    id: "4"
    name: "Lykurg"
    city: ""
    As you see city is empty and so it failed...



    Thanks,

    Lykurg

  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: QSqlRelationalTableModel and OnManualSubmit oddness

    What if you set a primary key for the second table?
    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.


  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    Still the same. I changed the creation to
    Qt Code:
    1. query.exec("create table city(id int primary key, name varchar(20))");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    How does the contents of the model look like before you submit them to the database?
    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:

    Lykurg (13th July 2010)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    Crazy! With OnManualSubmit my model record looks like this:
    QSqlRecord( 3 )
    " 0:" QSqlField("id", int, generated: yes, typeID: 1) "4"
    " 1:" QSqlField("namex", QString, generated: yes, typeID: 3) "Lykurg"
    " 2:" QSqlField("name", QString, generated: yes, typeID: 3) ""
    Which cant work, but my inserted record is:
    QSqlRecord( 3 )
    " 0:" QSqlField("id", int, generated: yes) "4"
    " 1:" QSqlField("namex", QString, generated: yes) "Lykurg"
    " 2:" QSqlField("city", int, generated: yes) "80000"
    AND HERE WE GO! I have to take the column name of the FK-Table! With a record like that:
    QSqlRecord( 3 )
    " 0:" QSqlField("id", int, generated: yes) "4"
    " 1:" QSqlField("namex", QString, generated: yes) "Lykurg"
    " 2:" QSqlField("name", int, generated: yes) "80000"
    it works. But it seems to me, that that can't be normal? Or why the "wrong" syntax works with OnRowChange?


    Thanks for pushing me in the right direction!

    Lykurg

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

    saa7_go (5th October 2010)

  8. #6
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    Actually what I was surprised was that it worked with OnRowChange. I knew (by own experience) about having to use the related column name but since it worked for OnRowChange I thought it didn't apply here.
    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.


  9. #7
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    I have a similar problem which arises only in Windows XP.
    I use a proxy attached to a model for filtering input search.

    Qt Code:
    1. model->setEditStrategy(QSqlTableModel::OnRowChange);
    2.  
    3. ...
    4. currentIndex = proxyModel->mapToSource(tViewDatiPannello->currentIndex());
    5. if (currentIndex.isValid()) {
    6.  
    7.  
    8. QSqlRecord r = model->record(currentIndex.row());
    9. qDebug()<<"DATA"<<currentIndex<<r.field("id")<<r;
    To copy to clipboard, switch view to plain text mode 
    In Windows 7, no problem. The debug output is:
    Qt Code:
    1. DATA QModelIndex(4,3,0x0,QSqlTableModel(0x5423da0) ) QSqlField("id", int, generated: yes, typeID: 1) QSqlRecord( 20 )
    2. " 0:" QSqlField("id", int, generated: yes, typeID: 1) "5"
    To copy to clipboard, switch view to plain text mode 
    but in Windows XP, the first row is empty! I mean it seems that it cannot find the column "id"

  10. #8
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    Are you sure you are using exactly the same code and exactly the same data in both situations?
    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.


  11. #9
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    I just copied the exe from one Os to the other (Windows XP is under a VirtualBox machine).
    On both, I installed the gdb and printout the debug messages.

    G

  12. #10
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    Does the data come from the same or similar file? Was the database opened properly?
    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.


  13. #11
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    Yes, also the database is the same. I just copied it from on Os to the other.
    The database is opened as usual
    Qt Code:
    1. model = new QSqlTableModel(this);
    2. listModel = new QStringListModel(this);
    3. proxyListModel = new QSortFilterProxyModel(this);
    4. proxyModel = new MyFilterProxyModel(this);
    To copy to clipboard, switch view to plain text mode 
    And there is a function to set the db name
    Qt Code:
    1. void MyData::setDatabase(const QSqlDatabase &db,const QString &t)
    2. {
    3. database = db;
    4. table = t;
    5. if (model!=0){
    6. delete model;
    7. model = new QSqlTableModel(0,database);
    8. }
    9. if (listModel!=0) {
    10. delete listModel;
    11. listModel = new QStringListModel(this);
    12.  
    13. }
    14.  
    15. initializeModel();
    16. refreshView();
    17. }
    To copy to clipboard, switch view to plain text mode 
    The problem is that with Qt 4.5 I never such thing. I will try to install Qt 4.5 again and try to repeat this apparent "bug".

  14. #12
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    Quote Originally Posted by giusepped View Post
    Yes, also the database is the same. I just copied it from on Os to the other.
    So it's not the same, it is similar.

    The database is opened as usual
    Do you make an actual check that the database does get opened?
    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.


  15. #13
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    Yes, because the debug output is the same except for the first row.
    I mean, I have 10 cols, and the first one is the "id" field or the key of the db, and I qDebug the QSqlRecord which comes from the proxy. In W7 I can see all the data correctly, in XP I can see everything except the first col, the "id". Just the first one.
    Regards

  16. #14
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    And what happens if you get rid of the MyFilterProxyModel instance?
    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.


  17. #15
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    Nothing.
    I just put
    Qt Code:
    1. QSqlRecord r = model->record(0);
    2. qDebug()<<"DATA"<<currentIndex<<r.field("id")<<r;
    To copy to clipboard, switch view to plain text mode 
    the result is the same (in XP). The first column is empty.

  18. #16
    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: QSqlRelationalTableModel and OnManualSubmit oddness

    What's the result of:
    Qt Code:
    1. qDebug() << currentIndex << r;
    To copy to clipboard, switch view to plain text mode 
    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.


  19. #17
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    I attach a screenshot. You can see that only the first field is empty.
    But if a try to query the database by using sqlite3 on the commandline, I can see that it is not!
    Regards
    Attached Images Attached Images

  20. #18
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlRelationalTableModel and OnManualSubmit oddness

    I think this is the reason why onManual submit won't work.
    It seems single field and row/record operations are not intended to work with insertRecord() function.

    Qt Code:
    1. /*!
    2.   Inserts the \a record after \a row. If \a row is negative, the
    3.   record will be appended to the end. Calls insertRows() and
    4.   setRecord() internally.
    5.  
    6.   Returns true if the row could be inserted, otherwise false.
    7.  
    8.   \sa insertRows(), removeRows()
    9. */
    10. bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record)
    11. {
    12. if (row < 0)
    13. row = rowCount();
    14. if (!insertRow(row, QModelIndex()))
    15. return false;
    16. if (!setRecord(row, record))
    17. return false;
    18. if (d->strategy == OnFieldChange || d->strategy == OnRowChange)
    19. return submit();
    20. return true;
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 25th January 2010, 20:56
  2. QGraphicsItem keyPressEvent oddness
    By jonks in forum Qt Programming
    Replies: 5
    Last Post: 3rd June 2009, 15:08
  3. QSqlRelationalTableModel
    By marvin in forum Newbie
    Replies: 2
    Last Post: 19th November 2008, 21:29
  4. Replies: 4
    Last Post: 9th May 2008, 17:02
  5. QSqlRelationalTableModel
    By jjay in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2008, 15:20

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.