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. Hot to express "Int16[] myInt16Array = new Int16[5];" in Qt?

Hot to express "Int16[] myInt16Array = new Int16[5];" in Qt?

Scheduled Pinned Locked Moved General and Desktop
widget
5 Posts 3 Posters 1.7k Views 3 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
    mkdh
    wrote on last edited by
    #1

    C# Code:
    Int16[] myInt16Array = new Int16[5];

    Hot to express the code in Qt?

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

      Hi,

      It's nothing Qt specific, it's a C++ question. Here you have a good explanation about dynamic allocation.

      However depending on what you want to do with your array, you could just use a QVector<qint16> so no need for dynamic allocation, just put your ints in it.

      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

      M 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        It's nothing Qt specific, it's a C++ question. Here you have a good explanation about dynamic allocation.

        However depending on what you want to do with your array, you could just use a QVector<qint16> so no need for dynamic allocation, just put your ints in it.

        Hope it helps

        M Offline
        M Offline
        mkdh
        wrote on last edited by
        #3

        @SGaist
        Thanks.
        I thought you mean the two way:

        Method 1:
        QList<qint16> myInt16Array;

        Method 2:
        qint8 *myInt16Array;
        myInt16Array=new qint8[5];

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          It's nothing Qt specific, it's a C++ question. Here you have a good explanation about dynamic allocation.

          However depending on what you want to do with your array, you could just use a QVector<qint16> so no need for dynamic allocation, just put your ints in it.

          Hope it helps

          M Offline
          M Offline
          mkdh
          wrote on last edited by mkdh
          #4

          @SGaist
          Q1:
          If my C# code look like below:

              byte[] mybyte = { };
              if (bWriteBmp)
              {
                  Bitmap mybmp = this.DrawBitmap(myFile, 75, 65);//<----my function
                  if (mybmp!= null)
                  {
                      MemoryStream ms = new MemoryStream();
                      mybmp.Save(ms, ImageFormat.Bmp);
                      mybyte = ms.ToArray();
                  }
              }
          

          Hot to express the code in Qt?

          Q2:
          my C# code:
          myCheck = BitConverter.GetBytes(myValue);

          Could I alway use the below code in Qt for any types of "myCheck"?
          (ex. set qint16 "myCheck" or set qint32 "myCheck" or set int "myCheck")
          myCheck = QByteArray::fromRawData((const char *)&myValue, sizeof(myValue));

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            @mkdh You're asking to translate a C# code in C++/Qt. I suggest to study the C++ language and then move to Qt.

            In your case you can find very useful theQByteArray class.

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            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