QTableWidget data to xml file
-
for(iRow = 0; iRow < 10; iRow ++)
{
QDomElement deName = xmlfeature.createElement("Name");
deStudent.appendChild(deName);
int iData = ui->tableWidget_data->item(iRow,0)->text().toInt();
QDomText dtData = xmlFeature.createTextNode(QString::number(iData));
deName.appendChild(dtData);
}I am using this code to access the rows in a TableWidget
However the program crashes and the values from the table are not written into the XML file.
Where am I going wrong?
Also I want to see if the value in the row iRow,0 in a table is empty or not. It should not have garbage value either
How do I check this?
The total number of tags that I want to create in my xml file should be equal to the number of non empty rows in the table.How do I do that?
Please help -
Hi,
There are several issues in your code:
- Use of magic numbers. Where is that 10 coming from ? You do not take into account the real numbers of row from your QTableWidget so you might be accessing invalid items through invalid indexes.
- Why are you converting a textual value to and int to immediately converting back to a string ?
As for your checks, loop over the actual count of rows, if it's zero then nothing will happen. As for non empty rows, how do you define a row to be empty ? Once you know that, us it as a condition to not process the row in question.
-
10 is the number of rows that I have defined my table to have. The total number of rows cannot cross 10. So I am iterating the entire table and then seeing if there is any value in that row in the table.
I am converting the value in the cell to an integer value and storing it in iData
Then to store that value into the XML file I convert it to a string so that it can be stored a value in a tag.A non empty row would be on that has no value in it at all.
So it should not have any value in it. -
10 is the number of rows that I have defined my table to have. The total number of rows cannot cross 10. So I am iterating the entire table and then seeing if there is any value in that row in the table.
I am converting the value in the cell to an integer value and storing it in iData
Then to store that value into the XML file I convert it to a string so that it can be stored a value in a tag.A non empty row would be on that has no value in it at all.
So it should not have any value in it.@QtisHard said in QTableWidget data to xml file:
10 is the number of rows that I have defined my table to have. The total number of rows cannot cross 10. So I am iterating the entire table and then seeing if there is any value in that row in the table.
It does not change the fact that you are using a magic number for that. The loop should always run on the actual row count of the model. What when you decide that it's not 10 but 12 rows ? Defining the size in the rowCount method allows to have the information in one central place.
I am converting the value in the cell to an integer value and storing it in iData
Then to store that value into the XML file I convert it to a string so that it can be stored a value in a tag.Then again: why the double conversion ? Just get the value as text directly.
A non empty row would be on that has no value in it at all.
So it should not have any value in it.The way you describe it makes it look that the row might now have any items in it and you are not checking for that.