Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Convert content of QlineEdite to Char *
Forum Updated to NodeBB v4.3 + New Features

[Solved] Convert content of QlineEdite to Char *

Scheduled Pinned Locked Moved General and Desktop
15 Posts 7 Posters 8.5k Views 2 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.
  • M Offline
    M Offline
    MohammadReza
    wrote on last edited by
    #1

    Hello friends.

    I want to convert content of QlineEdit to char *. I know the content is Qsrting.
    I found a code that convert Qstring to const char * (not char *).
    I really appreciate suggest some code that convert Qstring to char * or const char * o char *.

    Thanks a lot.
    Ya Ali.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      To me, this sounds like:

      • Allocate a char* buffer
      • Copy the string content from the const char* to the char* buffer
      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        get the text from the line edit
        @
        QString text = ui->lineEdit->text()
        @

        convert the text to QByteArray
        @
        QByteArray ba = text.toLatin1();
        @

        convert QByteArray to char *
        @
        const char * characters = ba.data();
        @

        all in all:
        @
        const char * characters = ui->lineEdit->text().toLatin1().data();
        @

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

          Hi,

          @const char * characters = ui->lineEdit->text().toLatin1().data();@

          This one's a dangerous shortcut, it's taking the address of the content of a temporary QByteArray.

          @
          QByteArray byteArray = ui->lineEdit->text.toLatin1();
          const char *characters = byteArray.constData();
          @
          Is the safe method

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

          J 1 Reply Last reply
          2
          • V Offline
            V Offline
            vezprog
            wrote on last edited by
            #5

            I apologize! and I agree SGaIst.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MohammadReza
              wrote on last edited by
              #6

              Thanks to all.
              Any idea to convert const char * to char *?
              Is this a abnormal conversion?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on last edited by
                #7

                [quote author="MohammadReza" date="1396059354"]
                Is this a abnormal conversion?[/quote]
                Usually yes, it is not good idea to do a const cast on a pointer to a buffer that you received.
                If you need to modify such buffer then make a copy, modify and put it back using api.
                But if you want it then use standard c++ const_cast.
                @
                const char* bar;
                char* foo = const_cast<char*>(bar)
                @

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MohammadReza
                  wrote on last edited by
                  #8

                  Thanks to dear andreyc & other friends reply my question.
                  Solved!

                  Ya Ali.

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    Xander84
                    wrote on last edited by
                    #9

                    just use
                    @
                    byteArray.data();
                    @
                    instead of
                    @
                    byteArray.constData();
                    @
                    if you need a non const pointer (no need for a cast), but the question is why do you need that at all? I would not suggest you modify the data in the QByteArray via the char* array..

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MohammadReza
                      wrote on last edited by
                      #10

                      Hi dear Xandeer84.
                      I searched & found another way. Like this:

                      @
                      QString MyQString = "Only Allah";
                      const char *MyConstChar = MyQString.toStdString().c_str();
                      @

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        Xander84
                        wrote on last edited by
                        #11

                        You might lose information if you do that, because a std::string cannot contain all the unicode chars from the QString I think.
                        I think it would help if you tell us what you want to do with that char* and why you need it.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          MohammadReza
                          wrote on last edited by
                          #12

                          I had a function that received char *.
                          There isn`t anymore, because i changed it const char *.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Asperamanca
                            wrote on last edited by
                            #13

                            [quote author="andreyc" date="1396066897"]@
                            const char* bar;
                            char* foo = const_cast<char*>(bar)
                            @
                            [/quote]

                            That sounds like a really bad idea in this case. Nobody expects the const char* to change, so any changes made will cause trouble.

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Hi,

                              @const char * characters = ui->lineEdit->text().toLatin1().data();@

                              This one's a dangerous shortcut, it's taking the address of the content of a temporary QByteArray.

                              @
                              QByteArray byteArray = ui->lineEdit->text.toLatin1();
                              const char *characters = byteArray.constData();
                              @
                              Is the safe method

                              J Offline
                              J Offline
                              Jc_Opc
                              wrote on last edited by
                              #14

                              @SGaist Hi, I had an issue with data corruption because of this. In this example I can see that you create a local variable QByteArray instead. Could you please clarify why the content of the QByteArray in one single line case is temporary? I may assume it is because it is called implicitly. In my example I used toStdString().c_str(), in the current function of my example it printed out with qDebug OK but when reading that global const char* in another place it was garbage.

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

                                @Jc_Opc Hi,

                                It's a question of object lifetime.

                                In this case:

                                const char * characters = ui->lineEdit->text().toLatin1().data();
                                

                                the QByteArray returned by toLatin1 ends its life on the same line it was created.
                                Therefore characters is pointing invalid memory as soon as the line ends.

                                You have the exact same issue:

                                toStdString().c_str();
                                

                                The std::string returned by toStdString() ends its life the same way as the QByteArray above.

                                qDebug() << myQString.toStdString().c_str();
                                

                                works because everything happens on the same line.

                                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
                                3

                                • Login

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