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. Converting value read from external file to Bool QVariant
Forum Updated to NodeBB v4.3 + New Features

Converting value read from external file to Bool QVariant

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    I have an external file created by an earlier version of my code (before I changed to use Qsettings.

    As an example consider the following record in the file:

    #WS#Software\DeepSkyStacker\FitsDDP|FITSisRAW=0

    The new code reads and parses that line and converts Software\DeepSkyStacker\FitsDDP|FITSisRAW to give a QSettings key of "FitsDDP/FITSisRAW", and a QString value for the stuff on the RHS of the = sign in this case "0".

    My QSettings for "FitsDDP/FITSisRAW" have this as a bool type.

    So my code goes:

                    WORKSPACESETTINGITERATOR			it;
    		it = findSetting(keyName);
    		if (it != m_vSettings.end())
    		{
    			//
    			// In all cases when we enter here the variable "value" will be
    			// a QString.
    			// We need to convert it to the same type as is currently stored
    			//
    			QVariant variant(value);
    			QVariant::Type type = it->value().type();
    			ZASSERT(variant.canConvert(type));
    			variant.convert(type);
    			it->setValue(variant);
    			bResult = true;
    		}
    

    I read the current type of the variable using it->value().type() which returns the value 1 (Bool)

    and then convert it to type Bool using variant.convert(type);

    Unfortunately at this point the value of the QVariant is true NOT false which doesn't seem correct to me!

    HELP

    1 Reply Last reply
    0
    • PerdrixP Offline
      PerdrixP Offline
      Perdrix
      wrote on last edited by
      #6

      @Bonnie said in Converting value read from external file to Bool QVariant:

      qDebug() << v;
      qDebug() << v.convert(QMetaType::Bool);
      qDebug() << v;

      Aha! The value of the string showed in the debugger in hover mode as just "0", but it actually contained:

      "0\n"

      So I was defeated by trailing white space. I see QString::trimmed() in my future :)

      Many thanks for your help
      David

      Christian EhrlicherC 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        What's the content of 'value' which you're trying to convert to a bool? Why is it a string in the first place?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #3

          I thought that was 100% clear from my post the value the QString whose name is value is "0" as that's what was read from the file.

          D

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

            I tried below code

            QString s("0");
            QVariant v(s);
            qDebug() << v;
            qDebug() << v.convert(QMetaType::Bool);
            qDebug() << v;
            

            The output is

            QVariant(QString, "0")
            true
            QVariant(bool, false)
            

            So I think the converting is correct.
            Maybe you can also print variant before and after the converting to check its value.

            1 Reply Last reply
            4
            • PerdrixP Perdrix

              I thought that was 100% clear from my post the value the QString whose name is value is "0" as that's what was read from the file.

              D

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Perdrix
              As @Bonnie has shown. In the case of converting a QString to a bool, https://doc.qt.io/qt-5/qvariant.html#toBool:

              Returns true [...] if the variant has type QMetaType::QString or QMetaType::QByteArray and its lower-case content is not one of the following: empty, "0" or "false"; otherwise returns false.

              1 Reply Last reply
              0
              • PerdrixP Offline
                PerdrixP Offline
                Perdrix
                wrote on last edited by
                #6

                @Bonnie said in Converting value read from external file to Bool QVariant:

                qDebug() << v;
                qDebug() << v.convert(QMetaType::Bool);
                qDebug() << v;

                Aha! The value of the string showed in the debugger in hover mode as just "0", but it actually contained:

                "0\n"

                So I was defeated by trailing white space. I see QString::trimmed() in my future :)

                Many thanks for your help
                David

                Christian EhrlicherC 1 Reply Last reply
                0
                • PerdrixP Perdrix

                  @Bonnie said in Converting value read from external file to Bool QVariant:

                  qDebug() << v;
                  qDebug() << v.convert(QMetaType::Bool);
                  qDebug() << v;

                  Aha! The value of the string showed in the debugger in hover mode as just "0", but it actually contained:

                  "0\n"

                  So I was defeated by trailing white space. I see QString::trimmed() in my future :)

                  Many thanks for your help
                  David

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Perdrix Then please mark the topic as solved, thx.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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