Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt + protobuf, types?

I would like to dip into Google's protocol buffers in Qt development, but I am having trouble figuring out how to incorporate them best.

Ultimately, I want to send with QUdpSocket and QTcpSocket using protocol buffers.

What is the best method for going between a protocol buffer message to sending the data over a socket (QByteArray) and then back again at the other side?

like image 889
Jay Avatar asked Nov 23 '10 04:11

Jay


1 Answers

Creating a QByteArray from a protobuf object:

Person person; // a protobuf object
person.set_id(123);
person.set_name("Bob");
person.set_email("[email protected]");

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());
sendSerializedPersonOverQTcpSocket(byteArray);

Reading back a protobuf object from a QByteArray:

QByteArray byteArray = readSerializedPersonFromQTcpSocket();
Person person;
if (!person.ParseFromArray(byteArray, byteArray.size())) {
  std::cerr << "Failed to parse person.pb." << std::endl;
}
like image 176
Vijay Mathew Avatar answered Sep 19 '22 18:09

Vijay Mathew