Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image




In the previous tutorial on how to create an image viewer with Qt example, a reader is wanted to know how to remove the horizontal and vertical scroll bars.

In Qt application, if the displayed image is large, the application will automatically resize the image and set up scroll bars along the right and bottom axis. To disable scroll bars, simply set the policies to Qt__ScrollBarAlwaysOff

Add these lines starting from line 53, before view.SetScene(scene)

 view.SetHorizontalScrollBarPolicy(core.Qt__ScrollBarAlwaysOff)
 view.SetVerticalScrollBarPolicy(core.Qt__ScrollBarAlwaysOff)
 view.SetEnabled(false) // disable interaction, no drag and move by mouse
 view.SetScene(scene)

If you're planning to disallow the user from dragging and moving the image. Simply use SetEnabled(false) to render any attempts to interact with the image futile.

Apart from ScrollBarAlwaysOff policy, there are also

 Qt__ScrollBarAsNeeded  = appear as needed
 Qt__ScrollBarAlwaysOn  = always appear along the axis

The full source code. 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
 scrollArea *widgets.QAbstractScrollArea
 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.SetHorizontalScrollBarPolicy(core.Qt__ScrollBarAlwaysOff)
 view.SetVerticalScrollBarPolicy(core.Qt__ScrollBarAlwaysOff)
 view.SetEnabled(false) // disable interaction, no drag and move by mouse
 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)

 // extract the screen resolution with ScreenGeometry function
 qrect := mainApp.Desktop().ScreenGeometry(0)

 // first way to get screen center
 // is to get the middle point of height and width
 screenHeight := qrect.Height()
 screenWidth := qrect.Width()
 fmt.Println("Screen Height : ", screenHeight)
 fmt.Println("Screen Width : ", screenWidth)

 fmt.Println("Screen height middle point : ", screenHeight/2)
 fmt.Println("Screen width middle point : ", screenWidth/2)

 // second way to get center screen
 // is to use the Center() function
 //centerPoint := qrect.Center()

 // Remember X = width, Y = height
 // startingPoint := core.NewQPoint2(screenWidth/2, (screenHeight/2)+300)
 //startingPoint := core.NewQPoint2(centerPoint.X(), centerPoint.Y())

 // move to screen center before showing
 // imageViewer().Move(startingPoint)
 imageViewer().Move2(0, 0)

 imageViewer().Show()

 widgets.QApplication_Exec()
 }

References:

https://forum.qt.io/topic/23415/scrollbar/2

  See also : Golang : Qt image viewer example





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