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
+25.1k Golang : convert rune to integer value
+5.2k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+30.1k Golang : Generate random string
+6.2k Golang : Break string into a slice of characters example
+13.4k Golang : Set image canvas or background to transparent
+6.5k Golang : Humanize and Titleize functions
+13.6k Golang : Gin framework accept query string by post request example
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+17.3k Golang : delete and modify XML file content
+51.7k Golang : How to get time in milliseconds?
+16.3k Golang : Delete files by extension
+14.1k Golang : How to shuffle elements in array or slice?