Qt 中開發 Protobuf 自動指令碼技巧分享

2021-09-10 06:19:33 字數 3919 閱讀 1680

目錄

安裝 protobuf

新建 protoc 自動化指令碼工程

新建 protodemo 測試工程

最近在研究谷歌的 google protocol buffer( 簡稱 protobuf) ,是 google 公司內部的混合語言資料標準,用於 rpc 系統和持續資料儲存系統。protocol buffers 是一種輕便高效的結構化資料儲存格式,可以用於結構化資料序列化,或者說序列化。它很適合做資料儲存或 rpc 資料交換格式。可用於通訊協議、資料儲存等領域的語言無關、平台無關、可擴充套件的序列化結構資料格式。作為乙個 qt 開發者當然要最大化的利用工具的整合功能。

第一步當然是安裝 protobuf 環境了,我這裡介紹的是基於 ubuntu 16.04 作業系統和 c++ 開發語言的安裝步驟,如果需要其他作業系統和語言的安裝方法protobuf github

安裝依賴:

安裝依賴的執行命令如下:

sudo apt-get install autoconf automake libtool curl make g++ unzip

通過下面幾個命令,您可以獲取到 protobuf 的倉庫**和子模組**:

$ git clone 

$ cd protobuf

$ git submodule update --init --recursive

$ ./autogen.sh

通過下面命令編譯 protobuf 的執行時庫和 protoc 工具:

$ ./configure

$ make

$ make check

$ sudo make install

$ sudo ldconfig # refresh shared library cache.

注意:如果「make check」沒有執行通過或發生錯誤,並不會對下面的命令產生影響,可以正常安裝。上面的操作都是執行的預設操作,protoc 安裝的路徑為:/usr/local/bin。

我們安裝完 protobuf 之後,發現要在控制台/命令列執行 protoc 才能生成標頭檔案和原始檔。想一想,如果我們的工程其實只需要 proto 介面檔案就可以了,標頭檔案和原始檔只是編譯過程需要的過程產物,並不是我們專案工程必須依賴的檔案,例如 dbus 的 xml 檔案。有沒有辦法,在專案工程檔案裡面只要配置 proto_files += ***x.proto 就可以讓 qt 專案工程一鍵構建執行呢?答案是肯定的。

第一步:我們先用 qt creator 新建乙個「empty qmake project」(qt creator 新建嚮導的「其他專案」),專案工程名稱為:protoc,編輯工程檔案:protoc.pro,內容如下:

template = aux

prf.files = protoc.prf

prf.path = $$[qt_host_data]/mkspecs/features

installs += prf

# ensure files are copied to qtbase mkspecs for non-prefixed builds

!force_independent:if(!debug_and_release|!build_all|config(release, debug|release))

prffiles2build.input = prf.files

prffiles2build.output = $$[qt_host_data]/mkspecs/features/$

prffiles2build.commands = $$qmake_copy $ $

prffiles2build.name = copy $

prffiles2build.config = no_link target_predeps

qmake_extra_compilers += prffiles2build

}

第二步:在 protoc 專案工程中新建乙個名為:protoc.prf 的檔案,內容如下:

cmd = /usr/local/bin/protoc

contains(qmake_host.os, windows) .exe)

}exists($$cmd): qt_tool.protoc.binary = $$cmd

# qtpreparetool honors qt_tool.protoc.binary if set

qtpreparetool(qmake_protoc, protoc)

definereplace(protofilepath) )

}definereplace(protofilename)

groups =

for(entry, proto_files) .files)

isempty(files) else _proto_file

}groups *= $$group

input_list = $$upper($$group)_list

for(subent, $$list($$unique(files)))

$$input_list += $$subent

}}for(group, groups) _list

$$_header.input = $$input_list

$$_header.output = $.pb.h

$$_header.variable_out = headers

$$_header.depends = $ $$qt_tool.protoc.binary

$$_header.commands = $$qmake_protoc --proto_path=$ --cpp_out=$$out_pwd $

$$_source.input = $$input_list

$$_source.output = $.pb.cc

$$_source.variable_out = sources

$$_source.depends += $.pb.h

$$_source.commands = $$escape_expand(\\n) # dummy command

qmake_extra_compilers += $$_header $$_source

}

第三步:在 qt creator 右鍵執行構建命令,會自動將 protoc.prf 指令碼檔案拷貝到 $$[qt_host_data]/mkspecs/features 目錄下面,在後面其他的工程中配置 config += protoc 即可引入該指令碼。

config += console c++11

config -= qt

# set protobuf pkg-config

config += link_pkgconfig

pkgconfig += protobuf

# set protoc config

config += protoc

proto_files = demo.proto

sources += main.cpp

第二步:在 protodemo 專案工程中新建乙個名為:demo.proto 的檔案,內容如下:

syntax = "proto3";

message demo

#include #include "demo.pb.h"

int main(int, char *)

構建並執行 demo 執行結果如下:

a: my name is protodemo !

b: hello protodemo !

Qt開發從青銅都王者 自動布局

使用qt進行各種控制項進行布局時,我們往往會用到qt的各種自動布局類,對比將控制項固定在某一固定座標,自動布局的好處是控制項可以根據介面的大小按照比例進行自動調整,達到 自適應 的效果。qt中常用的布局有qvboxlayout 垂直布局 qhboxlayout 水平布局 qgridlayout 網格...

protobuf中的omitempty欄位

一句話總結 帶有omitempty欄位的成員,如果該字段為nil 字串 空陣列等 則打包的json結果不會有這個字段。我們把proto檔案自動生成go 時會出現omitempty欄位,如下 type reply struct直接上 package main import encoding json ...

Protobuf 中的型別檢查

在使用protobuffer時,如果定義乙個訊息如下 enum my enum enum type1 1,enum type2 2 message my msg required my enum test enum 1 那麼,在protoc生成的 中,在賦值時 set test enum const...