Golang : Qt splash screen with delay example
Back in the 90s and early 2000's most software developers will develop for graphical user interface(GUI) application rather than web based application. It is a common practice to show a splash screen
first before allowing the user to use the main application. Below is an example of how to show a splash screen for a Qt GUI application with Golang.
Please follow the setup instruction for Golang-Qt bindings at https://github.com/therecipe/qt
Tips: Dump out the documentation to text files for easier search and viewing.
$godoc github.com/therecipe/qt/widgets > widgets.txt
$godoc github.com/therecipe/qt/gui > gui.txt
$godoc github.com/therecipe/qt/core > core.txt
To run the program below, use your own image file and save it to splash-screen.jpg
. We will use this image below for this example.
Credit : https://unsplash.com/search/splash?photo=WKIJ_ozmXNI by @bady
Here you go!
package main
import (
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
"os"
"time"
)
var (
mainApp *widgets.QApplication
item *widgets.QGraphicsPixmapItem
imageFileName string
)
func delaySecond(n time.Duration) {
time.Sleep(n * time.Second)
}
func showSplashScreen(delay int) {
var pixmap = gui.NewQPixmap5("splash-screen.jpg", "", core.Qt__AutoColor)
splash := widgets.NewQSplashScreen(pixmap, core.Qt__Window)
splash.Show()
delaySecond(time.Duration(delay))
splash.DestroyQSplashScreen()
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
mainWindow := widgets.NewQMainWindow(nil, core.Qt__Window)
mainWindow.SetWindowTitle("Splash screen main window")
mainWindow.ShowMaximized()
// can use Go routine to launch splash screen
// and show for 8 seconds but Qt will complain
// go showSplashScreen(8)
showSplashScreen(8)
widgets.QMessageBox_Information(nil, "OK", "Splash screen is gone now and time to quit. Click OK to close.", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
mainApp.Quit()
}
go build
the code above and place the executable in the same directory as the file splash-screen.jpg
. Execute the program and you should see the splash screen in action.
Photo credit:
https://unsplash.com/search/splash?photo=WKIJ_ozmXNI by @bady
References:
Golang delay in seconds and minutes function
https://wiki.qt.io/Howtocreateasplashscreenwithaninduced_delay
See also : Golang : Qt image viewer example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.9k Golang : How to pass map to html template and access the map's elements
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+4.5k Linux/MacOSX : How to symlink a file?
+6.3k Golang : Convert an executable file into []byte example
+7.6k Golang : Get today's weekday name and calculate target day distance example
+22.2k Generate checksum for a file in Go
+13.3k Golang : Activate web camera and broadcast out base64 encoded images
+5.5k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+6.7k Golang : Gargish-English language translator
+21k Curl usage examples with Golang
+3.4k Golang : Switch Redis database redis.NewClient
+8.7k Golang : Go as a script or running go with shebang/hashbang style