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
+5.7k Golang : Find the shortest line of text example
+18.2k Golang : Convert seconds to human readable time format example
+15k Golang : Execute terminal command to remote machine example
+7.6k Golang : Progress bar with ∎ character
+7.9k Golang : How to use Gorilla webtoolkit context package properly
+23.7k Golang : Get current file path of a file or executable
+18.2k Golang : Delete item from slice based on index/key position
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+9.1k Golang : Wait and sync.WaitGroup example
+7.6k Golang : Set or add headers for many or different handlers
+3.7k Golang : How to pass data between controllers with JSON Web Token
+9.9k Android Studio : Create custom icons for your application example