Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. how to make a window in the middle of the screen?
Forum Updated to NodeBB v4.3 + New Features

how to make a window in the middle of the screen?

Scheduled Pinned Locked Moved Unsolved Qt 6
4 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aria_aa
    wrote on last edited by
    #1

    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 :
    Capture.PNG

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      And are you sure your resolution is 1920x1080? Use QScreen to get your correct resolution.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        And are you sure your resolution is 1920x1080? Use QScreen to get your correct resolution.

        A Offline
        A Offline
        aria_aa
        wrote on last edited by
        #3

        @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()
        

        Capture.PNG

        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()
        

        Capture.PNG

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          All I can say it works fine for me with Qt6.dev on Linux.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved