Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to type cast or convert from QString to char *
QtWS25 Last Chance

How to type cast or convert from QString to char *

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 6 Posters 3.3k 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by
    #1

    structurechar.fileName = stuct.fileNameQString.toStdString().c_str();
    here stuct.fileNameQString is a QString type and
    structurechar.fileName is a character pointer.
    please help me in resolving this.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What will you do with your structurechar ?

      What you are currently doing is using a pointer to a char array build from a temporary std::string object which means that your fileName is pointing to a memory location that's not valid anymore by the end of the line.

      If you want to use that structure later on, you have to properly allocate fileName and copy the data inside it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • K Offline
        K Offline
        kishore_hemmady
        wrote on last edited by
        #3

        @SGaist
        I am connecting my QT code to a standard C++ code,
        here my stuct.fileNameQString contains a file path which is of QString ,
        while assigning this data to the other module code like to the structure
        structurechar.fileName which is of type char * (char pointer) i am getting an cast error.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          My point stays: properly allocate fileName and copy the stuff you need in there.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            My point stays: properly allocate fileName and copy the stuff you need in there.

            M Offline
            M Offline
            mvuori
            wrote on last edited by
            #5

            The point is very valid one, but doesn't explain the cast error.
            Whenever there is an error, it is important to see the error message exactly, not just a hint that there was one.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kishore_hemmady
              wrote on last edited by
              #6

              @SGaist
              Still i am facing the same problem even after allocating the memory for the structure.
              void MainWindow::on_clicked()
              {

               structptr = ( mystruct*)malloc(sizeof(mystruct));
              if (structptr==NULL) {
                  qDebug()<<"ERROR!";
              
              }
              else
                  structptr->word=GUI_videostruct.videoFileName.toStdString().c_str();;
                  qDebug()<<"the file name is"<<*structptr;
                  free(structptr);
              

              }
              where strcutptr is the structure obj of mystruct * type
              error: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
              structptr->word=GUI_videostruct.videoFileName.toStdString().c_str();;
              ^

              jsulmJ 1 Reply Last reply
              0
              • K kishore_hemmady

                @SGaist
                Still i am facing the same problem even after allocating the memory for the structure.
                void MainWindow::on_clicked()
                {

                 structptr = ( mystruct*)malloc(sizeof(mystruct));
                if (structptr==NULL) {
                    qDebug()<<"ERROR!";
                
                }
                else
                    structptr->word=GUI_videostruct.videoFileName.toStdString().c_str();;
                    qDebug()<<"the file name is"<<*structptr;
                    free(structptr);
                

                }
                where strcutptr is the structure obj of mystruct * type
                error: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
                structptr->word=GUI_videostruct.videoFileName.toStdString().c_str();;
                ^

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @kishore_hemmady What you are doing is not what @SGaist suggested. He suggested to copy the STRING.
                So, copy what GUI_videostruct.videoFileName.toStdString().c_str() returns to your allocated string of same size...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • K Offline
                  K Offline
                  kishore_hemmady
                  wrote on last edited by
                  #8

                  @jsulm
                  I am not getting it,
                  can you help me by the snippet please.

                  jsulmJ JonBJ 2 Replies Last reply
                  0
                  • K kishore_hemmady

                    @jsulm
                    I am not getting it,
                    can you help me by the snippet please.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @kishore_hemmady

                    size_t size = strlen(stuct.fileNameQString.toStdString().c_str());
                    structurechar.fileName = malloc(size);
                    strncpy(structurechar.fileName, stuct.fileNameQString.toStdString().c_str(), size);
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • K kishore_hemmady

                      @jsulm
                      I am not getting it,
                      can you help me by the snippet please.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @kishore_hemmady
                      I believe you want to know why you get the compilation error you see?

                      c_str() returns a const char*. Note the const. This C-string is considered immutable, i.e. it must not be updated.

                      You do not show your definition of mystruct.word, but I'm guessing that is char *, not const char *? So it is mutable.

                      Hence the error messge.

                      Is that what you wanted to know about the error?

                      K 1 Reply Last reply
                      3
                      • JonBJ JonB

                        @kishore_hemmady
                        I believe you want to know why you get the compilation error you see?

                        c_str() returns a const char*. Note the const. This C-string is considered immutable, i.e. it must not be updated.

                        You do not show your definition of mystruct.word, but I'm guessing that is char *, not const char *? So it is mutable.

                        Hence the error messge.

                        Is that what you wanted to know about the error?

                        K Offline
                        K Offline
                        kishore_hemmady
                        wrote on last edited by
                        #11

                        @JonB
                        @jsulm
                        @SGaist

                        Thank you.The issue is solved

                        1 Reply Last reply
                        1
                        • SR__S Offline
                          SR__S Offline
                          SR__
                          wrote on last edited by
                          #12

                          @kishore_hemmady
                          Have you lookedd at https://wiki.qt.io/Technical_FAQ#How_can_I_convert_a_QString_to_char.2A_and_vice_versa.3F ?

                          Do not forget to allocate space for the final 0 of the C-string btw.

                          1 Reply Last reply
                          3

                          • Login

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