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
+13.1k Golang : How to get a user home directory path?
+6k PHP : Get client IP address
+11.3k Golang : Characters limiter example
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+7k Restart Apache or Nginx web server without password prompt
+16.9k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+13.9k Golang : How to check if a file is hidden?
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+16.3k Golang :Trim white spaces from a string
+6.9k Golang : How to call function inside template with template.FuncMap
+5.6k Swift : Get substring with rangeOfString() function example
+28.1k Golang : Connect to database (MySQL/MariaDB) server