how to make a window in the middle of the screen?
Unsolved
Qt 6
-
Hi everyone.
I want to make my window in the middle of the screen with half of the screen size, but it acts a little weird for me.
I don't know where is problem.
The code:#include <QApplication> #include <QtWidgets> int main(int argc, char *argv[]){ int screen_width = 1920, screen_height = 1080; int window_width = screen_width/2, window_height = screen_height/2; int wtp = (screen_width-window_width)/2; //width of top-left point of the window int htp = (screen_height-window_height)/2; //height of top-left point of the window int wbp = window_width; //width of bottom-right point of the window int hbp = window_height; //height of bottom-right point of the window QApplication app (argc, argv); QWidget window; window.setWindowTitle("Main Window"); window.setGeometry(wtp, htp, wbp, hbp); window.show(); return app.exec(); }
The location and size of the window :
-
And are you sure your resolution is 1920x1080? Use QScreen to get your correct resolution.
-
@Christian-Ehrlicher said in how to make a window in the middle of the screen?:
And are you sure your resolution is 1920x1080? Use QScreen to get your correct resolution.
Yes, I'm totally sure about the screen resolution.
and I checked it now with python it works correctly with Qt5 but with Qt6 there is the same problem.the code:
import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow #from PyQt6 import QtWidgets #from PyQt6.QtWidgets import QApplication, QMainWindow class window(QMainWindow): def __init__(self): super(window, self).__init__() self.screen_width = 1920 self.screen_height = 1080 self.window_width = self.screen_width / 2 self.window_height = self.screen_height / 2 self.wtp = (self.screen_width-self.window_width)/2; #width of top-left point of the window self.htp = (self.screen_height-self.window_height)/2; #height of top-left point of the window self.wbp = self.window_width; #width of bottom-right point of the window self.hbp = self.window_height; #height of bottom-right point of the window self.setGeometry(self.wtp, self.htp, self.wbp, self.hbp) def My_app(): app = QApplication(sys.argv) win = window() win.show() sys.exit(app.exec()) if __name__ == '__main__': My_app()
import sys #from PyQt5 import QtWidgets #from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt6 import QtWidgets from PyQt6.QtWidgets import QApplication, QMainWindow class window(QMainWindow): def __init__(self): super(window, self).__init__() self.screen_width = 1920 self.screen_height = 1080 self.window_width = self.screen_width / 2 self.window_height = self.screen_height / 2 self.wtp = (self.screen_width-self.window_width)/2; #width of top-left point of the window self.htp = (self.screen_height-self.window_height)/2; #height of top-left point of the window self.wbp = self.window_width; #width of bottom-right point of the window self.hbp = self.window_height; #height of bottom-right point of the window self.setGeometry(self.wtp, self.htp, self.wbp, self.hbp) def My_app(): app = QApplication(sys.argv) win = window() win.show() sys.exit(app.exec()) if __name__ == '__main__': My_app()
-
All I can say it works fine for me with Qt6.dev on Linux.