How to get the screen position of `QMainWindow` and print it?











up vote
0
down vote

favorite












I'm trying to get the screen position of QMainWindow and print the position (x,y) values. I have tried both self.pos() and self.mapToGlobal(self.pos()) and both of these return 0.



import sys
from PyQt5.QtWidgets import QApplication, QMainWindow


class MainWindow(QMainWindow):

def __init__(self):
QMainWindow.__init__(self)
self.resize(400, 200)

# PRINTS 0 0
print(self.pos().x(), self.pos().y())

# PRINTS 0 0
print(self.mapToGlobal(self.pos()).x(), self.mapToGlobal(self.pos()).y())


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())


I'm using Python 3.7 and PyQt 5.11, how can I achieve this?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to get the screen position of QMainWindow and print the position (x,y) values. I have tried both self.pos() and self.mapToGlobal(self.pos()) and both of these return 0.



    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow


    class MainWindow(QMainWindow):

    def __init__(self):
    QMainWindow.__init__(self)
    self.resize(400, 200)

    # PRINTS 0 0
    print(self.pos().x(), self.pos().y())

    # PRINTS 0 0
    print(self.mapToGlobal(self.pos()).x(), self.mapToGlobal(self.pos()).y())


    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


    I'm using Python 3.7 and PyQt 5.11, how can I achieve this?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to get the screen position of QMainWindow and print the position (x,y) values. I have tried both self.pos() and self.mapToGlobal(self.pos()) and both of these return 0.



      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow


      class MainWindow(QMainWindow):

      def __init__(self):
      QMainWindow.__init__(self)
      self.resize(400, 200)

      # PRINTS 0 0
      print(self.pos().x(), self.pos().y())

      # PRINTS 0 0
      print(self.mapToGlobal(self.pos()).x(), self.mapToGlobal(self.pos()).y())


      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      sys.exit(app.exec_())


      I'm using Python 3.7 and PyQt 5.11, how can I achieve this?










      share|improve this question













      I'm trying to get the screen position of QMainWindow and print the position (x,y) values. I have tried both self.pos() and self.mapToGlobal(self.pos()) and both of these return 0.



      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow


      class MainWindow(QMainWindow):

      def __init__(self):
      QMainWindow.__init__(self)
      self.resize(400, 200)

      # PRINTS 0 0
      print(self.pos().x(), self.pos().y())

      # PRINTS 0 0
      print(self.mapToGlobal(self.pos()).x(), self.mapToGlobal(self.pos()).y())


      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      sys.exit(app.exec_())


      I'm using Python 3.7 and PyQt 5.11, how can I achieve this?







      python pyqt position coordinates qmainwindow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 23:02









      artomason

      457312




      457312
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The position of a widget is with respect to the parent if it has it, and if it does not have it is with respect to the screen, so in the case of MainWindow, since it is a window, pos() should be used, if it were a widget that has a parent you must use self.mapToGlobal(QtCore.QPoint(0, 0)) since it is the top-left position.



          On the other hand the initial position of every widget is QPoint(0, 0), and if it is a window the OS manipulates its position and moves it, so you get the value of (0, 0), so in your case you must track the change of position, for example using moveEvent:



          import sys
          from PyQt5 import QtCore, QtWidgets

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self):
          super(MainWindow, self).__init__()
          self.resize(400, 200)

          def moveEvent(self, e):
          print(self.pos())
          super(MainWindow, self).moveEvent(e)

          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          window = MainWindow()
          window.show()
          sys.exit(app.exec_())





          share|improve this answer





















          • Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
            – artomason
            Nov 10 at 6:20


















          up vote
          1
          down vote













          I'll add the link http://doc.qt.io/qt-5/application-windows.html#window-geometry and an example:



          import sys
          from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
          QVBoxLayout, QTextEdit, QPushButton)


          class MainWindow(QMainWindow):

          def __init__(self):
          QMainWindow.__init__(self)
          self.resize(400, 200)


          centralWidget = QWidget()
          self.setCentralWidget(centralWidget)

          self.textEdit = QTextEdit()
          self.btn = QPushButton("get the screen position of `QMainWindow`")
          self.btn.clicked.connect(self.btnClicked)

          layoutV = QVBoxLayout(centralWidget)
          layoutV.addWidget(self.textEdit)
          layoutV.addWidget(self.btn)

          self.textEdit.append("Start:")
          self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
          "".format(self.pos().x(), self.pos().y()))
          self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
          "".format(self.geometry().x(), self.geometry().y()))
          self.textEdit.append("--------------------------------------")

          def btnClicked(self):
          self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
          "".format(self.pos().x(), self.pos().y()))
          self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
          "".format(self.geometry().x(), self.geometry().y()))



          def moveEvent(self, event): # QMoveEvent
          print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
          super(MainWindow, self).moveEvent(event)

          def resizeEvent(self, event): # QResizeEvent
          print("w=`{}`, h=`{}`".format(event.size().width(), event.size().height()))
          super(MainWindow, self).resizeEvent(event)


          app = QApplication(sys.argv)
          window = MainWindow()
          window.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234360%2fhow-to-get-the-screen-position-of-qmainwindow-and-print-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            The position of a widget is with respect to the parent if it has it, and if it does not have it is with respect to the screen, so in the case of MainWindow, since it is a window, pos() should be used, if it were a widget that has a parent you must use self.mapToGlobal(QtCore.QPoint(0, 0)) since it is the top-left position.



            On the other hand the initial position of every widget is QPoint(0, 0), and if it is a window the OS manipulates its position and moves it, so you get the value of (0, 0), so in your case you must track the change of position, for example using moveEvent:



            import sys
            from PyQt5 import QtCore, QtWidgets

            class MainWindow(QtWidgets.QMainWindow):
            def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(400, 200)

            def moveEvent(self, e):
            print(self.pos())
            super(MainWindow, self).moveEvent(e)

            if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec_())





            share|improve this answer





















            • Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
              – artomason
              Nov 10 at 6:20















            up vote
            1
            down vote



            accepted










            The position of a widget is with respect to the parent if it has it, and if it does not have it is with respect to the screen, so in the case of MainWindow, since it is a window, pos() should be used, if it were a widget that has a parent you must use self.mapToGlobal(QtCore.QPoint(0, 0)) since it is the top-left position.



            On the other hand the initial position of every widget is QPoint(0, 0), and if it is a window the OS manipulates its position and moves it, so you get the value of (0, 0), so in your case you must track the change of position, for example using moveEvent:



            import sys
            from PyQt5 import QtCore, QtWidgets

            class MainWindow(QtWidgets.QMainWindow):
            def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(400, 200)

            def moveEvent(self, e):
            print(self.pos())
            super(MainWindow, self).moveEvent(e)

            if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec_())





            share|improve this answer





















            • Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
              – artomason
              Nov 10 at 6:20













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            The position of a widget is with respect to the parent if it has it, and if it does not have it is with respect to the screen, so in the case of MainWindow, since it is a window, pos() should be used, if it were a widget that has a parent you must use self.mapToGlobal(QtCore.QPoint(0, 0)) since it is the top-left position.



            On the other hand the initial position of every widget is QPoint(0, 0), and if it is a window the OS manipulates its position and moves it, so you get the value of (0, 0), so in your case you must track the change of position, for example using moveEvent:



            import sys
            from PyQt5 import QtCore, QtWidgets

            class MainWindow(QtWidgets.QMainWindow):
            def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(400, 200)

            def moveEvent(self, e):
            print(self.pos())
            super(MainWindow, self).moveEvent(e)

            if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec_())





            share|improve this answer












            The position of a widget is with respect to the parent if it has it, and if it does not have it is with respect to the screen, so in the case of MainWindow, since it is a window, pos() should be used, if it were a widget that has a parent you must use self.mapToGlobal(QtCore.QPoint(0, 0)) since it is the top-left position.



            On the other hand the initial position of every widget is QPoint(0, 0), and if it is a window the OS manipulates its position and moves it, so you get the value of (0, 0), so in your case you must track the change of position, for example using moveEvent:



            import sys
            from PyQt5 import QtCore, QtWidgets

            class MainWindow(QtWidgets.QMainWindow):
            def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(400, 200)

            def moveEvent(self, e):
            print(self.pos())
            super(MainWindow, self).moveEvent(e)

            if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec_())






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 0:11









            eyllanesc

            71.8k93054




            71.8k93054












            • Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
              – artomason
              Nov 10 at 6:20


















            • Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
              – artomason
              Nov 10 at 6:20
















            Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
            – artomason
            Nov 10 at 6:20




            Spent hours researching that and couldn't figure it out. Thank you for the clear explanations.
            – artomason
            Nov 10 at 6:20












            up vote
            1
            down vote













            I'll add the link http://doc.qt.io/qt-5/application-windows.html#window-geometry and an example:



            import sys
            from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
            QVBoxLayout, QTextEdit, QPushButton)


            class MainWindow(QMainWindow):

            def __init__(self):
            QMainWindow.__init__(self)
            self.resize(400, 200)


            centralWidget = QWidget()
            self.setCentralWidget(centralWidget)

            self.textEdit = QTextEdit()
            self.btn = QPushButton("get the screen position of `QMainWindow`")
            self.btn.clicked.connect(self.btnClicked)

            layoutV = QVBoxLayout(centralWidget)
            layoutV.addWidget(self.textEdit)
            layoutV.addWidget(self.btn)

            self.textEdit.append("Start:")
            self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
            "".format(self.pos().x(), self.pos().y()))
            self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
            "".format(self.geometry().x(), self.geometry().y()))
            self.textEdit.append("--------------------------------------")

            def btnClicked(self):
            self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
            "".format(self.pos().x(), self.pos().y()))
            self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
            "".format(self.geometry().x(), self.geometry().y()))



            def moveEvent(self, event): # QMoveEvent
            print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
            super(MainWindow, self).moveEvent(event)

            def resizeEvent(self, event): # QResizeEvent
            print("w=`{}`, h=`{}`".format(event.size().width(), event.size().height()))
            super(MainWindow, self).resizeEvent(event)


            app = QApplication(sys.argv)
            window = MainWindow()
            window.show()
            sys.exit(app.exec_())


            enter image description here






            share|improve this answer

























              up vote
              1
              down vote













              I'll add the link http://doc.qt.io/qt-5/application-windows.html#window-geometry and an example:



              import sys
              from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
              QVBoxLayout, QTextEdit, QPushButton)


              class MainWindow(QMainWindow):

              def __init__(self):
              QMainWindow.__init__(self)
              self.resize(400, 200)


              centralWidget = QWidget()
              self.setCentralWidget(centralWidget)

              self.textEdit = QTextEdit()
              self.btn = QPushButton("get the screen position of `QMainWindow`")
              self.btn.clicked.connect(self.btnClicked)

              layoutV = QVBoxLayout(centralWidget)
              layoutV.addWidget(self.textEdit)
              layoutV.addWidget(self.btn)

              self.textEdit.append("Start:")
              self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
              "".format(self.pos().x(), self.pos().y()))
              self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
              "".format(self.geometry().x(), self.geometry().y()))
              self.textEdit.append("--------------------------------------")

              def btnClicked(self):
              self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
              "".format(self.pos().x(), self.pos().y()))
              self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
              "".format(self.geometry().x(), self.geometry().y()))



              def moveEvent(self, event): # QMoveEvent
              print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
              super(MainWindow, self).moveEvent(event)

              def resizeEvent(self, event): # QResizeEvent
              print("w=`{}`, h=`{}`".format(event.size().width(), event.size().height()))
              super(MainWindow, self).resizeEvent(event)


              app = QApplication(sys.argv)
              window = MainWindow()
              window.show()
              sys.exit(app.exec_())


              enter image description here






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I'll add the link http://doc.qt.io/qt-5/application-windows.html#window-geometry and an example:



                import sys
                from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
                QVBoxLayout, QTextEdit, QPushButton)


                class MainWindow(QMainWindow):

                def __init__(self):
                QMainWindow.__init__(self)
                self.resize(400, 200)


                centralWidget = QWidget()
                self.setCentralWidget(centralWidget)

                self.textEdit = QTextEdit()
                self.btn = QPushButton("get the screen position of `QMainWindow`")
                self.btn.clicked.connect(self.btnClicked)

                layoutV = QVBoxLayout(centralWidget)
                layoutV.addWidget(self.textEdit)
                layoutV.addWidget(self.btn)

                self.textEdit.append("Start:")
                self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                "".format(self.pos().x(), self.pos().y()))
                self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                "".format(self.geometry().x(), self.geometry().y()))
                self.textEdit.append("--------------------------------------")

                def btnClicked(self):
                self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                "".format(self.pos().x(), self.pos().y()))
                self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                "".format(self.geometry().x(), self.geometry().y()))



                def moveEvent(self, event): # QMoveEvent
                print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
                super(MainWindow, self).moveEvent(event)

                def resizeEvent(self, event): # QResizeEvent
                print("w=`{}`, h=`{}`".format(event.size().width(), event.size().height()))
                super(MainWindow, self).resizeEvent(event)


                app = QApplication(sys.argv)
                window = MainWindow()
                window.show()
                sys.exit(app.exec_())


                enter image description here






                share|improve this answer












                I'll add the link http://doc.qt.io/qt-5/application-windows.html#window-geometry and an example:



                import sys
                from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
                QVBoxLayout, QTextEdit, QPushButton)


                class MainWindow(QMainWindow):

                def __init__(self):
                QMainWindow.__init__(self)
                self.resize(400, 200)


                centralWidget = QWidget()
                self.setCentralWidget(centralWidget)

                self.textEdit = QTextEdit()
                self.btn = QPushButton("get the screen position of `QMainWindow`")
                self.btn.clicked.connect(self.btnClicked)

                layoutV = QVBoxLayout(centralWidget)
                layoutV.addWidget(self.textEdit)
                layoutV.addWidget(self.btn)

                self.textEdit.append("Start:")
                self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                "".format(self.pos().x(), self.pos().y()))
                self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                "".format(self.geometry().x(), self.geometry().y()))
                self.textEdit.append("--------------------------------------")

                def btnClicked(self):
                self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                "".format(self.pos().x(), self.pos().y()))
                self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                "".format(self.geometry().x(), self.geometry().y()))



                def moveEvent(self, event): # QMoveEvent
                print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
                super(MainWindow, self).moveEvent(event)

                def resizeEvent(self, event): # QResizeEvent
                print("w=`{}`, h=`{}`".format(event.size().width(), event.size().height()))
                super(MainWindow, self).resizeEvent(event)


                app = QApplication(sys.argv)
                window = MainWindow()
                window.show()
                sys.exit(app.exec_())


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 0:42









                S. Nick

                2,201247




                2,201247






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234360%2fhow-to-get-the-screen-position-of-qmainwindow-and-print-it%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini