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
+21k Golang : Create and resolve(read) symbolic links
+18.9k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+9.1k Golang : How to check if a string with spaces in between is numeric?
+9.3k Golang : Qt Yes No and Quit message box example
+13.2k Golang : Verify token from Google Authenticator App
+6.8k Golang : Decode XML data from RSS feed
+9.2k Golang : Play .WAV file from command line
+5.3k Golang : If else example and common mistake
+13.4k Golang : Set image canvas or background to transparent
+16.5k Golang : Read integer from file into array
+20.6k PHP : Convert(cast) int to double/float
+8.7k Golang : Get final balance from bit coin address example