Results 1 to 6 of 6

Thread: QSqlRecord becoming invalid

  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 QSqlRecord becoming invalid

    hi,
    i have subclassed QAbstractTableModel to make a model that works easily with tables that have a primary key. in the beginning i fill a QMap with the contents of the table like this:
    Qt Code:
    1. void IndexedTableModel::setTable(QString mtable) {
    2. this->table = mtable;
    3. tableData.clear();
    4. // try to find an index:
    5. QSqlIndex idx = db.driver()->primaryIndex (table);
    6. Q_ASSERT(idx.count() == 1);
    7. indexColumn = idx.fieldName(0);
    8.  
    9. QString sql = "SELECT * FROM " + table;
    10. QSqlQuery query(sql);
    11. int row=0;
    12. while (query.next()) {
    13. QSqlRecord rec = query.record();
    14. int idxval = rec.field(indexColumn).value().toInt();
    15.  
    16. // as the rownumber can be unlike the value of the primary key
    17. // i store the information in a map:
    18. RowContent[row] = idxval;
    19.  
    20. tableData[row] = rec;
    21. row++;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    as long as every cell in the table has a content everything works fine, i read the stuff into the model and can edit it. The problem happens when i come across a row that does not have any data apart from that in the primary key column. i had some debugging input at the end of the above function that assured me that tableData[row].count() is 3 as expected.
    but when i try to edit one of the empty cells my function to find the QSqlField for this cell called from setData() bails out:

    Qt Code:
    1. QSqlField IndexedTableModel::FieldToIndex(const QModelIndex &index) {
    2. int row = index.row();
    3. int col = index.column();
    4. int idx = RowContent[row];
    5. qDebug() << "searching for row " << row << " column " << col << " index " << idx;
    6.  
    7. if (!index.isValid()) {
    8. qDebug() << "..index was invalid ";
    9. return QSqlField();
    10. }
    11. QSqlRecord rec = tableData[idx];
    12. if (rec.count() <= col) {
    13. qDebug() << "..the record has only " << rec.count() << " columns " << " but you want column " << col;
    14. return QSqlField();
    15. } else {
    16. return rec.field(col);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    will tell me that the record has 0 columns. Anybody knows why?

  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: QSqlRecord becoming invalid

    Maybe a bit offtopic, but aren't you trying to double the functionality of QSqlTableModel?

  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: QSqlRecord becoming invalid

    maybe
    but it seemed to me that QSqlTableModel would not use the indexes as hoped for.
    i just had some problemes with the models that were there and thought it would be easy to create my own model. well it has once again taken me days for what are not more than 300 lines.. mainly because i came across problems like this on different occasions. also when i was trying to build some sql-statements using information from the Fields i would fail because of invalid fields coming from perfectly legal queries that simply returned some empty fields. For some time i had the theorie that QSqlRecord would have invalid fields wherever it encountered empty columns.
    But then i realized that at the end of setTable() the tableData[row].count() was still > 0 only when i tried to access the record which i stored in tableData again later would its count() be 0.
    how can this be?

  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: QSqlRecord becoming invalid

    Quote Originally Posted by mikro View Post
    maybe
    but it seemed to me that QSqlTableModel would not use the indexes as hoped for.
    Correct me if I'm wrong, but it is the database which should use indexes, not the model.

    i just had some problemes with the models that were there and thought it would be easy to create my own model. well it has once again taken me days for what are not more than 300 lines.. mainly because i came across problems like this on different occasions.
    Then maybe if QSqlTableModel doesn't fit you, you could subclass the table model and extend it with the functionality you want instead of subclassing an abstract model directly.

    also when i was trying to build some sql-statements using information from the Fields i would fail because of invalid fields coming from perfectly legal queries that simply returned some empty fields. For some time i had the theorie that QSqlRecord would have invalid fields wherever it encountered empty columns.
    But then i realized that at the end of setTable() the tableData[row].count() was still > 0 only when i tried to access the record which i stored in tableData again later would its count() be 0.
    how can this be?
    Hard to say. Could you provide a minimal compilable example which reproduces the problem (for example one that only uses QSqlQuery)?

  5. #5
    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: QSqlRecord becoming invalid

    Quote Originally Posted by wysota View Post
    Correct me if I'm wrong, but it is the database which should use indexes, not the model.
    yes, but the model should be aware of the existence of the indexes. The QSqlTableModel would always fail when trying to insert new rows because it didn't know that they need to have the autoincremented primary key. I would get new rows in my tableview but on trying to edit them it would fail because those empty rows could not be made to connect with the database which refused anything without a decent key.

    Quote Originally Posted by wysota View Post
    Then maybe if QSqlTableModel doesn't fit you, you could subclass the table model and extend it with the functionality you want instead of subclassing an abstract model directly.
    i don't know if that would have made my live easier - i assumed that i would have had to rewrite a lot of basic stuff (beginning with the setTable() -Method which obviously needed to keep track of which column held the primary key and in which row of the model i could find which index.) Therefore i deemed it easier to begin with a tabula rasa - having just the basic functionality in the class i am subclassing lessening the chance of incompatibilities between how the tablemodel wanted to access the table and how i thought it should.
    Plus of course there is a nice example in the f...ine manual, so i could get started by just using this... always a big plus when you are still a beginner like me

    Quote Originally Posted by wysota View Post
    Hard to say. Could you provide a minimal compilable example which reproduces the problem (for example one that only uses QSqlQuery)?
    well i did that and once i had a smaller project i started to realise some things... well as always it was my fault
    now everyhing works. but as i made it anyway i'll attach the project that i created. Maybe someone can use this as a reference when he tries to create his own tablelike-database model. Just compile and run and point the fieldialog to the test.db in the datadirectory.
    Attached Files Attached Files

  6. #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: QSqlRecord becoming invalid

    bugfix: wouldn't know the columns unless there allready were some data in the table
    Attached Files Attached Files

Similar Threads

  1. QSqlRecord problem
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 23:32
  2. invalid sizeHint()
    By roleroz in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 23:11
  3. Qtopia cross-compile for arm
    By Bitz in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 12th June 2006, 13:11
  4. Invalid pixmap
    By munna in forum Newbie
    Replies: 2
    Last Post: 8th June 2006, 09:00
  5. invalid use of undefined type
    By mikro in forum Newbie
    Replies: 2
    Last Post: 9th April 2006, 13:10

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.