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. Help to port c# to Qt c++
Qt 6.11 is out! See what's new in the release blog

Help to port c# to Qt c++

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 8.9k Views 4 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi,

    Since you are likely going to use t somewhere else, rather:

    QByteArray trameBytes = QString(trame).toUtf8();
    char[] t = trameBytes.constData();
    

    otherwise you might be pointing to a buffer that can get invalidated.

    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
    • I Offline
      I Offline
      iTof26
      wrote on last edited by
      #4

      Ok, thank you for yours fast answers

      but when i try :
      QByteArray trameBytes = QString(trame).toUtf8();
      char t[] = trameBytes.constData();

      i can't compile:
      initializer fails to determine size of 't"
      array must be initialised wit a brace-enclosed initialiser

      i don't know how to determine the size because 'trame' is variable

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

        Depending on what you are going to do with it, use a const char *.

        In fact, what do you need t for ?

        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
        2
        • I Offline
          I Offline
          iTof26
          wrote on last edited by
          #6

          it 's for sending with QTcpsocket

          QByteArray trameBytes = QString(trame).toUtf8();
          const char* t[] = trameBytes.constData();

          clientSocket->write(...........

          but i have error when i compile

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #7

            Hi
            The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
            Why not use constData() directly ?
            clientSocket->write(trameBytes.constData(), xxx

            or
            const char* t = trameBytes.constData();

            Also if socket is Qt version, i think it can take ByteArray directly.

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

              No need for all that stuff then.

              clientSocket->write(frameBytes);

              is enough.

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

              E 1 Reply Last reply
              3
              • mrjjM mrjj

                Hi
                The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
                Why not use constData() directly ?
                clientSocket->write(trameBytes.constData(), xxx

                or
                const char* t = trameBytes.constData();

                Also if socket is Qt version, i think it can take ByteArray directly.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #9

                @mrjj said in Help to port c# to Qt c++:

                The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)

                You're correct, and our fellows fell into that one head first. :)

                char t[] is a valid construction only in two cases:

                1. If the size of t can be determined at compile-time, e.g. char t[] = { 'a', 'b' };.
                2. It's a formal parameter for a function, then char t[] equates to char * const t.
                  Note: When passing multidimensional arrays as formal parameters only the first dimension can be left without explicitly specifying the size.

                Read and abide by the Qt Code of Conduct

                raven-worxR 1 Reply Last reply
                1
                • kshegunovK kshegunov

                  @mrjj said in Help to port c# to Qt c++:

                  The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)

                  You're correct, and our fellows fell into that one head first. :)

                  char t[] is a valid construction only in two cases:

                  1. If the size of t can be determined at compile-time, e.g. char t[] = { 'a', 'b' };.
                  2. It's a formal parameter for a function, then char t[] equates to char * const t.
                    Note: When passing multidimensional arrays as formal parameters only the first dimension can be left without explicitly specifying the size.
                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #10

                  @kshegunov said in Help to port c# to Qt c++:

                  You're correct, and our fellows fell into that one head first. :)

                  yep, my fault. Answered too fast.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  kshegunovK 1 Reply Last reply
                  1
                  • raven-worxR raven-worx

                    @kshegunov said in Help to port c# to Qt c++:

                    You're correct, and our fellows fell into that one head first. :)

                    yep, my fault. Answered too fast.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #11

                    @raven-worx, I wasn't looking to place blame, I just found it somewhat funny (no offence). :D

                    Read and abide by the Qt Code of Conduct

                    raven-worxR 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @raven-worx, I wasn't looking to place blame, I just found it somewhat funny (no offence). :D

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #12

                      @kshegunov
                      i didn't take it as such anyway.
                      No piece of code exists without bugs ;)

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1
                      • I Offline
                        I Offline
                        iTof26
                        wrote on last edited by
                        #13

                        Perfect, finaly i use:

                        QByteArray trameBytes = QString(trame).toUtf8();
                        clientSocket->write(trameBytes.constData());

                        Thanks all for your help :)

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

                          Again: there's no need to call constData. QTcpSocket has a write method that takes a QByteArray.

                          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
                          2
                          • SGaistS SGaist

                            No need for all that stuff then.

                            clientSocket->write(frameBytes);

                            is enough.

                            E Offline
                            E Offline
                            Eeli K
                            wrote on last edited by
                            #15

                            @SGaist said in Help to port c# to Qt c++:

                            No need for all that stuff then.

                            clientSocket->write(frameBytes);

                            is enough.

                            As a general rule of thumb, when you use Qt, use QString and QByteArray as early as you can and use char*-arrays as late as possible. If you create a string with "", create a QString: QString("a string you know compile time"). If you get [] array data from outside Qt program, convert it as soon as you get it. Most Qt classes/functions which can use char* directly have that possibility only for convenience: for example QTcpSocket and other classes which communicate with outside world take and give QByteArrays. Low level stuff or c++ standard library stuff is of course sometimes necessary or preferable (there might be several good reasons), but don't use it unless you have a specific reason. See docs for QString, QByteArray and QIODevice. They are all important basic classes in Qt programming. And of course the container classes like QList.

                            kshegunovK 1 Reply Last reply
                            1
                            • E Eeli K

                              @SGaist said in Help to port c# to Qt c++:

                              No need for all that stuff then.

                              clientSocket->write(frameBytes);

                              is enough.

                              As a general rule of thumb, when you use Qt, use QString and QByteArray as early as you can and use char*-arrays as late as possible. If you create a string with "", create a QString: QString("a string you know compile time"). If you get [] array data from outside Qt program, convert it as soon as you get it. Most Qt classes/functions which can use char* directly have that possibility only for convenience: for example QTcpSocket and other classes which communicate with outside world take and give QByteArrays. Low level stuff or c++ standard library stuff is of course sometimes necessary or preferable (there might be several good reasons), but don't use it unless you have a specific reason. See docs for QString, QByteArray and QIODevice. They are all important basic classes in Qt programming. And of course the container classes like QList.

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #16

                              With the minor note you should use QStringLiteral("some literal") or QLatin1Literal("latin 1 literal") to enable moc to do a bit of magic and optimize some constructor calls.

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0

                              • Login

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