Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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

    Hot to express the code in Qt?


  • Lifetime Qt Champion

    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



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

    Method 1:
    QList<qint16> myInt16Array;

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



  • @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));



  • @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.


Log in to reply