Results 1 to 5 of 5

Thread: Load from a file to QSqlTableModel

  1. #1
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Load from a file to QSqlTableModel

    Hi ,

    In my example I use a model/view. My class is QSqlTableModel for the model and QTableView for the view.
    I know save data to a file (.txt and even .csv if I wish).

    Qt Code:
    1. void Data_Example::txtExport()
    2. {
    3.  
    4. QString fileName = QFileDialog::getSaveFileName(this,
    5. tr("Save"), "",
    6. tr("Save (*.txt);;All Files (*)"));
    7.  
    8. if (fileName.isEmpty())
    9. return;
    10. else {
    11. QFile file(fileName);
    12. if (!file.open((QIODevice::WriteOnly) | QIODevice::Text))
    13. {
    14. QMessageBox::information(this, tr("Unable to open file"),
    15. file.errorString());
    16. return;
    17. }
    18.  
    19. QString data;
    20. data ="";
    21.  
    22. for (int row = 0; row < model->rowCount(); ++row)
    23. {
    24. QSqlRecord record = model->record(row);
    25. for (int field = 0; field < record.count(); ++field)
    26. {
    27. if(field != 0 && field !=4 )
    28. {
    29. if (field > 1) data += "\n";
    30.  
    31. data += record.field(field).value().toString();
    32.  
    33. }
    34. }
    35. }
    36.  
    37. QTextStream output(&file);
    38. output.setCodec("UTF-8");
    39. output << data;
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    And in my file (.txt) I have for instance:

    Alicia
    Tucker
    21/09/1985

    John
    Smith
    18/02/1964

    Jessica
    Houston
    07/11/1993
    Now, I search to load this file or an other file like this but I don't find solutions...
    I start to write this:

    void Data_Example::txtImport()
    {

    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open My File"), "",
    tr("My File (*.txt);;All Files (*)"));

    if (fileName.isEmpty())
    return;
    else
    {
    QFile file(fileName);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QMessageBox::information(this, tr("Unable to open file"),
    file.errorString());
    return;
    }

    QTextStream input(&file);
    QString line;
    do
    {
    input >> data;
    line = input.readLine();
    } while(!line.isNull());
    //file.close();


    }
    }
    This part doesn't function because I don't have a loop which load data in my QslqTableModel.
    If somebody know...

    Thanks.

  2. #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 

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

    dd44 (27th August 2012)

  4. #3
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Load from a file to QSqlTableModel

    Thank you for your answer

    The code functions and compiles, however I have some problems with the display.
    In my view, the table display for instance:

    Surname Name Birthday
    Alicia Alicia Alicia
    Tucker Tucker Tucker
    21/09/1985 21/09/1985 21/09/1985
    and when I click on it, I have "Alicia" in all my QLineEdit or "Tucker", or "21/09/1985"

    So, I search to do this:

    Surname Name Birthaday
    Alicia Tucker 21/09/1985
    John Smith 18/02/1964
    Jessica Houston 07/11/1993
    But I don't find...

  5. #4
    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

    From the output the error is quite obvious.
    Qt Code:
    1. for (int field=1 ; field<rec.count() ; ++field)
    2. {
    3. if (field==4) continue; // was ignored on save
    4. rec.setValue(field, line);
    5. line = in.readLine(); // one field per line
    6. }
    7. model->insertRecord(-1, rec);
    8. // line = in.readLine() this line moved inside the for loop;
    To copy to clipboard, switch view to plain text mode 

    The code functions and compiles, however ...
    Just because it compiles doesn't mean its logic is right.
    My suggestion is that you should have tried a few alternatives yourself. After all these errors are quite common in alpha testing.

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

    dd44 (28th August 2012)

  7. #5
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Load from a file to QSqlTableModel

    Ok thank you , I'm not very at ease with QSqlTableModel, so your help is important to me.
    My problem is not completely solved because I can't read my file as I want.
    Indeed, It works when the file is like this for example:

    Alicia
    Tucker
    21/09/1985
    John
    Smith
    18/02/1964
    Jessica
    Houston
    07/11/1993
    but if the file is like that:
    Alicia
    Tucker
    21/09/1985

    John
    Smith
    18/02/1964

    Jessica
    Houston
    07/11/1993
    It skips each time a cell in my view (so in my model too).

    I don't find a method() or anything else to do this.


    Added after 8 minutes:


    Ok, I find the solution sorry

    for (int field=1 ; field<rec.count() ; ++field)
    {
    if (field==4) continue; // was ignored on save
    rec.setValue(field, line);
    line = in.readLine(); // one field per line
    }
    model->insertRecord(-1, rec);
    line = in.readLine();
    For the moment it's OK for me...

    Thks.
    Last edited by dd44; 28th August 2012 at 16:02.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.