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
+11.4k Golang : Gorilla web tool kit secure cookie example
+19.4k Golang : How to get own program name during runtime ?
+11k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+8.4k Golang : Combine slices but preserve order example
+6.3k Golang : Skip or discard items of non-interest when iterating example
+6k Golang : Break string into a slice of characters example
+18.6k Golang : Populate dropdown with html/template example
+9.3k Javascript : Read/parse JSON data from HTTP response
+16.3k Golang : Set up source IP address before making HTTP request
+12.8k Golang : Date and Time formatting
+5.4k Golang : Error handling methods