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. accessing C string within QString

accessing C string within QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.0k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 19 Sept 2017, 16:11 last edited by
    #1

    Hi, all -

    I'm using the output of a QSerialPort read to update a QPlainTextEdit. The serial port read uses a standard C char *, while the QPlainTextEdit expects a QString. I'm trying to find an efficient way to do this (hopefully not having to copy the buffer from char * to QString).

    Is there some way for me to access the C string within the QString as the target of the read()?

    If I'm "doing this wrong" feel free to so inform me.

    Thanks...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 19 Sept 2017, 16:14 last edited by
      #2

      Hi
      Normally its
      QByteArray QIODevice::readAll()

      so where does the char * come from?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 19 Sept 2017, 16:18 last edited by
        #3

        Anyway, QString have
        QString(const char *str)
        so it can be constructed directly from char *

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mzimmers
          wrote on 19 Sept 2017, 16:20 last edited by
          #4

          I'm using this call.

          I haven't done much with QByteArrays; do they efficiently convert to QStrings? I'm communicating with a primitive device whose communications is a bit erratic, so I need to move data out of the input buffer as quickly as possible.

          M K 2 Replies Last reply 19 Sept 2017, 16:25
          0
          • M mzimmers
            19 Sept 2017, 16:20

            I'm using this call.

            I haven't done much with QByteArrays; do they efficiently convert to QStrings? I'm communicating with a primitive device whose communications is a bit erratic, so I need to move data out of the input buffer as quickly as possible.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 19 Sept 2017, 16:25 last edited by mrjj
            #5

            @mzimmers
            Hi
            Well you can do
            QString s_data = QString::fromLatin1 fromAscii(data.data()); // data being ByteArray, ( fromAscii is obsolete. sorry)
            so i think they offer better handling of utf8 etc but i do not think its faster than
            constructing a QString from char * buffer.

            so if the read() works fine then when ready, make sure its 0 terminated and
            simply do
            PlainTextEdit->append( QString( mycharbuffer ) ) ;

            1 Reply Last reply
            0
            • M mzimmers
              19 Sept 2017, 16:20

              I'm using this call.

              I haven't done much with QByteArrays; do they efficiently convert to QStrings? I'm communicating with a primitive device whose communications is a bit erratic, so I need to move data out of the input buffer as quickly as possible.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 19 Sept 2017, 16:27 last edited by kshegunov
              #6

              Use this instead. Then use QString::fromLatin1 assuming that's the encoding your device uses.

              I haven't done much with QByteArrays; do they efficiently convert to QStrings?

              That would depend what is in the byte array, but generally not "very efficiently". Strings are encoding aware, while array of bytes is just that - an array of bytes.

              Read and abide by the Qt Code of Conduct

              M 1 Reply Last reply 19 Sept 2017, 16:48
              0
              • A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 19 Sept 2017, 16:47 last edited by
                #7

                @mzimmers: Don't worry about conversion speed, with serial ports, the longest time is to
                wait for the characters to arrive.

                You didn't tell us the bitrate you are using, but I guess it's in the upper micro- or lower millisecond range for one char to come in.

                Converting one character from char to QChar (a QString is an array of QChars) should take max. lower microsecond range.

                The fastest possible conversion is fromLatin1(), if you receive pure ASCII or Latin-1 encoded strings, you can use this. In case you have to convert fromUtf8(), it will be a bit slower, but trust me: you will not feel a difference.

                Qt has to stay free or it will die.

                1 Reply Last reply
                1
                • K kshegunov
                  19 Sept 2017, 16:27

                  Use this instead. Then use QString::fromLatin1 assuming that's the encoding your device uses.

                  I haven't done much with QByteArrays; do they efficiently convert to QStrings?

                  That would depend what is in the byte array, but generally not "very efficiently". Strings are encoding aware, while array of bytes is just that - an array of bytes.

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 19 Sept 2017, 16:48 last edited by
                  #8

                  @kshegunov thanks. I was hoping for some way of avoiding the copy entirely (like port->read(myQString->someMagicAccessToCString) but I guess that's not possible.

                  Anyway, as aha pointed out, it probably doesn't matter. My desktop is arbitrarily fast compared to the serial port.

                  1 Reply Last reply
                  0

                  1/8

                  19 Sept 2017, 16:11

                  • Login

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