Question

I am a student programmer using QT to develop and application for work. Currently I am developing the save functions in which data is taken from a table and saved to a file. Im running into some trouble when I try to write the data into columns. Not to confuse anyone; my problem is formatting text WHEN SAVING from the data table; not reading into it. So currently when data is saved from my Table it is saved into a file and looks like this:

0 0.048 300 101325 0 0
10 0.048 300 101325 0 0

I need it to look like this

0  0.048 300 101325 0 0
10 0.048 300 101325 0 0

During research I came across setw from the iomanip library in c++ which sets a minimum amount of characters to fill data into and then uses the rest as padding. Sounds perfect right? Problem is I cant get it to work with anything besides cout; which isn't what I need it for. I need it to basically append spaces to my string prior to being written out.

I also feel like Qt should probably have something to help me accomplish what I need here(alot easier). I just cant seem to find this member function after refencing QString documentation, QStringList Doucmentation, and most of all QTextStream Documentation.

Currently my save function looks like this:

QTextStream data(&saveFileAsscf);
QStringList tmpList;
for (int x = 0; x<ui->tableWidgetReaderTable->rowCount(); x++)
                {
                    strList.clear();
                    for(int a = 0; a < ui->tableWidgetReaderTable->columnCount(); a++)
                    {
                        strList << ui->tableWidgetReaderTable->item(x,a)->text();
                    }
                    data <<strList.join(" ") << "\n";
                }

I am so sure that setFieldWidth is my answer from the QTextStream Documentation. but I cant get it to work right. Tried:

data.setFieldWidth(13) << strList.join(" ") << "\n";

I'm hoping that this is an easy answer for someone and I'm just running into some writers block but any help would be apperciated in accomplishing this. Thanks for reading my post and I appreciate any help contributed to bridge the gap here!

Was it helpful?

Solution

std::fstream is one option if you want to use STL. However if you're treating the entire string list as a field you're width is far more than 13 by the example output given you could try setFieldWidth(23) but it will probably pad the end. If you instead treat each value as a field then it becomes easier.

Example of what I mean by treating each value as a field:

QTextStream data(&saveFileAsscf);
for (int x = 0; x<ui->tableWidgetReaderTable->rowCount(); x++)
{
   data.setPadChar(' ');
   for(int a = 0; a < ui->tableWidgetReaderTable->columnCount(); a++)
   {
      QString value = ui->tableWidgetReaderTable->item(x,a)->text();
      data.setFieldWidth(qMax(2, value.length()));
      data << value;
      data.setFieldWidth(1);
      data << " ";
   }
   data << "\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top