[SOLVED] QDomElement and QString::toDouble



  • Hi all, I am using Qt 5.2.1
    I found that saving qreal values into an XML tag like this:

    @QPointF pos = getPosition();
    QDomElement tag = doc->createElement("Position");
    tag.setAttribute("xPos", pos.x());
    tag.setAttribute("yPos", pos.y());@

    And then loading them like this:

    @QPointF pos(0, 0);
    pos.setX(tag.attribute("xPos").toDouble());
    pos.setY(tag.attribute("yPos").toDouble());@

    Always returns a 0,0 QPointF.
    I found out the cause is that written values have a "," like these:
    @<Position xPos="4588,235294117647" yPos="921,5686274509804"/>@

    While Qt expects number with a ".", therefore not parsing values correctly.
    Am I missing something or have I just found a bug ?

    If I remember correctly this was working on Qt 4.8.5.

    Thanks


  • Moderators

    What happens when you use this:
    @
    tag.setAttribute("xPos", QString::number(pos.x()));
    tag.setAttribute("yPos", QString::number(pos.y()));
    @



  • We had the same problem. Our solution is to change either the locale of your system or the locale for the shell executing your application.
    This might be done directly from the shell or in the properties of QtCreator if you use it.
    We never managed to find a solution which works at any time so far. For some reasons setting the locale via the dedicated c functions does not work.



  • [quote author="sierdzio" date="1392767623"]What happens when you use this:
    @
    tag.setAttribute("xPos", QString::number(pos.x()));
    tag.setAttribute("yPos", QString::number(pos.y()));
    @[/quote]

    This seems to be a valid solution. It cuts digits after the decimal points to 2 but if you don't need extreme precision (like in my case) it's ok.

    Thanks sierdzio !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.