Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget data to xml file
Forum Updated to NodeBB v4.3 + New Features

QTableWidget data to xml file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 475 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtisHard
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There are several issues in your code:

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtisHard
        wrote on last edited by
        #3

        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.

        SGaistS 1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          where is the crash location? Is ui->tableWidget_data->item(iRow,0) nullptr or not?

          1 Reply Last reply
          0
          • Q QtisHard

            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.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved