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. Qstring to const char*
QtWS25 Last Chance

Qstring to const char*

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.7k 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.
  • R Offline
    R Offline
    r_spb
    wrote on 22 Jan 2014, 07:22 last edited by
    #1

    Hello.
    I'm trying to use QList to send data via serial port.

    @QList<QString> command;

    command << "ant.hold.az -60\n"
    << "ant.holding\n";@

    How should I convert qstring to send data with QIODevice::write(const char * data)?
    Thanks fot help.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 22 Jan 2014, 07:31 last edited by
      #2

      @QString sdata;
      char *cdata = sdata.toLocal8Bit().data();@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • U Offline
        U Offline
        uranusjr
        wrote on 22 Jan 2014, 07:39 last edited by
        #3

        First you need to join the strings together into a single string object.

        Method 1: Straightforward concatenation
        @
        QString result;
        foreach (QString s, command)
        result += s;
        @

        Method 2: Use QStringList

        @
        QStringList commandSL(command);
        QString result = commandSL.join("");
        @

        The choice between the above two depends on how long your list, and the strings in it, are. If you are doing relatively light operation, straightforward concatenation is better, because QString probably has enough pre-allocated space for simple copying. If the strings you are dealing with is quite long, however, you should use QStringList and join.

        Now, with a single QString instance, you can convert it to const char * quite easily. Simply select one of the methods that convert a QString into QByteArray based on the encoding you wish to use (toUtf8, toLatin1, etc.), and then call constData on the QByteArray, e.g.

        @
        const char *data = result.toUtf8().constData();
        @

        Check the docs for more information.

        • "QString":http://qt-project.org/doc/qt-5/qstring.html
        • "QStringList":http://qt-project.org/doc/qt-5/qstringlist.html
        • "QByteArray":http://qt-project.org/doc/qt-5/qbytearray.html
        1 Reply Last reply
        0
        • R Offline
          R Offline
          r_spb
          wrote on 22 Jan 2014, 08:45 last edited by
          #4

          Again, thanks for help!

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Jan 2014, 21:56 last edited by
            #5

            Hi,

            You can use the write(const QByteArray &byteArray) overload, that will save you some line of codes.

            And possibly some memory access error

            @char *cdata = sdata.toLocal8Bit().data();@
            toLocal8Bit() returns a temporary QByteArray that will exist only during this line.

            @QByteArray local8Bit = sdata.toLocal8Bit();
            char *cdata = local8Bit.data();@

            Would be a more clean solution but you should rather use something like

            @serialPort->write(data.toLocal8Bit());@

            Hope it helps

            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
            0

            2/5

            22 Jan 2014, 07:31

            • Login

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