PDA

View Full Version : Read data from txt file



wiatrak11
10th March 2013, 12:11
Hi Qt Users,


I have a small question about reading data from txt file and put it into spinbox, editline

I have txt file like this :

(Transform "EulerTransform")
(NumberOfParameters 6)
(TransformParameters 0.010616 0.024962 -0.006457 -9.413307 10.784872 12.639569)
(InitialTransformParametersFileName "NoInitialTransform")
(HowToCombineTransforms "Compose")

// Image specific
(FixedImageDimension 3)
(MovingImageDimension 3)
(FixedInternalImagePixelType "float")
(MovingInternalImagePixelType "float")
(Size 84 99 98)

(Index 0 0 0)
(Spacing 2.0000000000 2.0000000000 2.0000000000)
(Origin -80.0000000000 -33.0000000000 -112.0000000000)
(Direction 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000)
(UseDirectionCosines "false")

// EulerTransform specific
(CenterOfRotationPoint 3.0000000000 65.0000000000 -15.0000000000)
(ComputeZYX "false")


Here : I have already tried to write a method, but I have a problem with parsing data form line () :


//------------------------------------------------------------------------------
void vTool::LoadFile()
{
//Open File to read the transformation parameters
QString fileName = QFileDialog::getOpenFileName(
this,
"Choose elastix transfrom parameters file",
vtksys::SystemTools::GetFilenamePath(mCurrentSlice rManager->GetFileName()).c_str(),
"TransformParameters.0.txt");

if(fileName != "") {
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;
}

// Center of point
QString Xcen, Ycen, Zcen;


QString Xt_txt, Yt_txt, Zt_txt, Xr_txt, Yr_txt, Zr_txt;

double Xtrans = 0, Ytrans = 0, Ztrans = 0;
double Xrot = 0, Yrot = 0, Zrot = 0;

QTextStream in(&file);

while(!in.atEnd())
{
QString mText = in.readLine();
if(mText.startsWith("(TransformParameters"))
{
// in <<'/0'<< Xr_txt <<'/0' << Yr_txt<< '/0' << Zr_txt << '/0'<< Xt_txt << '/0' << Yt_txt <<'/0'<< Zt_txt;
mText.split(" ");

Xrot = Xr_txt.toDouble();
Yrot = Yr_txt.toDouble();
Zrot = Zr_txt.toDouble();

xrot_sb->setValue(Xrot);
yrot_sb->setValue(Yrot);
zrot_sb->setValue(Zrot);

Xtrans = Xt_txt.toDouble();
Ytrans = Yt_txt.toDouble();
Ztrans = Zt_txt.toDouble();

xtrans_sb->setValue(Xtrans);
ytrans_sb->setValue(Ytrans);
ztrans_sb->setValue(Ztrans);

}

if(mText.startsWith("(CenterOfRotationPoint"))
{

in << Xcen << Ycen << Zcen;
Xval->setText(Xcen);
Yval->setText(Ycen);
Zval->setText(Zcen);
}



}

}

}
Could You help me please ?

I would appreciate for any help please :)

wysota
10th March 2013, 12:31
What exactly is the problem?

wiatrak11
10th March 2013, 12:38
I want to read transformParameters values from txt file :


(TransformParameters 0.010616 0.024962 -0.006457 -9.413307 10.784872 12.639569)

(CenterOfRotationPoint 3.0000000000 65.0000000000 -15.0000000000)


and put into variables : Xr_txt,Yr_txt, Zr_txt ...

wysota
10th March 2013, 14:05
Yes, I understand that. But what doesn't work? I don't see any code of yours that tries to read any data from the line.

By the way, don't you think the file format is simple enough to write a proper parser for it? You can even use tools such as qlalr to help you.

wiatrak11
10th March 2013, 16:50
Yes, I understand that. But what doesn't work? I don't see any code of yours that tries to read any data from the line.

By the way, don't you think the file format is simple enough to write a proper parser for it? You can even use tools such as qlalr to help you.



Ok, thank I have already resolved this problem :)

Thanks very much for help.