qt protobuf使用

来源:互联网 发布:微信网页授权回调域名 编辑:程序博客网 时间:2024/04/29 03:03

GitHub:下载地址:https://github.com/google/protobuf
自定义.proto文件然后通过工具生成.h/.cc文件
这里写图片描述
这里写图片描述
生成.h/.cc文件
这里写图片描述
这里写图片描述

pro文件中

CONFIG(debug,debug|release){#debug    macx:{        # macOS only        LIBS += -L"$$PWD/third_party_lib/lib/macOS" \            -lprotobufd\            -lgmappingd    }    linux-g++:{        # linux only        LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \            -lprotobufd\            -lgmappingd    }    win32:{        # windows only        LIBS += -lWs2_32        LIBS += -lnetapi32        LIBS += -lmpr        LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \            -lprotobufd\            -lgmappingd \            -lopencv_core2410d.dll\            -lopencv_highgui2410d.dll\            -lopencv_imgproc2410d.dll    }}
 CONFIG(release,debug|release){#release macx:{        # macOS only        LIBS += -L"$$PWD/third_party_lib/lib/macOS" \            -lprotobuf\            -lgmapping    }    linux-g++:{        # linux only        LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \            -lprotobuf\            -lgmapping    }    win32:{        # windows only        LIBS += -lWs2_32        LIBS += -lnetapi32        LIBS += -lmpr        LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \        -lprotobuf\        -lgmapping \        -lopencv_core2410.dll\        -lopencv_highgui2410.dll\        -lopencv_imgproc2410.dll    }}

在代码中使用

//.h#include "Messages/message_map.pb.h"Message_MapLog _map_log_;//.cpp#include "google/protobuf/util/json_util.h"#include "google/protobuf/util/type_resolver.h"#include "google/protobuf/util/type_resolver_util.h" //注册MapData信号槽可用    qRegisterMetaType<Message_MapLogData*>("Message_MapLogData*");//读取离线文件数据bool GThreadDrawMap::readOffLineMap(const QString&filename){    _map_log_.Clear();    QFile file(filename);    bool is_ok = file.open(QIODevice::ReadOnly);    if (is_ok) {        QByteArray b = file.readAll();        //--------------------------------        google::protobuf::util::TypeResolver* type_resolver =                 google::protobuf::util::                NewTypeResolverForDescriptorPool("",                                                 google::protobuf::DescriptorPool::generated_pool());        std::string str;        std::string str_url = "/";        str_url.append("rbk.protocol.Message_MapLog");        google::protobuf::util::JsonToBinaryString(type_resolver, str_url,b.toStdString(), &str);        _map_log_.ParseFromString(str);        //-----------------------------------        if (""!=str)        {            is_ok = _map_log_.ParseFromString(str);            if(is_ok){            }else{                qDebug()<<"map_data_.ParseFromString: not ok";            }        }else{            qDebug()<<"google::protobuf::util::JsonToBinaryString is empty";             PublicClass::init()->showMessage(tr("这是一个空的地图文件"));        }        file.close();    }else{              PublicClass::init()->showMessage(tr("打开地图文件失败(可能是文件被占用)."));    }    return  is_ok;//Message_MapLog*;}//保存  ofstream ofs(path.toStdString(),ios::out|ios::binary);    if (ofs) {        google::protobuf::util::TypeResolver* type_resolver = google::protobuf::util::NewTypeResolverForDescriptorPool("",google::protobuf::DescriptorPool::generated_pool());        google::protobuf::util::JsonOptions json_options;        std::string json_buffer;        std::string str_url = "/";        str_url.append("rbk.protocol.Message_Map");        google::protobuf::util::BinaryToJsonString(type_resolver,str_url,current_map_data_.SerializeAsString(),&json_buffer,json_options);        ofs<<json_buffer;        ofs.flush();        ofs.close();    }
1 0