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
+10.1k Golang : cannot assign type int to value (type uint8) in range error
+6.6k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+13.5k Golang : Convert spaces to tabs and back to spaces example
+14.2k Golang : Parsing or breaking down URL
+5k Golang : Customize scanner.Scanner to treat dash as part of identifier
+11.2k Android Studio : Create custom icons for your application example
+8k Golang : HttpRouter multiplexer routing example
+15.7k Golang : Update database with GORM example
+5.8k AWS S3 : Prevent Hotlinking policy
+7.6k Golang : Reverse a string with unicode
+5.2k Golang : Intercept, inject and replay HTTP traffics from web server
+9.2k Facebook : Getting the friends list with PHP return JSON format