Convert QString to char



  • How do I convert a QString to a char. When using latcardinalDirection = latlist[3].QLatin1Char(); the compiler says that QString does not have a member called QLatin1Char(). May you please assist?

    @
    char latcardinalDirection,longcardinalDirection ;
    QString latitudestring = QInputDialog::getText(0, "Latitude"," Degrees Minutes Seconds Cardinal Direction: ",QLineEdit::Normal,"",&ok,0);

        QStringList latlist;
        latlist = latitudestring.split(" ");
        int latdegrees = latlist[0].toInt();
        int latminutes = latlist[1].toInt();
        int latseconds = latlist[2].toInt();
        latcardinalDirection = latlist[3].QLatin1Char();@


  • Normally the compiler is right saying a member is not existing if it is not.

    If you are sure that the string is set correctly you can use something like
    @
    latlist[3].c_str()[0]
    @

    (Did not check const correctness on this)


  • Lifetime Qt Champion

    Hi,

    QLatin1Char() is not a method it's a constructor (invalid in this case)

    You can use e.g.

    @latlist[3][0].toAscii();@



  • As SGaist wrote "QLatin1Char() is not a method it’s a constructor"
    in your case
    @latlist[3][0].toAscii();@
    is good solution. In future if you need char array or QByteArray you can use:
    @latlist[3].toLatin1()@


Log in to reply
 

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