Important: Please read the Qt Code of Conduct -

About QByteArray

  • Hello, everyone.
    I'm using Qt 5.15.1 + MSVC2019 32bit.
    I have a few questions.
    When I compile the following code

    There's a warning on line 22.
    C4309: 'conversion': truncation of constant value

    I think the BYTE range is 256, why?

    There is an error on line 23.
    call to member function 'append' is ambiguous
    candidate function

    On line 15, it's not a problem, so why is it?

    Best regards, thank you.

    1	#include <Windows.h>
    3	void MainWindow::on_pushButton_clicked()
    4	{
    5	    BYTE data[5];
    6	    data[0] = 0x08;
    7	    data[1] = 0x86;
    8	    data[2] = 0x00;
    9	    data[3] = 0x41;
    10	    data[4] = 0x53;
    12	    QByteArray Byt;
    14	    for(int i=0; i<5; i++){
    15	        Byt.append(data[i]);
    16	    }
    18	    ui->lineEdit->setText(Byt.toHex(' '));
    20	    Byt.clear();
    21	    Byt.append(0x08);
    22	    Byt.append(0x86);
    23	    Byt.append(0x00);						call to member function 'append' in ambiguous
    24	    Byt.append(0x41);
    25	    Byt.append(0x53);
    27	    ui->lineEdit_2->setText(Byt.toHex(' '));
    28	}

  • @taku-s said in About QByteArray:
    BYTE is probably unsigned char, or similar. In lines 21--25 you are passing literal numbers, which are ints.

    The warning on #22 is doubtless because Byt.append(0x86); has its top bit set/negative value? At a guess, the "ambiguous" Byt.append(0x00); is because with value 0 it could be a pointer and it doesn't know between QByteArray &QByteArray::append(char ch) and QByteArray &QByteArray::append(const char *str)?

    Make your constant values chars, like char(0x08) or unsigned char(0x08), or as character constants like '\x08'.

  • Thank you for explaining this in detail.
    I've managed to understand it.

Log in to reply