How to Setup Qt for Go on Mac

In this tutorial, you will learn how to setup Qt for Go on Mac.

Be default Qt supports development with C and C++. With the popularity of Go Lang smaller learning curve, there is a demand for using Go instead of C/C++. But developers dont want to leave the robust deployment platform that helps in making builds for Android, iOS, Windows, Mac and Linux.

In this tutorial, you will learn how to setup Qt for Go on Mac. Steps are similar for other  platforms but we will take care of that in other post.

Step 1: Install Xcode and the Command Line Tools:

You can install Xcode from the Mac App Store. Here is the App Store Link

https://itunes.apple.com/us/app/xcode/id497799835

Now to install the Command Line Tools run the following command in a new terminal window.

xcode-select --install

Now we are ready to move to next step.

Step 2 : Qt Setup

There are several ways you can setup Qt on Mac. Here are some of the popular package managers which you can use to install Qt.

  1. Homebrew
  2. Macports
  3. Nix

But none of the above will support mobile platforms. Thus we will no go with the package managers. Rather, we will download and install directly from the Qt website.

By itself Qt is a paid tool. If you are just getting started then you can use the Open Source version of Qt. Although the premium version has more features and plugins.

As of today if your annual revenue is more than $250K, then you will have to pay more than $4000 every year. But if your annual revenue is less than $250K then you will just need to pay $499 per year.

For this post we will use the opensource version of Qt. You can download Qt from the below link on the Qt website.

https://www.qt.io/download-open-source

When you are installing then it will ask for the folder where you want to install. It is recommnded to install in the Applications directory. For example if the Qt version is "5.14.2", then install in the folder "/Applications/Qt/5.14.2"

I also make an alias of the latest version as "/Applications/Qt/Latest" which points to "/Applications/Qt/5.14.2" or which ever is the latest version. This way I can have multiple versions of Qt installed.

Also make sure that you select all the appropriate components while you are installing Qt. It is recommended to install support for all the mobile and desktop platforms.

Now that Qt is installed we will need to setup some environment variables and paths.

Add the following lines to you profile files.

export QT_DIR="/Applications/Qt" export QT_VERSION="5.14.2" export PATH="/Applications/Qt/Latest/clang_64/bin:$PATH" export LDFLAGS="-L/Applications/Qt/Latest/clang_64/lib" export CPPFLAGS="-I/Applications/Qt/Latest/clang_64/include"

Now your Qt setup is complete. You can try running some samples  using the Qt Creator.

Before moving ahead we have to make sure that we are able to call Qt from command line. Try running the below command.

neeraj@Neerajs-MacBook Documents % $QT_DIR/$QT_VERSION/clang_64/bin/qmake -query
You should get similar output as shown below

QT_SYSROOT: QT_INSTALL_PREFIX:/Applications/Qt/5.14.2/clang_64 QT_INSTALL_ARCHDATA:/Applications/Qt/5.14.2/clang_64 QT_INSTALL_DATA:/Applications/Qt/5.14.2/clang_64 QT_INSTALL_DOCS:/Applications/Qt/Docs/Qt-5.14.2 QT_INSTALL_HEADERS:/Applications/Qt/5.14.2/clang_64/include QT_INSTALL_LIBS:/Applications/Qt/5.14.2/clang_64/lib QT_INSTALL_LIBEXECS:/Applications/Qt/5.14.2/clang_64/libexec QT_INSTALL_BINS:/Applications/Qt/5.14.2/clang_64/bin QT_INSTALL_TESTS:/Applications/Qt/5.14.2/clang_64/tests QT_INSTALL_PLUGINS:/Applications/Qt/5.14.2/clang_64/plugins QT_INSTALL_IMPORTS:/Applications/Qt/5.14.2/clang_64/imports QT_INSTALL_QML:/Applications/Qt/5.14.2/clang_64/qml QT_INSTALL_TRANSLATIONS:/Applications/Qt/5.14.2/clang_64/translations QT_INSTALL_CONFIGURATION:/Applications/Qt/5.14.2/clang_64 QT_INSTALL_EXAMPLES:/Applications/Qt/Examples/Qt-5.14.2 QT_INSTALL_DEMOS:/Applications/Qt/Examples/Qt-5.14.2 QT_HOST_PREFIX:/Applications/Qt/5.14.2/clang_64 QT_HOST_DATA:/Applications/Qt/5.14.2/clang_64 QT_HOST_BINS:/Applications/Qt/5.14.2/clang_64/bin QT_HOST_LIBS:/Applications/Qt/5.14.2/clang_64/lib QMAKE_SPEC:macx-clang QMAKE_XSPEC:macx-clang QMAKE_VERSION:3.1 QT_VERSION:5.14.2

If you didnt or got some errors then you need to recheck your installation.

Now that we are done with Qt setup, time to move to the next step.

Step 3 : Go Setup

For Go I would suggest that you use homebrew or any of the other package managers. Here is the command for installing Go on Mac using Homebrew.

brew install go

Its rare that you will have any issues while installing Go. Still, you can test your setup by running the go command to check if everything is setup correctly.

Here is the result when I run the go command.

neeraj@Neerajs-MacBook qt-go % go

Go is a tool for managing Go source code.

Usage:

go [arguments]

The commands are:

bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages

Use "go help " for more information about a command.

Additional help topics:

buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions

Use "go help " for more information about that topic.

dsd

If you got output similar to above then you are good to go :)

Step 4: Qt-Go Bindings

We will be using the Go Bindings from "therecipe" on Github. So let us clone the repo by running the following command.

go get -v -tags=no_env github.com/therecipe/qt/cmd/...

Once you have successfully cloned the repo, you will need to run the setup as follows.

$(go env GOPATH)/bin/qtsetup

Now you are good to go. We are now done with all the setups.

Step 5: Running the samples

We can run a simple hello world sample to check if our setup is running fine.

qtdeploy test desktop github.com/therecipe/examples/basic/widgets

If everything is working correctly then it should open the following window.

hello-world-1

Conclusion

Now that we have setup Qt for Go, we can get started with developing some basic apps in Go using Qt and Qml. If you face any issues while doing the setup, please share it in the comments below. Also if you have any suggestions or some alternate ways for setup then also share the resources for other who are following this post.

Categories: : Tutorials