Golang : Qt image viewer example
This is the Qt version of the previous tutorial on how to create a simple image viewer with Go-GTK. To create this Qt image viewer program, we will use the Go-Qt binding package
Please follow up the setup instruction at https://github.com/therecipe/qt to setup Qt.
What this program does is to load the given image file and display it. It will be able to detect animated GIF file and use the proper Qt functions such as QLabel
and QMovie
to display the animated GIF. There is also a button to quit the image viewer application properly.
Here you go!
package main
import (
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
"os"
)
var (
displayArea *widgets.QWidget
scene *widgets.QGraphicsScene
view *widgets.QGraphicsView
item *widgets.QGraphicsPixmapItem
mainApp *widgets.QApplication
imageFileName string
)
func imageViewer() *widgets.QWidget {
displayArea = widgets.NewQWidget(nil, 0)
scene = widgets.NewQGraphicsScene(nil)
view = widgets.NewQGraphicsView(nil)
var imageReader = gui.NewQImageReader3(imageFileName, "")
// test to see if we are dealing with animated GIF
fmt.Println("Animated GIF : ", imageReader.SupportsAnimation())
if imageReader.SupportsAnimation() {
// instead of reading from file(disk) again, we take from memory
// HOWEVER, this will cause segmentation violation error ! :(
//var movie = gui.NewQMovieFromPointer(imageReader.Pointer())
var movie = gui.NewQMovie3(imageFileName, "", nil)
// see http://stackoverflow.com/questions/5769766/qt-how-to-show-gifanimated-image-in-qgraphicspixmapitem
var movieLabel = widgets.NewQLabel(nil, core.Qt__Widget)
movieLabel.SetMovie(movie)
movie.Start()
scene.AddWidget(movieLabel, core.Qt__Widget)
} else {
var pixmap = gui.NewQPixmap5(imageFileName, "", core.Qt__AutoColor)
item = widgets.NewQGraphicsPixmapItem2(pixmap, nil)
scene.AddItem(item)
}
view.SetScene(scene)
//create a button and connect the clicked signal
var button = widgets.NewQPushButton2("Quit", nil)
button.ConnectClicked(func(flag bool) {
//os.Exit(0)
widgets.QApplication_Beep()
widgets.QMessageBox_Information(nil, "OK", "You clicked quit button!", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
// errmm... proper way to quit Qt application
// https://godoc.org/github.com/therecipe/qt/widgets#QApplication.Quit
mainApp.Quit()
})
var layout = widgets.NewQVBoxLayout()
layout.AddWidget(view, 0, core.Qt__AlignCenter)
layout.AddWidget(button, 0, core.Qt__AlignCenter)
displayArea.SetLayout(layout)
return displayArea
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <image file>\n", os.Args[0])
os.Exit(0)
}
imageFileName = os.Args[1]
fmt.Println("Loading image : ", imageFileName)
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
imageViewer().Show()
widgets.QApplication_Exec()
}
To test this program, build and run this program from the command line. Supply it with animated and non-animated images.
Happy coding!
References:
https://godoc.org/github.com/therecipe/qt/widgets
https://godoc.org/github.com/therecipe/qt/core
https://godoc.org/github.com/therecipe/qt/gui
https://godoc.org/github.com/therecipe/qt/widgets#QApplication.Quit
https://socketloop.com/tutorials/golang-simple-image-viewer-with-go-gtk
See also : Golang : Simple image viewer with Go-GTK
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
+9k Golang : does not implement flag.Value (missing Set method)
+6.8k Golang : How to call function inside template with template.FuncMap
+13.2k Golang : Read from buffered reader until specific number of bytes
+14k Android Studio : Use image as AlertDialog title with custom layout example
+12.8k Golang : List objects in AWS S3 bucket
+13.5k Golang : Check if an integer is negative or positive
+5.4k Golang : Detect words using using consecutive letters in a given string
+6.4k Elasticsearch : Shutdown a local node
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+12.2k Golang : Forwarding a local port to a remote server example
+33.7k Golang : Create x509 certificate, private and public keys
+13.9k Elastic Search : Mapping date format and sort by date