Results 1 to 3 of 3

Thread: QSqlTableModel set Integer data to NULL

  1. #1
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QSqlTableModel set Integer data to NULL

    I have a sql table with a column of type integer allowing null values and I use a QSqlTableModel to manipulate its data.

    I am facing the following problem: I want to insert a Null value using the QSqlTableModel setData function. When I do this, QSqlTableModel translates my Null into 0, its default value (Qt QTBUG-1512), which is not what I want (0 has a specific meaning in my application).

    How can I set a null value?

    Any hint will be greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel set Integer data to NULL

    HI,

    I'm working with Qt 4.7.1 on Windows XP and for me it works fine.

    This Code
    Qt Code:
    1. QString connName;
    2.  
    3. QString fileName = QFileDialog::getSaveFileName (this, tr ("Create DB"));
    4. if (!fileName.isEmpty ()) {
    5. bool result = false;
    6.  
    7. QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE");
    8. db.setDatabaseName (fileName);
    9.  
    10. result = db.open ();
    11. if (!result) {
    12. qWarning () << "Error in db.open():" << db.lastError ().text();
    13. return;
    14. }
    15. connName = db.connectionName ();
    16.  
    17. db.exec ("CREATE TABLE T_PDC ("
    18. "val1 INTEGER NOT NULL, "
    19. "val2 INTEGER)");
    20.  
    21. QSqlRecord record = db.record ("T_PDC");
    22.  
    23.  
    24. QSqlTableModel model (this, db);
    25. model.setTable ("T_PDC");
    26. model.setEditStrategy (QSqlTableModel::OnManualSubmit);
    27. model.select ();
    28.  
    29. int row;
    30. for (int i = 0; i < 5; ++i) {
    31. row = model.rowCount ();
    32. qDebug () << "model.rowCount() = " << row;
    33.  
    34. result = model.insertRows (row, 1);
    35. if (!result) {
    36. qWarning () << "Error in model.insertRow () :" << model.lastError ().text ();
    37. break;
    38. }
    39. else
    40. qDebug () << "model.insertRow(): SUCCESS";
    41.  
    42. idx = model.index (row, 0);
    43. if (!idx.isValid ()) {
    44. qWarning () << "Invalid Index (" << row << ", 0)";
    45. break;
    46. }
    47. model.setData (idx, i);
    48.  
    49. idx = model.index (row, 1);
    50. if (!idx.isValid ()) {
    51. qWarning () << "Invalid Index (" << row << ", 0)";
    52. break;
    53. }
    54.  
    55. if (i % 2)
    56. result = model.setData (idx, 10 * (i + 1));
    57. else
    58. result = model.setData (idx, QVariant());
    59.  
    60. if (!result) {
    61. qWarning() << "Error in model.setData ():" << model.lastError ().text ();
    62. break;
    63. }
    64. }
    65.  
    66. result = model.submitAll ();
    67. if (!result) {
    68. qWarning () << "Error in model.submitAll() :" << model.lastError ().text ();
    69. }
    70. db.close ();
    71. }
    72. QSqlDatabase::removeDatabase (connName);
    To copy to clipboard, switch view to plain text mode 

    produce this result

    sql.jpg

    However consider to use QSqlRecord for obtain more compact code
    Qt Code:
    1. int row;
    2. QSqlRecord record = db.record (model.tableName ());
    3. for (int i = 0; i < 5; ++i) {
    4. row = model.rowCount ();
    5. qDebug () << "model.rowCount() = " << row;
    6.  
    7. record.setValue (0, i);
    8. if (i % 2)
    9. record.setNull (1);
    10. else
    11. record.setValue (1, 10 * (i + 1));
    12.  
    13. model.insertRecord (row, record);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 21st April 2011 at 14:37. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel set Integer data to NULL

    Thanks for answering. Unfortunately I am using Qt 4.5.3 and this is not working. I tried to set the value directly with the QSqlRecord.

    Is it an issue corrected in Qt 4.7 or do I have a chance with Qt 4.6?

Similar Threads

  1. Null Handling with QDataMapper and QSqlTableModel
    By FinancialStudent in forum Newbie
    Replies: 0
    Last Post: 22nd December 2010, 09:23
  2. QSqlTableModel and NULL
    By karas in forum Qt Programming
    Replies: 0
    Last Post: 9th February 2009, 12:55
  3. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 11:50
  4. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:40
  5. Replies: 5
    Last Post: 29th January 2008, 17:36

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.