[Solved] Retrieving data of various types from file using QTextStream
-
Hello,
I am trying to retrieve data from a file that consists of ints and Strings. The snippet of the code is shown below:
@QFile file(QDir::home().path()+"/.cuviewer/cuviewrc-"+VERSION);
if(QFile::exists(QDir::home().path()+"/.cuviewer/cuviewrc-"+VERSION)){
if(file.open(QIODevice::ReadOnly)){
QTextStream ts(&file);
QString pad;
QVariant value;
ts.readLine();
ts >> pad >> value.toInt();
pd->restoreWS = value.toBool();
ts >> pad >> value.toInt(); // showSplashScreen
pd->showSplashScreen = value.toBool();
ts >> pad >> value.asString();
pd->docPath = ts.readLine().stripWhiteSpace();
ts >> pad >> value.toInt();
pd->updateRelease = value.toBool();
ts >> pad >> value.toInt();
pd->boxModel = value.toBool();
ts >> pad >> value.toInt();
pd->drawBoxAlways = value.toBool();
ts >> pad >> value.toInt();
pd->drawAxis = value.toBool();
ts >> pad >> value.toInt();
pd->drawAxisOrigin = value.toBool();
ts >> pad >> value.toInt();
pd->mouseButtons = value.toInt();
ts >> pad >> value.asString();
QString format = value.toString();@
When I run this code I get errors saying
@ error:ambiguous overload for 'operator>>' in 'ts.QTextStream::operator>>(((QString&)(& pad))) >> value.QVariant::toInt(0u)'
error:no match for 'operator>>' in 'ts.QTextStream::operator>>(((QString&)(& pad))) >> QVariant::toString() const()'@
I have narrowed the problem down to the QVariant type.Please tell me the error in my way of thinking because I feel that I am missing something obvious here.
Thanks for your help in advance.P.S. Please note that pd is just a struct containing variables.
-
The toInt(), toBool(), toString(), etc. methods are used for fetching values FROM a QVariant as a specific type of variable, not in defining how to store data in a QVariant.
@
QVariant var;var = QString("3"); // Store the string "3" in the QVariant --> equivalent to var = QVariant("3");
int intVar = var.toInt(); // returns integer 3
bool boolVar = var.toBool(); // returns "true" because 3 != 0
QString strVar = var.toString(); // returns "3" as a QStringvar = 0; // Store the integer 0 in the QVariant --> equivalent to "var = QVariant(0);"
intVal = var.toInt(); // Returns the integer 0
boolVar = var.toBool(); // Returns "false" because 0 == 0
strVar = var.toString(); // Returns "0" as a QString.
@In your case, you probably want to stream data directly into actual int , QString, or bool variables (or whatever) rather than trying to use a QVariant as a temporary container/converter.
Check out the QTextStream::operator>>() methods.
Edit: Additionally, you may want to consider using QSettings to handle storing/reading this type of data (assuming that you are also the one writing the data to begin with.) It's a much cleaner interface and is designed for this very type of operation.
-
Are you absolutely bound to QTextStream? If not, I strongly recommend using a [[Doc:QDataStream]] which handles writing and reading all sorts of types in a very convenient way!
-
Thanks guys for the replies and wow, I feel embarrassed. I had checked the documentation for QVariant before I made the post and saw that the functions did have a return type, but I somehow did not put two and two together.
The good thing is that I am not strictly bound to QTextStream since I control both reading and writing of the file. I'm more concerned about the neatness of the code so I was trying to avoid using actual int and QString variables. However I'll look into both the QSettings and QDataStream that you guys have recommended and figure things out from there.
Again, thanks for the quick reply, I really appreciate it.
-
Why aren't you using QSettings to store your settings?
-
[quote author="Tobias Hunger" date="1316845290"]Why aren't you using QSettings to store your settings?[/quote]
That's already been mentioned above. :-)