Results 1 to 2 of 2

Thread: Save and read data from tableWidget to XML file

  1. #1
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Save and read data from tableWidget to XML file

    Hi, I have a problem. I want to save my items from tableWidget to Xml file, and load them into a table with xml.

    Qt Code:
    1. void MainWindow::AddItemsToTable()
    2. {
    3. CheckAddingEmptyValue();
    4.  
    5. int row = ui->tableWidget->rowCount();
    6. ui->tableWidget->insertRow(row);
    7.  
    8. for(int i = 0; i < ui->tableWidget->columnCount(); ++i)
    9. ui->tableWidget->setItem(row, i, new QTableWidgetItem(""));
    10.  
    11. Q_ASSERT(ui->tableWidget->item(row, 0));
    12. ui->tableWidget->item(row, 0)->setText(ui->lineEdit_3->text());
    13.  
    14. Q_ASSERT(ui->tableWidget->item(row, 1));
    15. ui->tableWidget->item(row, 1)->setText(ui->lineEdit_2->text());
    16.  
    17. Q_ASSERT(ui->tableWidget->item(row, 2));
    18. ui->tableWidget->item(row, 2)->setText(ui->lineEdit->text());
    19. }
    20.  
    21. void MainWindow::SaveDataToXml()
    22. {
    23. QFile file("Data.xml");
    24. if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    25. {
    26. QMessageBox msgBox;
    27. msgBox.setWindowTitle("Error");
    28. msgBox.setIcon(QMessageBox::Warning);
    29. msgBox.setInformativeText(trUtf8("Cannot write file !"));
    30. msgBox.setStandardButtons(QMessageBox::Ok);
    31. }
    32.  
    33. QXmlStreamWriter xmlWriter(&file);
    34. xmlWriter.setAutoFormatting(true);
    35. xmlWriter.writeStartDocument("1.0");
    36.  
    37. //?????????????????????????????????//
    38. // Code that write table data to xml///
    39. //////////////////////////////////////////
    40. /////////////////////////////////////////
    41. ////////////////////////////////////////
    42.  
    43. }
    44. xmlWriter.writeEndDocument();
    45.  
    46. file.close();
    47. if (file.error())
    48. {
    49. QMessageBox msgBox;
    50. msgBox.setWindowTitle("Error");
    51. msgBox.setIcon(QMessageBox::Warning);
    52. msgBox.setInformativeText(trUtf8("Cannot write file !"));
    53. msgBox.setStandardButtons(QMessageBox::Ok);
    54. }
    55. }
    56.  
    57.  
    58. void MainForm::ReadDataFromXml()
    59. {
    60. QFile file("Data.xml");
    61. if (!file.open(QFile::ReadOnly | QFile::Text))
    62. {
    63. QMessageBox msgBox;
    64. msgBox.setWindowTitle("Error");
    65. msgBox.setIcon(QMessageBox::Warning);
    66. msgBox.setInformativeText(trUtf8("Cannot open database file !"));
    67. msgBox.setStandardButtons(QMessageBox::Ok);
    68. }
    69.  
    70. //?????????????????????????????????//
    71. // Code that read xml and add data do Table///
    72. //////////////////////////////////////////
    73. /////////////////////////////////////////
    74. ////////////////////////////////////////
    75.  
    76. file.close();
    77. if (file.error() != QFile::NoError)
    78. {
    79. QMessageBox msgBox;
    80. msgBox.setWindowTitle("Error");
    81. msgBox.setIcon(QMessageBox::Warning);
    82. msgBox.setInformativeText(trUtf8("Cannot read database file !"));
    83. msgBox.setStandardButtons(QMessageBox::Ok);
    84. }
    85. }
    86.  
    87. void MainForm::on_pushButton_clicked()
    88. {
    89. AddItemsToTable();
    90. SaveDataToXml();
    91. }
    To copy to clipboard, switch view to plain text mode 

    I need help
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save and read data from tableWidget to XML file

    One common way to represent a table in XML is to have one XML element per row and within the row element one XML element per cell. The cell's data can be saved as the cell element's text.

    So for each row you would call writeStartElement with the row tag name, then for each column in that row writeStartElement with the cell tag name and using writeCharacters to write each cell's data.

    You then close the tags using writeEndElement() where appropriate.

    Have a look at the bookmark example in the Qt Documentation for XML Streaming

    Cheers,
    _

Similar Threads

  1. Save binary data into XML file
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2013, 11:16
  2. read ini file content and save into array
    By cooper in forum Newbie
    Replies: 8
    Last Post: 14th March 2011, 21:33
  3. Read/write data from file
    By Insomnium in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2010, 07:35
  4. Problem with save data from model to file
    By matulik in forum Newbie
    Replies: 0
    Last Post: 24th May 2010, 15:19
  5. Replies: 3
    Last Post: 23rd June 2006, 17:46

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.