Golang : Qt get screen resolution and display on center example
Problem:
You need to start your Qt desktop application main window on the screen center and also need to detect the screen's resolution in order to compute the X and Y co-ordinates for moving your application to the screen center. How to do that?
Solution:
In order to detect the screen resolution, use the QApplication.Desktop()
function and its ScreenGeometry()
method. It will return the default screen's (if specified to 0) width and height.
To calculate the screen center's X and Y co-ordinates, you can divide the width and height by 2 or use second method which is the Center()
function.
Below are two small programs that demonstrate the first and second ways.
Here you go!
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)
button := widgets.NewQPushButton2("Bye!", nil)
button.ConnectClicked(func(flag bool) {
mainApp.Quit()
})
layout := widgets.NewQVBoxLayout()
layout.AddWidget(button, 0, core.Qt__AlignCenter)
mainLayout.SetLayout(layout)
return mainLayout
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
// get screen resolution(heights and width)
// 0 for default screen
screenRect := mainApp.Desktop().ScreenGeometry(0)
fmt.Println("Detected screen Height : ", screenRect.Height())
fmt.Println("Detected screen Width : ", screenRect.Width())
p := populate()
// first way to display on screen center
screenWidthCenter := screenRect.Width() / 2
screenHeightCenter := screenRect.Height() / 2
p.Move2(screenWidthCenter, screenHeightCenter)
p.Show()
mainApp.Exec()
}
and second way with slight modification. Both programs will show the same result.
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)
button := widgets.NewQPushButton2("Bye!", nil)
button.ConnectClicked(func(flag bool) {
mainApp.Quit()
})
layout := widgets.NewQVBoxLayout()
layout.AddWidget(button, 0, core.Qt__AlignCenter)
mainLayout.SetLayout(layout)
return mainLayout
}
func main() {
mainApp = widgets.NewQApplication(len(os.Args), os.Args)
// get screen resolution(heights and width)
// 0 for default screen
screenRect := mainApp.Desktop().ScreenGeometry(0)
fmt.Println("Detected screen Height : ", screenRect.Height())
fmt.Println("Detected screen Width : ", screenRect.Width())
p := populate()
// second way to display on screen center
centerPoint := screenRect.Center()
p.Move2(centerPoint.X(), centerPoint.Y())
p.Show()
mainApp.Exec()
}
Please follow up the setup instruction at https://github.com/therecipe/qt to setup Qt if you haven't done so.
Build and run both programs above and it will show the main application window starting in the middle of your screen.
Happy Qt-ing!
References:
http://www.qtcentre.org/threads/43158-How-to-center-main-window-on-screen
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
Tutorials
+10.2k Golang : How to profile or log time spend on execution?
+14.5k Golang : How to get URL port?
+10.9k Golang : Get UDP client IP address and differentiate clients by port number
+9.4k Golang : Create unique title slugs example
+6.5k Golang : Convert an executable file into []byte example
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+19.5k Golang : Example for DSA(Digital Signature Algorithm) package functions
+9.2k Golang : Write multiple lines or divide string into multiple lines
+11.1k Golang : Web routing/multiplex example
+17.6k Golang : [json: cannot unmarshal object into Go value of type]
+11.7k Golang : Calculations using complex numbers example
+6.7k Golang : Derive cryptographic key from passwords with Argon2