PDA

View Full Version : QByteArray initialization



vivekyuvan
20th September 2017, 10:26
12585Hi Friends

I am facing an issue in QByteArray when QByteArray Initialization.

I can initialize a QByteArray like:



QByteArray m_data;
m_data[0] = 0x0c;
m_data[1] = 0x06;
m_data[2] = 0x04;
m_data[3] = 0x04;
m_data[4] = 0x02;
m_data[5] = 0x00;


But I would like something more compact, like:


QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

Unfortunately, this form isn't allowed:

error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};


I want to pass m_data(array Data ) in emit signal. Any code example and snippet will be helpful.


Thanks In Advance

Best Regards
Vivek Yuvan

ado130
20th September 2017, 11:03
I'm not sure if this is the best way, but this should works:

char temp[3] = {0x0c, 0x06, 0x12};
QByteArray m_data = QByteArray((char*)temp, 3);