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
+28.1k Golang : Change a file last modified date and time
+7.5k Golang : Trim everything onward after a word
+19.4k Golang : Archive directory with tar and gzip
+9.9k Golang : Compare files modify date example
+10.6k Golang : How to transmit update file to client by HTTP request example
+9.1k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+12.4k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+7.7k Swift : Convert (cast) String to Float
+20.3k Golang : Read directory content with os.Open
+36.3k Golang : Display float in 2 decimal points and rounding up or down
+14k Golang : Get uploaded file name or access uploaded files
+6.8k Restart Apache or Nginx web server without password prompt