Results 1 to 4 of 4

Thread: [Snippet] QSqlTableModel to CSV Export

  1. #1

    Default [Snippet] QSqlTableModel to CSV Export

    this code exports data from model (in my case displayed in QTableView) to csv text file, taking into regard the hidden columns;

    Qt Code:
    1. void MainWindow::csvexport()
    2. {
    3.  
    4. QString linki = QFileDialog::getSaveFileName(this, tr("Export CSV"),
    5. QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)+"/base.csv",
    6. tr("Comma Separated Values (*.csv)"));
    7.  
    8.  
    9. int x = 0;
    10. QString exportdata;
    11. while (x < model->columnCount()){
    12.  
    13. if (!ui->tableView->isColumnHidden(x)) {
    14.  
    15. exportdata.append(model->headerData(x,Qt::Horizontal,Qt::DisplayRole).toString());
    16. //msgbox(exportdata);
    17. if (model->columnCount() - x != 1)
    18. exportdata.append(",");
    19. else
    20. exportdata.append("\n");
    21.  
    22. }
    23. x++;
    24.  
    25. }
    26. int z = 0;
    27.  
    28.  
    29. while (z < model->rowCount()) {
    30.  
    31. x = 0;
    32. while (x < model->columnCount()) {
    33. if (!ui->tableView->isColumnHidden(x)) {
    34.  
    35. exportdata.append(model->data(model->index(z,x),Qt::DisplayRole).toString());
    36.  
    37. if (model->columnCount() - x != 1)
    38. exportdata.append(",");
    39. else
    40. exportdata.append("\n");
    41.  
    42. }
    43. x++;
    44.  
    45. }
    46.  
    47. z++;
    48. }
    49.  
    50.  
    51.  
    52. QFile file;
    53. if (!linki.isEmpty()) {
    54. file.setFileName(linki);
    55. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    56. return;
    57. }
    58. QByteArray ttext;
    59. ttext.append(exportdata);
    60. file.write(ttext);
    61. }
    To copy to clipboard, switch view to plain text mode 

  2. #2

    Default Re: [Snippet] QSqlTableModel to CSV Export

    Fixed Bug regarding hidden columns:

    Qt Code:
    1. QString linki = QFileDialog::getSaveFileName(this, tr("Export CSV"),
    2. QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)+"/base.csv",
    3. tr("Comma Separated Values (*.csv)"));
    4.  
    5.  
    6. int x = 0;
    7. QString exportdata;
    8.  
    9. int counthidden = 0, jint = 0;
    10.  
    11. while (jint < model->columnCount()) {
    12.  
    13. counthidden+=ui->tableView->isColumnHidden(jint);
    14. jint++;
    15. }
    16.  
    17.  
    18.  
    19. while (x < model->columnCount()){
    20.  
    21. if (!ui->tableView->isColumnHidden(x)) {
    22.  
    23. exportdata.append(model->headerData(x,Qt::Horizontal,Qt::DisplayRole).toString());
    24. //msgbox(exportdata);
    25. if (model->columnCount() - x != counthidden)
    26. exportdata.append(";");
    27. else
    28. exportdata.append("\n");
    29.  
    30. }
    31. x++;
    32.  
    33. }
    34. int z = 0;
    35.  
    36.  
    37. while (z < model->rowCount()) {
    38.  
    39. x = 0;
    40. while (x < model->columnCount()) {
    41. if (!ui->tableView->isColumnHidden(x)) {
    42.  
    43. exportdata.append(model->data(model->index(z,x),Qt::DisplayRole).toString());
    44.  
    45. if (model->columnCount() - x != counthidden)
    46. exportdata.append(";");
    47. else
    48. exportdata.append("\n");
    49.  
    50. }
    51. x++;
    52.  
    53. }
    54.  
    55. z++;
    56. }
    57.  
    58.  
    59.  
    60. QFile file;
    61. if (!linki.isEmpty()) {
    62. file.setFileName(linki);
    63. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    64. return;
    65. }
    66. QByteArray ttext;
    67. ttext.append(exportdata);
    68. file.write(ttext);
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: [Snippet] QSqlTableModel to CSV Export

    Final and working version:

    Qt Code:
    1. void MainWindow::csvexport()
    2. {
    3.  
    4.  
    5. QString linki = QFileDialog::getSaveFileName(this, tr("Export CSV"),
    6. QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)+"/errors.csv",
    7. tr("Comma Separated Values (*.csv)"));
    8.  
    9. int x = 0;
    10. QString exportdata;
    11.  
    12. int counthidden=0, jint = 0;
    13.  
    14. while (jint < model->columnCount()) {
    15.  
    16. counthidden+=ui->tableView->isColumnHidden(jint);
    17. jint++;
    18. }
    19.  
    20.  
    21. int w = 1;
    22. while (x < model->columnCount()){
    23.  
    24. if (!ui->tableView->isColumnHidden(x)) {
    25.  
    26.  
    27. exportdata.append(model->headerData(x,Qt::Horizontal,Qt::DisplayRole).toString());
    28.  
    29.  
    30. if (model->columnCount() - w > counthidden)
    31. exportdata.append(";");
    32. else {
    33. exportdata.append("\n");
    34.  
    35. }
    36. w++;
    37. }
    38. x++;
    39.  
    40. }
    41.  
    42. int z = 0;
    43.  
    44. w = 1;
    45. while (z < model->rowCount()) {
    46.  
    47. x = 0;
    48.  
    49. w = 1;
    50. while (x < model->columnCount()) {
    51. if (!ui->tableView->isColumnHidden(x)) {
    52.  
    53.  
    54. exportdata.append(model->data(model->index(z,x),Qt::DisplayRole).toString());
    55.  
    56. if (model->columnCount() - w > counthidden)
    57. exportdata.append(";");
    58. else
    59. exportdata.append("\n");
    60.  
    61. w++;
    62. }
    63. x++;
    64.  
    65. }
    66.  
    67. z++;
    68. }
    69.  
    70.  
    71.  
    72.  
    73.  
    74. QFile file;
    75. if (!linki.isEmpty()) {
    76. file.setFileName(linki);
    77. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    78. return;
    79. }
    80. QByteArray ttext;
    81. ttext.append(exportdata);
    82. file.write(ttext);
    83. }
    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: [Snippet] QSqlTableModel to CSV Export

    It won't work. What if some cell contains a semicolon or a newline?
    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.


Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 20:06
  2. how to export dll with gui?
    By cutie.monkey in forum Qt Programming
    Replies: 5
    Last Post: 14th November 2009, 02:19
  3. Export SVG: Qwt Bug?
    By giusepped in forum Qwt
    Replies: 4
    Last Post: 19th December 2008, 10:50
  4. DLL export problem
    By hrcariaga in forum Qt Programming
    Replies: 5
    Last Post: 12th February 2008, 06:26
  5. HPGL PLT code snippet?
    By CuCullin in forum KDE Forum
    Replies: 1
    Last Post: 7th February 2006, 21:17

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.