Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Coversion of QString to char array
Qt 6.11 is out! See what's new in the release blog

Coversion of QString to char array

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 22.8k 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.
  • Naveen_DN Offline
    Naveen_DN Offline
    Naveen_D
    wrote on last edited by
    #1

    Hi all,

    I want to convert QString to char array. how can i do this ?

    for eg: i have a QString Str="Qt Qml Development"
    and a char text[100]={"Some text "}

    i want to replace Some text with the above Str

    Can anyone please guide me how i can do this.

    Thanks

    Naveen_D

    J.HilkJ 1 Reply Last reply
    0
    • Naveen_DN Naveen_D

      Hi all,

      I want to convert QString to char array. how can i do this ?

      for eg: i have a QString Str="Qt Qml Development"
      and a char text[100]={"Some text "}

      i want to replace Some text with the above Str

      Can anyone please guide me how i can do this.

      Thanks

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Naveen_D hi,
      seems like static_cast<char> should help you.

      I'm not sur if you can convert a QString into an char-array directly, therefore I propose this:

      //Warning untested code
      QString myString;
      char text [myString.length())];
      
      for(int i = 0; i < myString.length(); i++)
          text[i] = static_cast<char>(myString.at(i));
      

      This would also be a bit easier if you used QChar


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      Naveen_DN 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @Naveen_D hi,
        seems like static_cast<char> should help you.

        I'm not sur if you can convert a QString into an char-array directly, therefore I propose this:

        //Warning untested code
        QString myString;
        char text [myString.length())];
        
        for(int i = 0; i < myString.length(); i++)
            text[i] = static_cast<char>(myString.at(i));
        

        This would also be a bit easier if you used QChar

        Naveen_DN Offline
        Naveen_DN Offline
        Naveen_D
        wrote on last edited by
        #3

        @J.Hilk Thanks for the reply,

        i tried with this i am getting the following error

        error: invalid static_cast from type ‘const QChar’ to type ‘char’
        text[i] = static_cast<char>(myString.at(i));

        error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
        char text[100] = {textStr};

        i tried like this ->

        QString myString="Qt Qml";
                char textStr [myString.length()];
                for(int i = 0; i < myString.length(); i++)
                    textStr[i] = static_cast<char>(myString.at(i));
                char text[100] = {textStr};
        

        Naveen_D

        E 1 Reply Last reply
        0
        • Naveen_DN Naveen_D

          @J.Hilk Thanks for the reply,

          i tried with this i am getting the following error

          error: invalid static_cast from type ‘const QChar’ to type ‘char’
          text[i] = static_cast<char>(myString.at(i));

          error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
          char text[100] = {textStr};

          i tried like this ->

          QString myString="Qt Qml";
                  char textStr [myString.length()];
                  for(int i = 0; i < myString.length(); i++)
                      textStr[i] = static_cast<char>(myString.at(i));
                  char text[100] = {textStr};
          
          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Naveen_D Use toLocal8Bit() or toLatin1() and use QByteArray::data and possibly QByteArray/Related Non-Members/qstrcpy.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            depends on the encoding. for UTF-8:

            const QByteArray stringData = myString.toUtf8();
            char text[100];
            text[qMin(99,stringData.size())]='\0';
            std::copy(stringData.constBegin(),stringData.constBegin()+qMin(99,stringData.size()),text);
            

            This, of course, might truncate any multi-byte character at the end of the string

            char textStr [myString.length()]; this is not valid C++ syntax

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            Naveen_DN 1 Reply Last reply
            2
            • VRoninV VRonin

              depends on the encoding. for UTF-8:

              const QByteArray stringData = myString.toUtf8();
              char text[100];
              text[qMin(99,stringData.size())]='\0';
              std::copy(stringData.constBegin(),stringData.constBegin()+qMin(99,stringData.size()),text);
              

              This, of course, might truncate any multi-byte character at the end of the string

              char textStr [myString.length()]; this is not valid C++ syntax

              Naveen_DN Offline
              Naveen_DN Offline
              Naveen_D
              wrote on last edited by
              #6

              @VRonin Thank you sir. it is working now. \o/

              Naveen_D

              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