Results 1 to 7 of 7

Thread: How to get QMainWindow position

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 42 Times in 41 Posts

    Default Re: How to get QMainWindow position

    Hello,

    you ask too early for the window position (in the CTOR). Try overriding QMainWindow::moveEvent() and print the result of geometry() inside this function. You observe that moveEvent() gets called multiple times even without manually moving the window around.

    Here is a code snippet:
    Qt Code:
    1. void MainWindow::moveEvent(QMoveEvent *e)
    2. {
    3. QMainWindow::moveEvent(e);
    4. QRect r = geometry();
    5. std::cout << "moveEvent: ";
    6. std::cout << "x = " << r.x() << ", y = " << r.y() << ", width = " << r.width() << ", height = " << r.height() << std::endl;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Running the application produces the following output:
    CTOR: x = 0, y = 0, width = 400, height = 300
    moveEvent: x = 0, y = 0, width = 400, height = 300
    moveEvent: x = 808, y = 23, width = 400, height = 300
    The line with CTOR shows the coordinates received when calling geometry in the contructor. Note that there are two calls to moveEvent().

    You may also look at frameGeometry() and pos() methods. See http://doc.qt.io/qt-5.4/application-...indow-geometry for details.

    Best regards
    ars
    Last edited by ars; 14th August 2015 at 21:54. Reason: updated contents

  2. The following user says thank you to ars for this useful post:

    sh_erfan (15th August 2015)

Similar Threads

  1. how to change the position of menu of QMainWindow
    By yxmaomao in forum Qt Programming
    Replies: 11
    Last Post: 17th September 2016, 17:12
  2. Change Start position of Drawer in RightArea of QMainWindow
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2014, 22:27
  3. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 05:46
  4. Replies: 3
    Last Post: 13th November 2011, 09:12
  5. Replies: 2
    Last Post: 7th September 2011, 18:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.