Hi, I have a problem. I want to save my items from tableWidget to Xml file, and load them into a table with xml.
void MainWindow::AddItemsToTable()
{
CheckAddingEmptyValue();
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
for(int i = 0; i < ui->tableWidget->columnCount(); ++i)
Q_ASSERT(ui->tableWidget->item(row, 0));
ui->tableWidget->item(row, 0)->setText(ui->lineEdit_3->text());
Q_ASSERT(ui->tableWidget->item(row, 1));
ui->tableWidget->item(row, 1)->setText(ui->lineEdit_2->text());
Q_ASSERT(ui->tableWidget->item(row, 2));
ui->tableWidget->item(row, 2)->setText(ui->lineEdit->text());
}
void MainWindow::SaveDataToXml()
{
{
msgBox.setWindowTitle("Error");
msgBox.setInformativeText(trUtf8("Cannot write file !"));
}
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument("1.0");
//?????????????????????????????????//
// Code that write table data to xml///
//////////////////////////////////////////
/////////////////////////////////////////
////////////////////////////////////////
}
xmlWriter.writeEndDocument();
file.close();
if (file.error())
{
msgBox.setWindowTitle("Error");
msgBox.setInformativeText(trUtf8("Cannot write file !"));
}
}
void MainForm::ReadDataFromXml()
{
{
msgBox.setWindowTitle("Error");
msgBox.setInformativeText(trUtf8("Cannot open database file !"));
}
//?????????????????????????????????//
// Code that read xml and add data do Table///
//////////////////////////////////////////
/////////////////////////////////////////
////////////////////////////////////////
file.close();
if (file.
error() != QFile::NoError) {
msgBox.setWindowTitle("Error");
msgBox.setInformativeText(trUtf8("Cannot read database file !"));
}
}
void MainForm::on_pushButton_clicked()
{
AddItemsToTable();
SaveDataToXml();
}
void MainWindow::AddItemsToTable()
{
CheckAddingEmptyValue();
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
for(int i = 0; i < ui->tableWidget->columnCount(); ++i)
ui->tableWidget->setItem(row, i, new QTableWidgetItem(""));
Q_ASSERT(ui->tableWidget->item(row, 0));
ui->tableWidget->item(row, 0)->setText(ui->lineEdit_3->text());
Q_ASSERT(ui->tableWidget->item(row, 1));
ui->tableWidget->item(row, 1)->setText(ui->lineEdit_2->text());
Q_ASSERT(ui->tableWidget->item(row, 2));
ui->tableWidget->item(row, 2)->setText(ui->lineEdit->text());
}
void MainWindow::SaveDataToXml()
{
QFile file("Data.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setInformativeText(trUtf8("Cannot write file !"));
msgBox.setStandardButtons(QMessageBox::Ok);
}
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument("1.0");
//?????????????????????????????????//
// Code that write table data to xml///
//////////////////////////////////////////
/////////////////////////////////////////
////////////////////////////////////////
}
xmlWriter.writeEndDocument();
file.close();
if (file.error())
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setInformativeText(trUtf8("Cannot write file !"));
msgBox.setStandardButtons(QMessageBox::Ok);
}
}
void MainForm::ReadDataFromXml()
{
QFile file("Data.xml");
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setInformativeText(trUtf8("Cannot open database file !"));
msgBox.setStandardButtons(QMessageBox::Ok);
}
//?????????????????????????????????//
// Code that read xml and add data do Table///
//////////////////////////////////////////
/////////////////////////////////////////
////////////////////////////////////////
file.close();
if (file.error() != QFile::NoError)
{
QMessageBox msgBox;
msgBox.setWindowTitle("Error");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setInformativeText(trUtf8("Cannot read database file !"));
msgBox.setStandardButtons(QMessageBox::Ok);
}
}
void MainForm::on_pushButton_clicked()
{
AddItemsToTable();
SaveDataToXml();
}
To copy to clipboard, switch view to plain text mode
I need help
Regards
Bookmarks