Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Retrieving data of various types from file using QTextStream
Qt 6.11 is out! See what's new in the release blog

[Solved] Retrieving data of various types from file using QTextStream

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bobby B
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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 QString

      var = 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.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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!

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bobby B
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

            Why aren't you using QSettings to store your settings?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              [quote author="Tobias Hunger" date="1316845290"]Why aren't you using QSettings to store your settings?[/quote]

              That's already been mentioned above. :-)

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved