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
+5.1k Golang : Get FX sentiment from website example
+25k Golang : Get current file path of a file or executable
+6.8k Golang : constant 20013 overflows byte error message
+10k Golang : How to check if a website is served via HTTPS
+22.3k Golang : Set and Get HTTP request headers example
+16.5k Golang : read gzipped http response
+28.7k Golang : Get first few and last few characters from string
+12.4k Golang : Remove or trim extra comma from CSV
+5.7k Facebook : How to force facebook to scrape latest URL link data?
+12.6k Golang : Convert int(year) to time.Time type
+5.8k Golang : How to verify input is rune?