Overview
Qt is a cross-platform application framework.
Go (often referred to as golang) is a free and open source programming language created at Google.
This binding aims to make it as simple as possible to write applications for all operating systems supported by Qt in Go.
The project is pretty much a WIP and not recommended to be used in production yet.
However it should already contain everything you need to build fully featured Qt applications in Go.
Screenshots of the Line Edits example
Installation
Full or stub installation?
The full installation requires at least 8gb free ram and takes 20 min.
The stub installation requires only 1gb free ram and takes 10 min. (experimental)
The only difference between those two version is, that you won't be able to use go run/build
to build your applications if you choose to install the stub version.
You are therefore limited to the use of qtdeploy
to build your application.
To build the stub version export QT_STUB=true
system wide and proceed with the installation as usual.
Environmental variables
If you define environmental variables during the installation export them system wide, as they are needed not only during the installation but also later.
Add the environmental variables into your $HOME/.bash_profile
or $HOME/.profile
on macOS or Linux.
Or if you are on Windows simply use the advanced system settings.
Docker Images (experimental)
You can also use Docker images from hub.docker.
To cross compile to Windows, Linux and Android.
You will still need to install the desktop version for your host system first!
Command line debugging
You can export QT_DEBUG=true
before "qtdeploying" your application to enable printing of the current function name at runtime.
Speedup your development
Instead of using qtdeploy
during development, you might want to use qtrcc
, qtmoc
, qtminimal
manually with go build/run -tags=minimal ...
In general qtminimal
should always be called after the use of the optional use of qtrcc
and qtmoc
.
qtrcc
is used to bundle your project/qml
folder so you can access the files within by using the qrc
prefix, like this qrc:///qml/somefile
from various Qt functions.
qtmoc
is used to subclass core.QObject based classes and to add your own signals/slots as shown in some examples.
qtminimal
is used to create a small subset of the binding tailored to your code to reduce the binary size. (Use the -tags=minimal
flag to make use of it)
If you use the STUB version, you are forced to use qtminimal
otherwise it's optional like qtrcc
and qtmoc
.
Instructions
-
Desktop
-
Mobile
-
Embedded
-
Docker
Windows
-
Install Go >= 1.7.1 and setup a proper GOPATH
-
Install Qt 5.7.0
- Install the official prebuilt package; you can also define a custom location with QT_DIR.
or
- Install the Qt-dev package with MSYS2 and define QT_MSYS2=true or define a custom Qt location with QT_MSYS2_DIR (usually: C:\msys32\ or C:\msys64);
pacman -Syyu
- if you want to deploy 32-bit applications
pacman -S mingw-w64-i686-qt-creator mingw-w64-i686-qt5
- if you want to deploy 64-bit applications
pacman -S mingw-w64-x86_64-qt-creator mingw-w64-x86_64-qt5
pacman -Scc
-
Add the directory that contains gcc and g++ to your PATH (not needed with MSYS2)
C:\Qt\Qt5.7.0\Tools\mingw530_32\bin
-
Download the binding
go get -u -v github.com/therecipe/qt/cmd/...
-
Generate, install and test (20 min) (MSYS2: run within the MSYS2 MinGW 32-bit or MSYS2 MinGW 64-bit shell)
-
Create your first application
-
Deploy applications with %GOPATH%\bin\qtdeploy build desktop path\to\your\project
(MSYS2: run within the MSYS2 MinGW 32-bit or MSYS2 MinGW 64-bit shell)
macOS
-
Install Go >= 1.7.1 and setup a proper GOPATH
-
Install Qt 5.7.0
- Install the official prebuilt package; you can also define a custom location with QT_DIR.
or
- Install the Qt-dev package with Homebrew and define QT_HOMEBREW=true or define a custom Qt location with QT_DIR (usually: /usr/local/opt/qt5/); if you want to link against homebrews Qt libs (experimental)
-
Install Xcode >= 7.2.0; you can also define a custom location with XCODE_DIR
-
Download the binding
go get -u -v github.com/therecipe/qt/cmd/...
-
Generate, install and test (20 min)
-
Create your first application
-
Deploy applications with $GOPATH/bin/qtdeploy build desktop path/to/your/project
Linux
-
Install Go >= 1.7.1 and setup a proper GOPATH
-
Install Qt 5.7.0
- Install the official prebuilt package; you can also define a custom location with QT_DIR.
or
- Install the Qt-dev package with your system package manager; if you want to link against your system Qt libs (experimental)
- add export QT_PKG_CONFIG=true to your .profile or .bash_profile
- if needed you can also define custom locations for the misc and doc dir with QT_MISC_DIR and/or QT_DOC_DIR
- and you may want to define a custom pkg-config search path with PKG_CONFIG_PATH, if the default path points to old Qt pkg-config files
-
Install g++ >= 5 and OpenGL dependencies
-
Debian/Ubuntu (apt-get)
sudo apt-get -y install build-essential libgl1-mesa-dev
-
Fedora/RHEL/CentOS (yum)
sudo yum -y groupinstall "C Development Tools and Libraries"
sudo yum -y install mesa-libGL-devel
-
openSUSE (zypper)
sudo zypper -n install -t pattern devel_basis
-
Download the binding
go get -u -v github.com/therecipe/qt/cmd/...
-
Generate, install and test (20 min)
-
Create your first application
-
Deploy applications with $GOPATH/bin/qtdeploy build desktop path/to/your/project
(use the *.sh file to start your application)
Optional: Cross compile for Windows on Debian/Ubuntu (experimental)
-
Install Wine
sudo apt-get -y install wine
-
Install MXE (M cross environment)
-
echo "deb http://pkg.mxe.cc/repos/apt/debian wheezy main" | sudo tee --append /etc/apt/sources.list.d/mxeapt.list > /dev/null
-
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D43A795B73B16ABE9643FE1AFD8FFF16DB45C6AB
-
sudo apt-get update
-
if you want to deploy 32-bit applications sudo apt-get -y install mxe-i686-w64-mingw32.shared-qt3d mxe-i686-w64-mingw32.shared-qtactiveqt mxe-i686-w64-mingw32.shared-qtbase mxe-i686-w64-mingw32.shared-qtcanvas3d mxe-i686-w64-mingw32.shared-qtcharts mxe-i686-w64-mingw32.shared-qtconnectivity mxe-i686-w64-mingw32.shared-qtdatavis3d mxe-i686-w64-mingw32.shared-qtdeclarative mxe-i686-w64-mingw32.shared-qtdeclarative-render2d mxe-i686-w64-mingw32.shared-qtgamepad mxe-i686-w64-mingw32.shared-qtgraphicaleffects mxe-i686-w64-mingw32.shared-qtimageformats mxe-i686-w64-mingw32.shared-qtlocation mxe-i686-w64-mingw32.shared-qtmultimedia mxe-i686-w64-mingw32.shared-qtofficeopenxml mxe-i686-w64-mingw32.shared-qtpurchasing mxe-i686-w64-mingw32.shared-qtquickcontrols mxe-i686-w64-mingw32.shared-qtquickcontrols2 mxe-i686-w64-mingw32.shared-qtscript mxe-i686-w64-mingw32.shared-qtscxml mxe-i686-w64-mingw32.shared-qtsensors mxe-i686-w64-mingw32.shared-qtserialbus mxe-i686-w64-mingw32.shared-qtserialport mxe-i686-w64-mingw32.shared-qtservice mxe-i686-w64-mingw32.shared-qtsvg mxe-i686-w64-mingw32.shared-qtsystems mxe-i686-w64-mingw32.shared-qttools mxe-i686-w64-mingw32.shared-qttranslations mxe-i686-w64-mingw32.shared-qtvirtualkeyboard mxe-i686-w64-mingw32.shared-qtwebchannel mxe-i686-w64-mingw32.shared-qtwebkit mxe-i686-w64-mingw32.shared-qtwebsockets mxe-i686-w64-mingw32.shared-qtwinextras mxe-i686-w64-mingw32.shared-qtxlsxwriter mxe-i686-w64-mingw32.shared-qtxmlpatterns
-
if you want to deploy 64-bit applications sudo apt-get -y install mxe-x86-64-w64-mingw32.shared-qt3d mxe-x86-64-w64-mingw32.shared-qtactiveqt mxe-x86-64-w64-mingw32.shared-qtbase mxe-x86-64-w64-mingw32.shared-qtcanvas3d mxe-x86-64-w64-mingw32.shared-qtcharts mxe-x86-64-w64-mingw32.shared-qtconnectivity mxe-x86-64-w64-mingw32.shared-qtdatavis3d mxe-x86-64-w64-mingw32.shared-qtdeclarative mxe-x86-64-w64-mingw32.shared-qtdeclarative-render2d mxe-x86-64-w64-mingw32.shared-qtgamepad mxe-x86-64-w64-mingw32.shared-qtgraphicaleffects mxe-x86-64-w64-mingw32.shared-qtimageformats mxe-x86-64-w64-mingw32.shared-qtlocation mxe-x86-64-w64-mingw32.shared-qtmultimedia mxe-x86-64-w64-mingw32.shared-qtofficeopenxml mxe-x86-64-w64-mingw32.shared-qtpurchasing mxe-x86-64-w64-mingw32.shared-qtquickcontrols mxe-x86-64-w64-mingw32.shared-qtquickcontrols2 mxe-x86-64-w64-mingw32.shared-qtscript mxe-x86-64-w64-mingw32.shared-qtscxml mxe-x86-64-w64-mingw32.shared-qtsensors mxe-x86-64-w64-mingw32.shared-qtserialbus mxe-x86-64-w64-mingw32.shared-qtserialport mxe-x86-64-w64-mingw32.shared-qtservice mxe-x86-64-w64-mingw32.shared-qtsvg mxe-x86-64-w64-mingw32.shared-qtsystems mxe-x86-64-w64-mingw32.shared-qttools mxe-x86-64-w64-mingw32.shared-qttranslations mxe-x86-64-w64-mingw32.shared-qtvirtualkeyboard mxe-x86-64-w64-mingw32.shared-qtwebchannel mxe-x86-64-w64-mingw32.shared-qtwebkit mxe-x86-64-w64-mingw32.shared-qtwebsockets mxe-x86-64-w64-mingw32.shared-qtwinextras mxe-x86-64-w64-mingw32.shared-qtxlsxwriter mxe-x86-64-w64-mingw32.shared-qtxmlpatterns
-
Export QT_MXE_ARCH=386
to deploy 32-bit applications or QT_MXE_ARCH=amd64
to deploy 64-bit applications.
-
Generate, install and test (20 min)
$GOPATH/bin/qtsetup windows
-
Deploy applications with $GOPATH/bin/qtdeploy build windows path/to/your/project
Android
-
Install the desktop version for Windows, macOS or Linux
-
Unzip the Android SDK in C:\
or $HOME
; you can also define a custom location with ANDROID_SDK_DIR
-
Install the SDK dependencies with C:\android-sdk-windows\tools\android.bat
or $HOME/android-sdk-{ macosx | linux }/tools/android
- Tools
- Android SDK Build-tools (25.0.2)
- Android 7.1.1 (API 25)
- Extras (Windows only)
-
Unzip the Android NDK in C:\
or $HOME
; you can also define a custom location with ANDROID_NDK_DIR
-
Install Java SE Development Kit >= 8 (Linux: install in $HOME/jdk/
); you can also define a custom location with JDK_DIR
-
You may need to chmod
your $GOROOT/pkg
dir or run the setup as root
-
Install and test (20 min)
-
Create your first application
-
Deploy applications with [GOPATH]/bin/qtdeploy build android path/to/your/project
iOS
-
Install the desktop version for macOS
-
Install and test (20 min)
$GOPATH/bin/qtsetup ios && $GOPATH/bin/qtsetup ios-simulator
-
Create your first application
-
Deploy applications with $GOPATH/bin/qtdeploy build ios path/to/your/project
or $GOPATH/bin/qtdeploy build ios-simulator path/to/your/project
Sailfish OS
-
Install the desktop version for Windows, macOS or Linux
-
Install VirtualBox; you can also define a custom location with VIRTUALBOX_DIR
-
Install the Sailfish OS SDK; you can also define a custom location with SAILFISH_DIR
-
You may need to chmod
your $GOROOT
dir or run the setup as root
-
Install and test (20 min)
-
Create your first application
-
Deploy applications with [GOPATH]/bin/qtdeploy build sailfish path/to/your/project
or [GOPATH]/bin/qtdeploy build sailfish-emulator path/to/your/project
Raspberry Pi
-
Install the desktop version for Linux
-
Create a folder $HOME/raspi
-
Download and unpack the Qt source
cd $HOME/raspi && wget https://download.qt.io/official_releases/qt/5.7/5.7.0/single/qt-everywhere-opensource-src-5.7.0.tar.gz
tar -xzf qt-everywhere-opensource-src-5.7.0.tar.gz qt-everywhere-opensource-src-5.7.0
-
Patch Qt Source
-
cd $HOME/raspi/qt-everywhere-opensource-src-5.7.0/qtbase && sed -i 's/c++1z/c++11/' ./mkspecs/devices/linux-rpi3-g++/qmake.conf
-
cd $HOME/raspi/qt-everywhere-opensource-src-5.7.0/qtwayland && wget https://github.com/qtproject/qtwayland/commit/75294be3.patch && patch -p1 -i 75294be3.patch
-
Download the cross compiler; you can also define a custom location with RPI_TOOLS_DIR
cd $HOME/raspi && git clone --depth 1 https://github.com/raspberrypi/tools.git
-
Get dependencies and install Arch Linux on your SD card
-
Start your Raspberry Pi
-
Enable root login over ssh
-
export RASPI_IP=192.168.XXX.XXX
(replace XXX.XXX with the valid ip ending)
-
ssh alarm@$RASPI_IP
(password: alarm)
-
su
(password: root)
-
sed -i 's/#PermitRootLogin/PermitRootLogin/' /etc/ssh/sshd_config && sed -i 's/prohibit-password/yes/' /etc/ssh/sshd_config && systemctl restart sshd.service
-
Update and install dependencies (5 min)
-
Get sysroot for cross compiling (password: root) (5 min)
-
cd $HOME/raspi && mkdir sysroot sysroot/usr sysroot/opt
-
rsync -avz root@$RASPI_IP:/lib sysroot --delete
-
rsync -avz root@$RASPI_IP:/usr/include sysroot/usr --delete
-
rsync -avz root@$RASPI_IP:/usr/lib sysroot/usr --delete
-
rsync -avz root@$RASPI_IP:/opt/vc sysroot/opt --delete
-
Prepare sysroot; you can also define a custom location with RPI1_SYSROOT_DIR, RPI2_SYSROOT_DIR or RPI3_SYSROOT_DIR
cd $HOME/raspi && wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
chmod +x sysroot-relativelinks.py && ./sysroot-relativelinks.py sysroot
-
Build Qt (2 h)
-
Prepare the Qt directory
sudo chown -R $USER $HOME/Qt5.7.0/
-
You may need to chmod
your $GOROOT/pkg
dir or run the setup as root
-
Install and test the binding (20 min)
-
Raspberry Pi 1
-
Raspberry Pi 2
-
Raspberry Pi 3
-
Notes
-
run startx &
before starting an application with -platform xcb
(qml/quick applications won't work; they may work with Qt 5.8)
-
run weston &
or weston --tty=1 &
(via ssh) or create your own compositor before starting an application with -platform wayland-egl
(qml/quick applications won't work; they may work with Qt 5.8)
-
you can increase the available gpu memory by editing /boot/config.txt
-
Create your first application
-
Deploy applications with $GOPATH/bin/qtdeploy build rpiX path/to/your/project
(replace X with 1, 2 or 3)
Docker
-
Install the desktop version for Windows, macOS or Linux
-
Install and start Docker
-
Share your GOPATH host directory as a data volume with Docker.
And make sure your Project folder is in your GOPATH.
-
Download the necessary Docker image(s) and run the setup.
-
For cross compiling to Windows:
-
docker pull therecipe/qt:base_windows
(to deploy 32-bit applications)
-
docker pull therecipe/qt:base_windows_64
(to deploy 64-bit applications)
-
Optional: Install Wine to test your applications.
-
define QT_MXE_ARCH=386
to deploy 32-bit applications or QT_MXE_ARCH=amd64
to deploy 64-bit applications
-
$GOPATH/bin/qtsetup windows-docker
-
Deploy applications with $GOPATH/bin/qtdeploy build windows path/to/your/project docker
-
For cross compiling to Linux:
-
docker pull therecipe/qt:base
-
$GOPATH/bin/qtsetup linux-docker
-
Deploy applications with $GOPATH/bin/qtdeploy build linux path/to/your/project docker
-
For cross compiling to Android:
-
docker pull therecipe/qt:base_android
-
$GOPATH/bin/qtsetup android-docker
-
Deploy applications with $GOPATH/bin/qtdeploy build android path/to/your/project docker
Code Editor Settings (for code completion)
-
IntelliJ Idea
Help > Edit Custom Properties
# custom IntelliJ IDEA properties
idea.max.intellisense.filesize=999999
Help > Edit Custom VM Options
# custom IntelliJ IDEA VM options
-Xms128m
-Xmx2000m
-XX:ReservedCodeCacheSize=240m
-XX:+UseCompressedOops
-
Neovim
Update your neocomplete.vim
let g:neocomplete#skip_auto_completion_time = ""
Example
-
Create a project folder [GOPATH]/src/qtExample
-
Create a file [GOPATH]/src/qtExample/main.go
package main
import (
"os"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
)
func main() {
widgets.NewQApplication(len(os.Args), os.Args)
//create a button and connect the clicked signal
var button = widgets.NewQPushButton2("Click me!", nil)
button.ConnectClicked(func(flag bool) {
widgets.QMessageBox_Information(nil, "OK", "You clicked me!", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
})
//create a layout and add the button
var layout = widgets.NewQVBoxLayout()
layout.AddWidget(button, 0, core.Qt__AlignCenter)
//create a window, add the layout and show the window
var window = widgets.NewQMainWindow(nil, 0)
window.SetWindowTitle("Hello World Example")
window.SetMinimumSize2(200, 200)
window.Layout().DestroyQObject()
window.SetLayout(layout)
window.Show()
widgets.QApplication_Exec()
}
3. Open the terminal in `[GOPATH]/src` and run `[GOPATH]/bin/qtdeploy build desktop ./qtExample`
4. You will find the application in `[GOPATH]/src/qtExample/deploy/[GOOS]_minimal/`
5. Take a look at the [examples](https://github.com/therecipe/qt/tree/master/internal/examples)
6. Make yourself familiar with the [Qt documentation](https://doc.qt.io/qt-5/classes.html)
7. Join our Slack Channel [#qt-binding](https://gophers.slack.com/messages/qt-binding/details/)