Qt protobuf使用

GitHub:下载地址:https://github.com/google/protobuf

protobuf 编译

https://blog.csdn.net/u012020854/article/details/104514865

如何构造一个简单的.proto

这里写图片描述
其中 repeated 类似于list ,但是它没有提供删除函数。。。

如何把.proto 生成.h/.cc文件

这里写图片描述
这里写图片描述

如果你看完上面的内容就开始敲命令那么你将会很懊恼:
1.新建.txt文件,复制一下数据,改文件名为 protoc.bat
2.把该文件与 protoc.exe放在同一目录下
3.把写好的 .proto文件拷贝到 protoc.exe目录下
4.双击protoc.bat运行(如果没有生成.h,.cc 那么就是你的.proto写错了,请敲上面介绍的的命令行方法,它会提示错误)

@echo off
for /f "delims=" %%a in ('dir /b/a-d/oN *.proto') do (
start protoc.exe --cpp_out=./ %%a
)

msvc 编译器使用时,需要导出宏、

@echo off
for /f "delims=" %%a in ('dir /b/a-d/oN *.proto') do (
start protoc.exe --cpp_out=dllexport_decl=YHMSVCDLL_EXPORT:./ %%a
)

在这里插入图片描述

如何在.pro文件中添加 protobuf 库

注:如果在.pri,pro等文件中看到【$$PWD】表示.pri/.pro文件所在的路径

$$PWD/third_party_lib/lib/mingw_x86 =>.pro同路径下third_party_lib文件夹


CONFIG(debug,debug|release){#debug

    macx:{
        # macOS only
        LIBS += -L"$$PWD/third_party_lib/lib/macOS" \
            -lprotobufd
    }
    linux-g++:{
        # linux only
        LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \
            -lprotobufd
    }
    win32:{
        # windows only
        LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \
            -lprotobufd
    }
}
CONFIG(release,debug|release){#release
 macx:{
        # macOS only
        LIBS += -L"$$PWD/third_party_lib/lib/macOS" \
            -lprotobuf
    }
    linux-g++:{
        # linux only
        LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \
            -lprotobuf
    }
    win32:{
        # windows only
        LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \
        -lprotobuf
    }
}

如何在代码中使用

//.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();
    }
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值