Results 1 to 5 of 5

Thread: Load from a file to QSqlTableModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Load from a file to QSqlTableModel

    The following code is untested but it will give you the basics
    Qt Code:
    1. QTextStream in(&file);
    2. in.setCodec("UTF-8");
    3. QString line=in.readLine();
    4. while (!line.isNull())
    5. {
    6.  
    7. QSqlRecord rec=model->record();
    8. // just guessing that you have an autoincrement field on column 0
    9. rec.setValue(0, QVariant());
    10. for (int field=1 ; field<rec.count() ; ++field)
    11. {
    12. if (field==4) continue; // was ignored on save
    13. rec.setValue(field, line); // line is a Qstring. maybe you should change it to something else
    14. }
    15. model->insertRecord(-1, rec);
    16. line = in.readLine();
    17. }
    18. // what is the model's edit strategy?
    19. // if it is QSqlTableModel::OnManualSubmit
    20. // call model->submitAll() and check for errors
    21. if (!model->submitAll())
    22. qDebug()<<"error: "<<model->lastError().text();
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to Rhayader for this useful post:

    dd44 (27th August 2012)

Similar Threads

  1. How to load My own .cur file and set cursor
    By AKB48 in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2016, 00:49
  2. Can Qwebview load xml/xsl file?
    By richardander in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2015, 22:36
  3. How to load a .h or .cpp file in designer?
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2009, 13:33
  4. Load ActiveX from file
    By Passer_dj in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 23:24
  5. Replies: 2
    Last Post: 13th September 2006, 09:11

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
  •  
Qt is a trademark of The Qt Company.