Golang : Qt progress dialog example
Found a need to show progress indication while developing a Qt application recently. Below is a simple example of how to invoke the Qt ProgressDialog in Golang with Qt-binding.
What it does is to launch a GUI application with a button to press. Upon pressing the button, you will see a progress dialog box in action and show the text update on your terminal screen.
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
Here you go!
package main
import (
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
"os"
"time"
)
var (
mainApp *widgets.QApplication
progressDialog *widgets.QProgressDialog
mainLayout *widgets.QWidget
)
func delaySecond(n time.Duration) {
time.Sleep(n * time.Second)
}
func populate() *widgets.QWidget {
mainLayout = widgets.NewQWidget(nil, 0)
button := widgets.NewQPushButton2("Show Progress Dialog", nil)
button.ConnectClicked(func(flag bool) {
jobs := 100
progressDialog := widgets.NewQProgressDialog(mainLayout, core.Qt__Widget)
progressDialog.SetWindowModality(core.Qt__NonModal)
progressDialog.SetWindowTitle("Progress bar progressing...")
progressDialog.SetMinimum(0)
progressDialog.SetMaximum(jobs)
progressDialog.Show()
for i := 1; i < jobs; i++ {
progressDialog.SetValue(i)
// see http://stackoverflow.com/questions/8399349/simple-example-using-qprogressdialog-any-ideas-why-this-doesnt-work-properly
// on the QApplication::processEvents()
// somehow ExcludeUserInputEvents will prevent us from clicking the Cancel button!
//core.QCoreApplication_ProcessEvents(core.QEventLoop__ExcludeUserInputEvents)
// without this line, the progress bar or UI elements will not be updated!!
core.QCoreApplication_ProcessEvents(core.QEventLoop__AllEvents)
// too fast! introduce some delay to let you see the progression
delaySecond(time.Duration(1))
if progressDialog.WasCanceled() {
fmt.Printf("\r")
fmt.Printf("\rAborted at %d/%d", i, jobs)
break
}
// too lazy to add text label in the widget
// so, we use https://www.socketloop.com/tutorials/golang-overwrite-previous-output-with-count-down-timer
fmt.Printf("\rProgressing %d/%d", progressDialog.Value(), jobs)
}
progressDialog.SetValue(jobs)
})
layout := widgets.NewQVBoxLayout()
layout.AddWidget(button, 0, core.Qt__AlignCenter)
mainLayout.SetLayout(layout)
return mainLayout
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
populate().Show()
mainApp.Exec()
}
References:
http://www.bogotobogo.com/Qt/Qt5QProgressDialogModalModelessQTimer.php
http://doc.qt.io/qt-5/qprogressdialog.html
http://stackoverflow.com/questions/25966730/how-can-i-add-a-progress-bar-in-splash-screen-pyqt4
See also : Golang : Qt splash screen with delay 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
+12.8k How to automatically restart your crashed Golang server
+6.7k Gogland : Where to put source code files in package directory for rookie
+8.8k Golang : Check a web page existence with HEAD request example
+4.2k PHP : See installed compiled-in-modules
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+12.1k Golang : Convert(cast) int to int64
+8.6k Golang : Validate IPv6 example
+6.4k Golang : Not able to grep log.Println() output
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+8.2k Golang : How to get garbage collection data?
+4.8k Linux : Disable and enable IPv4 forwarding
+23.7k Golang : Storing cookies in http.CookieJar example