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
+5.9k Golang : Create new color from command line parameters
+11.4k Golang : Secure file deletion with wipe example
+5.8k nginx : force all pages to be SSL
+18.8k Golang : Clearing slice
+12.8k Golang : Calculate elapsed years or months since a date
+6.1k Golang : Selection sort example
+6.2k PHP : Proper way to get UTF-8 character or string length
+23.6k Golang : Use regular expression to validate domain name
+8.2k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+12.1k Golang : Extract part of string with regular expression
+12.6k Golang : Convert IPv4 address to packed 32-bit binary format
+14.2k Golang : Find network of an IP address