PDA

View Full Version : QFile Question



YDYD
10th July 2014, 05:42
Hi all,

Now i have a method as following


void Dialog::saveValue()
{
QString path="/home/pi/valueHSV/hsv.xml";
QFile valueHSV(path);
valueHSV.open(QIODevice::WriteOnly);
...
}

I would like to change the "path" in different situation,

I tried writing a switch case in different method and call to saveValue mathod, this won't work,
I tried to do a switch case inside saveValue method, this won't work too.

Please advise, Thanks

stampede
10th July 2014, 09:10
I'm not sure what you want to achieve. You can always create a "saveFile" function taking a QString as parameter and call it with different arguments:


void Dialog::saveFile(const QString& path)
{
QFile valueHSV(path);
valueHSV.open(QIODevice::WriteOnly);
...
}

//
void Dialog::saveValue()
{
QString path="/home/pi/valueHSV/hsv.xml";
this->saveFile(path);
QString differentPath="/home/pi/valueHSV/another.xml";
this->saveFile(differentPath);
...
}


If you want to manually select the save file path, you can use QFileDialog:


void Dialog::saveValue()
{
QString path=QFileDialog::getSaveFileName(this, tr("Save File"),
"/home/my_path/",
tr("Xml Files(*.xml)");
if (!path.isEmpty()){
QFile valueHSV(path);
valueHSV.open(QIODevice::WriteOnly);
...
}
}

YDYD
10th July 2014, 09:20
WOW!! Stampede!

You really help me a lot!!

Thanks again!!! =);)