You didn't quite read my reply, did you? No ReadWrite for TextFiles!!
What you need to do is read everything, change your line and then rewrite everything.
Why don't you load the data at program begin, store it in a list for the user to to edit and append, and then write to file, when the user saves/exits?
struct XYZTriple {
double x;
double y;
double z;
}
// maybe as private member of your class:
class DatumForm : ..
{
private:
QList<XYZTriple> data;
int oldindex;
}
void DatumForm::readfile()
{
{
while ( !stream.atEnd() )
{
line = stream.readLine( );
datumlines = line.split(",");
if (datumlines.length() == 3) {
XYZTriple datum;
datum.x = datumlines[0].toDouble();
datum.y = datumlines[1].toDouble();
datum.z = datumlines[2].toDouble();
data.append(datum);
} else {
// error in line..
}
}
file.close();
}
}
void DatumForm::writefile()
{
{
for (int i=0;i<data.length();++i)
{
stream <<
QString("%1,%2,%3\n").
arg(data
[i
].
x,
0,
'f',
3).
arg(data
[i
].
y,
0,
'f',
3).
arg(data
[i
].
z,
0,
'f',
3);
}
stream.flush();
file.close();
}
}
struct XYZTriple {
double x;
double y;
double z;
}
// maybe as private member of your class:
class DatumForm : ..
{
private:
QList<XYZTriple> data;
int oldindex;
}
void DatumForm::readfile()
{
QFile file("datum.txt");
if (file.open( QIODevice::ReadOnly | QIODevice::QIODevice::Text))
{
QTextStream stream( &file );
while ( !stream.atEnd() )
{
line = stream.readLine( );
datumlines = line.split(",");
if (datumlines.length() == 3) {
XYZTriple datum;
datum.x = datumlines[0].toDouble();
datum.y = datumlines[1].toDouble();
datum.z = datumlines[2].toDouble();
data.append(datum);
} else {
// error in line..
}
}
file.close();
}
}
void DatumForm::writefile()
{
QFile file("datum.txt");
if (file.open( QIODevice::WriteOnly | QIODevice::QIODevice::Text | QIODevice::Truncate))
{
QTextStream stream( &file );
for (int i=0;i<data.length();++i)
{
stream << QString("%1,%2,%3\n").arg(data[i].x,0,'f', 3).arg(data[i].y,0,'f', 3).arg(data[i].z,0,'f', 3);
}
stream.flush();
file.close();
}
}
To copy to clipboard, switch view to plain text mode
For your Widgets, don't use simple LineEdits, but QSpinBox and QDoubleSpinBox.
Now you need to make sure, that your lineNumber is in the right range, and if it is beyond => append data.
DatumForm::DatumForm()
{
..
oldindex = 0;
lineNumberSpinBox->setMinimum(1);
}
void DatumForm::lineNumberSpinBoxValueChanged(int i)
{
// save values in array if previous value is valid
if (oldindex > 0)
{
data[oldindex-1].x = xValueDoubleSpinBox->value();
data[oldindex-1].y = yValueDoubleSpinBox->value();
data[oldindex-1].z = zValueDoubleSpinBox->value();
}
// make sure list is big enough:
for (var j=data.length();j<i;++j)
{
// add default values..
XYZTriple datum = {0.0,0.0,0.0};
data.append(datum);
}
// show the values
xValueDoubleSpinBox->setValue(data[i-1].x);
yValueDoubleSpinBox->setValue(data[i-1].y);
zValueDoubleSpinBox->setValue(data[i-1].z);
// save oldindex
oldindex = i;
}
DatumForm::DatumForm()
{
..
oldindex = 0;
lineNumberSpinBox->setMinimum(1);
}
void DatumForm::lineNumberSpinBoxValueChanged(int i)
{
// save values in array if previous value is valid
if (oldindex > 0)
{
data[oldindex-1].x = xValueDoubleSpinBox->value();
data[oldindex-1].y = yValueDoubleSpinBox->value();
data[oldindex-1].z = zValueDoubleSpinBox->value();
}
// make sure list is big enough:
for (var j=data.length();j<i;++j)
{
// add default values..
XYZTriple datum = {0.0,0.0,0.0};
data.append(datum);
}
// show the values
xValueDoubleSpinBox->setValue(data[i-1].x);
yValueDoubleSpinBox->setValue(data[i-1].y);
zValueDoubleSpinBox->setValue(data[i-1].z);
// save oldindex
oldindex = i;
}
To copy to clipboard, switch view to plain text mode
You could also restrict the lineNumber to the valid ranage with QSpinBox::setRange(1,data.length()) and have an additional append-button.
Now: Good luck with your project!
BTW: Code is as is :-> There are probably several typos in there...
Johannes
Bookmarks