Question

i need add on firt position of qbytearray a quint16 and after read it: How can i to do it?

I have try this:

quint16 pos = 0;
QFile file(m_pathFile);
if (file.open(QFile::ReadOnly))
{
    qDebug() << "el fichero existe";
    m_udpSocket->bind(m_port);
    QByteArray datagram;
    while (!file.atEnd())
    {
        datagram.begin();
        datagram.append(pos++);
        datagram = file.read(m_blockSize);
        qDebug() << "Sec" << datagram.at(0);
    }
 }

Thanks you very much

I got add with:

datagram.begin();
datagram.setNum(pos, 10);
datagram.append(file.read(m_blockSize));

but i don't know as read it

Thanks

Était-ce utile?

La solution

Ok, first of all, that datagram.begin() is useless since it returns an iterator that you don't assign at all. If you want to insert a number in the first position of a QByteArray you can do something like:

datagram.insert(0, QString::number(pos++));

To read it, the simplest way is to use a QTextStream like this:

QTextStream str(datagram);
quint16 num;
str >> num;

Also, take a look at the docs before posting, because the Qt ones are really simple and helpful if you know how to search (and it's not that difficult, trust me).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top