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
+19.4k Golang : How to run your code only once with sync.Once object
+9.2k Random number generation with crypto/rand in Go
+33.9k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+10.5k Golang : How to transmit update file to client by HTTP request example
+16.3k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+7.8k Golang : Configure Apache and NGINX to access your Go service example
+9.3k Golang : Turn string or text file into slice example
+9.1k Golang : Changing a RGBA image number of channels with OpenCV
+19k Golang : Example for DSA(Digital Signature Algorithm) package functions
+13.3k Golang : Tutorial on loading GOB and PEM files
+44k Golang : Use wildcard patterns with filepath.Glob() example
+6.4k Nginx : Password protect a directory/folder