Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Converting value read from external file to Bool QVariant



  • 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



  • @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


  • Lifetime Qt Champion

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



  • 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



  • 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.



  • @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.



  • @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


  • Lifetime Qt Champion

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


Log in to reply