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
+10k Golang : How to tokenize source code with text/scanner package?
+7.1k Golang : Check if one string(rune) is permutation of another string(rune)
+51.8k Golang : How to get time in milliseconds?
+21.5k Golang : GORM create record or insert new record into database example
+16.4k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+5.2k Javascript : Change page title to get viewer attention
+27.3k Golang : dial tcp: too many colons in address
+19.7k Golang : Append content to a file
+5.3k Unix/Linux/MacOSx : How to remove an environment variable ?
+5.7k Linux : Disable and enable IPv4 forwarding
+12.2k Golang : Display list of countries and ISO codes
+8.4k PHP : How to parse ElasticSearch JSON ?