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.3k Golang : Add ASCII art to command line application launching process
+9.3k Golang : Read file with ioutil
+15k Golang : Find location by IP address and display with Google Map
+22.2k Golang : Set and Get HTTP request headers example
+36.2k Golang : Display float in 2 decimal points and rounding up or down
+8.4k Golang : Set or add headers for many or different handlers
+7.2k Golang : Convert source code to assembly language
+19.2k Golang : How to Set or Add Header http.ResponseWriter?
+8.1k Your page has meta tags in the body instead of the head
+5.6k Facebook : How to force facebook to scrape latest URL link data?
+15.2k Golang : rune literal not terminated error
+5.8k Golang : Compound interest over time example