Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved How to type cast or convert from QString to char *

    Mobile and Embedded
    6
    12
    2421
    Loading More Posts
    • 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
      kishore_hemmady last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 4
        • K
          kishore_hemmady last edited by

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • M
              mvuori @SGaist last edited by

              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 Reply Quote 0
              • K
                kishore_hemmady last edited by

                @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();;
                ^

                jsulm 1 Reply Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion @kishore_hemmady last edited by

                  @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 Reply Quote 2
                  • K
                    kishore_hemmady last edited by

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

                    jsulm JonB 2 Replies Last reply Reply Quote 0
                    • jsulm
                      jsulm Lifetime Qt Champion @kishore_hemmady last edited by

                      @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 Reply Quote 1
                      • JonB
                        JonB @kishore_hemmady last edited by 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 1 Reply Last reply Reply Quote 3
                        • K
                          kishore_hemmady @JonB last edited by

                          @JonB
                          @jsulm
                          @SGaist

                          Thank you.The issue is solved

                          1 Reply Last reply Reply Quote 1
                          • SR__
                            SR__ last edited by

                            @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 Reply Quote 3
                            • First post
                              Last post