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
QtWS25 Last Chance

Coversion of QString to char array

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 20.2k Views
  • 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.
  • N Offline
    N Offline
    Naveen_D
    wrote on 18 Apr 2017, 12:43 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 1 Reply Last reply 18 Apr 2017, 12:58
    0
    • N Naveen_D
      18 Apr 2017, 12:43

      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 Online
      J Online
      J.Hilk
      Moderators
      wrote on 18 Apr 2017, 12:58 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.

      N 1 Reply Last reply 18 Apr 2017, 13:13
      0
      • J J.Hilk
        18 Apr 2017, 12:58

        @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

        N Offline
        N Offline
        Naveen_D
        wrote on 18 Apr 2017, 13:13 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 18 Apr 2017, 13:30
        0
        • N Naveen_D
          18 Apr 2017, 13:13

          @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 18 Apr 2017, 13:30 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
          • V Offline
            V Offline
            VRonin
            wrote on 18 Apr 2017, 13:32 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

            N 1 Reply Last reply 19 Apr 2017, 05:53
            2
            • V VRonin
              18 Apr 2017, 13:32

              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

              N Offline
              N Offline
              Naveen_D
              wrote on 19 Apr 2017, 05:53 last edited by
              #6

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

              Naveen_D

              1 Reply Last reply
              0

              6/6

              19 Apr 2017, 05:53

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved