这是本系列的第三篇文章,前两篇我们讲了qt的安装和编译,今天我们讲一讲程序的打包。
好像我们现在都没怎么讲到qt的使用,因为想要放开手脚写代码,一些基础是要打牢的。
不过请放心,下一篇文章开始我们就会真正进入正题了。
这是针对使用qtdeploy时的打包教程,不适用于使用qt-tools + go build进行构建的情况。
打包
首先我们做一些打包前的准备工作,没错,做事之前先做好准备是个好习惯:-p。
这次用于打包的仍然是一个小例子,将一张图片缩小一半显示出来,这个例子正好需要使用外部资源,因此我也会在其中展示qrc的用法。
项目结构:
tree makedeb
makedeb ├── images │ └── 1.jpg ├── main.go └── makedeb.qrc
没错,images里的就是我们要显示的图片,makedeb.qrc是我们的资源配置文件,因为是打包发布,所以我们使用qrc来同一管理外部资源。
<!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>images/1.jpg</file> </qresource> </RCC>
qrc的配置和原生Qt一样,这里不多做解释。
下面是主程序:
1 package main 2 3 import ( 4 "os" 5 6 "github.com/therecipe/qt/gui" 7 "github.com/therecipe/qt/core" 8 "github.com/therecipe/qt/widgets" 9 ) 10 11 func main() { 12 widgets.NewQApplication(len(os.Args), os.Args) 13 14 window := widgets.NewQMainWindow(nil, 0) 15 window.SetWindowTitle("Test deb package") 16 17 img := gui.NewQPixmap5(":/images/1.jpg", "", core.Qt__AutoColor) 18 size := img.Size() 19 img = img.ScaledToHeight(size.Height()/2, core.Qt__FastTransformation) 20 img = img.ScaledToWidth(size.Width()/2, core.Qt__FastTransformation) 21 22 canvas := widgets.NewQLabel(window, 0) 23 canvas.SetPixmap(img) 24 25 window.SetCentralWidget(canvas) 26 window.Show() 27 28 widgets.QApplication_Exec() 29 }
将图片读取至QPixmap,然后在使用QLabel来显示的简单例子,其中
img := gui.NewQPixmap5(":/images/1.jpg", "", core.Qt__AutoColor)
“:/images/1.jpg”表示按照.qrc指定的规则来获取资源文件1.jpg。
接下来就是编译了,只需要简单的一条代码
qtdelpoy build desktop makedeb
再看一看现在的目录结构:
makedeb ├── deploy │ └── linux │ ├── lib │ ├── makedeb │ ├── makedeb.sh │ ├── plugins │ └── qml ├── images │ └── 1.jpg ├── linux ├── main.go ├── makedeb.qrc ├── rcc_cgo_linux_linux_amd64.go └── rcc.cpp