Golang : Qt Yes No and Quit message box example
A quick tutorial on how to create simple dialog box and message box with Yes, No and Quit buttons. Pretty useful in asking user for confirmation.
Here you go!
Example 1:
package main
import (
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
"os"
)
var (
mainApp *widgets.QApplication
mainLayout *widgets.QWidget
)
func populate() *widgets.QWidget {
mainLayout = widgets.NewQWidget(nil, 0)
yesButton := widgets.NewQPushButton2("Yes", nil)
yesButton.ConnectClicked(func(flag bool) {
fmt.Println("Yes!")
})
noButton := widgets.NewQPushButton2("No", nil)
noButton.ConnectClicked(func(flag bool) {
fmt.Println("No!")
})
quitButton := widgets.NewQPushButton2("Quit", nil)
quitButton.ConnectClicked(func(flag bool) {
mainApp.Quit()
})
layout := widgets.NewQVBoxLayout()
layout.AddWidget(yesButton, 0, core.Qt__AlignLeft)
layout.AddWidget(noButton, 0, core.Qt__AlignLeft)
layout.AddWidget(quitButton, 0, core.Qt__AlignRight)
mainLayout.SetLayout(layout)
return mainLayout
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
populate().Show()
mainApp.Exec()
}
and example 2 is with QMessageBox
and QMessage__StandardButton
package main
import (
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
"os"
)
var (
mainApp *widgets.QApplication
mainLayout *widgets.QWidget
replyBox *widgets.QMessageBox
replyButtons *widgets.QMessageBox__StandardButton
)
func populate() *widgets.QWidget {
mainLayout = widgets.NewQWidget(nil, 0)
replyBox := widgets.NewQMessageBox(nil)
replyButtons := replyBox.Question(nil, "Test", "Quit", widgets.QMessageBox__Yes, widgets.QMessageBox__No)
if replyButtons == widgets.QMessageBox__Yes {
fmt.Println("Yes! Quit")
// mainApp.Quit() -- strange won't work!
os.Exit(0)
} else {
fmt.Println("No!")
}
layout := widgets.NewQVBoxLayout()
layout.AddWidget(replyBox, 0, core.Qt__AlignLeft)
mainLayout.SetLayout(layout)
return mainLayout
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
populate().Show()
mainApp.Exec()
}
References:
https://socketloop.com/tutorials/golang-how-to-stop-user-from-directly-running-an-executable-file
https://stackoverflow.com/questions/13111669/yes-no-message-box-using-qmessagebox
See also : Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
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 uint value to string type
+14.3k Golang : How to check if your program is running in a terminal
+6.6k Golang : Humanize and Titleize functions
+21k Golang : Clean up null characters from input data
+11.2k Golang : How to flush a channel before the end of program?
+6.7k Mac/Linux/Windows : Get CPU information from command line
+4.8k Nginx and PageSpeed build from source CentOS example
+18.4k Golang : Write file with io.WriteString
+5.3k Golang *File points to a file or directory ?
+5.4k PHP : Fix Call to undefined function curl_init() error
+6.9k Golang : Takes a plural word and makes it singular
+8.6k Golang : On lambda, anonymous, inline functions and function literals