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. Weird stuff with QVariant::toBool()

Weird stuff with QVariant::toBool()

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 209 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 the following code:

    	QVariant temp{ workspace.value("Stacking/BackgroundCalibration", true) };
    	bool backgroundCalibration = temp.toBool();
    	// bool backgroundCalibration = workspace.value("Stacking/BackgroundCalibration", true).toBool();
    

    The third line is commented out as I wanted to inspect the variable in the debugger.

    The debugger shows me this:

    eafbdc8e-8197-4c17-a4b5-f15b5b667fa1-image.png

    I suspect that the QVariant isn't being set correctly ... which is almost certainly my fault! Is that a correct representation of a bool QVariant or have I stuffed a character string in there when I shouldn't have?

    In this case the settings were initialised from a file which contains lines like:
    #V5WS#Stacking/BackgroundCalibration#false
    The code that reads the file looks like this:

    	WORKSPACESETTINGITERATOR it = findSetting(keyName);
    	if (it != m_vSettings.end())
    	{
    		//
    		// Special Case for Stacking/Mosaic - don't allow Custom mode
    		// 
    		if ("Stacking/Mosaic" == keyName && "2" == value)
    		{
    			value.setNum(static_cast<int>(SM_INTERSECTION)); // Force Intersection mode
    		}
    		//
    		// 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);
    		QMetaType type = it->value().metaType();
    		ZASSERT(variant.canConvert(type));
    		variant.convert(type);
    		it->setValue(variant);
    		bResult = true;
    	}
    

    Thanks,
    David

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

      Problem found - the code that read from QSettings (elsewhere in the code) wasn't checking the MetaType of the existing QVariant() when reading all the values so bools were getting converted to QStrings.

      JonBJ 1 Reply Last reply
      0
      • PerdrixP Perdrix

        I have the following code:

        	QVariant temp{ workspace.value("Stacking/BackgroundCalibration", true) };
        	bool backgroundCalibration = temp.toBool();
        	// bool backgroundCalibration = workspace.value("Stacking/BackgroundCalibration", true).toBool();
        

        The third line is commented out as I wanted to inspect the variable in the debugger.

        The debugger shows me this:

        eafbdc8e-8197-4c17-a4b5-f15b5b667fa1-image.png

        I suspect that the QVariant isn't being set correctly ... which is almost certainly my fault! Is that a correct representation of a bool QVariant or have I stuffed a character string in there when I shouldn't have?

        In this case the settings were initialised from a file which contains lines like:
        #V5WS#Stacking/BackgroundCalibration#false
        The code that reads the file looks like this:

        	WORKSPACESETTINGITERATOR it = findSetting(keyName);
        	if (it != m_vSettings.end())
        	{
        		//
        		// Special Case for Stacking/Mosaic - don't allow Custom mode
        		// 
        		if ("Stacking/Mosaic" == keyName && "2" == value)
        		{
        			value.setNum(static_cast<int>(SM_INTERSECTION)); // Force Intersection mode
        		}
        		//
        		// 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);
        		QMetaType type = it->value().metaType();
        		ZASSERT(variant.canConvert(type));
        		variant.convert(type);
        		it->setValue(variant);
        		bResult = true;
        	}
        

        Thanks,
        David

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

        @Perdrix
        What is your question?

        If you think you can rely on debugger view for some variable or memory location where you do not know what has been optimized where you cannot or should not.

        (As side note, asking about the details of a debugger, or for that matter compiler, without saying what tool or platform won't get far.)

        Especially since temp here is just a local variable. If you want to see what is in it reliably do something like pass it as an argument to some function (which access it as a parameter) and look there, we are you sure it will have to be "valid".

        UPDATE:
        On careful inspection of screenshot it looks like the value read in was a string, false, in the first place? It is the variable backgroundCalibration whose value you do not like the look of? It would really help if you mentioned this in your post. Local int/bool variable is even more likely not to guarantee you will see its value get set in memory.

        But actually everything shown in the debugger, including backgroundCalibration, was correct from the start. So don't know what the issue per your question ever was.

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

          Problem found - the code that read from QSettings (elsewhere in the code) wasn't checking the MetaType of the existing QVariant() when reading all the values so bools were getting converted to QStrings.

          JonBJ 1 Reply Last reply
          0
          • PerdrixP Perdrix has marked this topic as solved on
          • PerdrixP Perdrix

            Problem found - the code that read from QSettings (elsewhere in the code) wasn't checking the MetaType of the existing QVariant() when reading all the values so bools were getting converted to QStrings.

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

            @Perdrix OK, wasn't worth the time looking and trying to send you an answer.

            Christian EhrlicherC 1 Reply Last reply
            0
            • JonBJ JonB

              @Perdrix OK, wasn't worth the time looking and trying to send you an answer.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @JonB said in Weird stuff with QVariant::toBool():

              OK, wasn't worth the time looking and trying to send you an answer.

              And you are still trying... :P

              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
                #6

                Problem is now fixed...

                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