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
+10.2k Golang : Display a text file line by line with line number example
+4.3k Golang : Return multiple values from function
+15.1k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+4.7k Golang : Use NLP to get sentences for each paragraph example
+15k Golang :Trim white spaces from a string
+7.8k Golang : automatically figure out array length(size) with three dots
+9.1k Golang : cannot assign type int to value (type uint8) in range error
+6.5k Gogland : Single File versus Go Application Run Configurations
+4.1k Golang : Constant and variable names in native language
+7.2k Golang : How to check if input string is a word?
+10.5k Golang : Handle API query by curl with Gorilla Queries example
+5.3k Golang : Convert an executable file into []byte example