Results 1 to 7 of 7

Thread: How to get QMainWindow position

  1. #1
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default How to get QMainWindow position

    Hello
    I am new to Qt
    I have written a simple application and I want to get current position of the QMainWindow.
    this is my code:

    Qt Code:
    1. M::M(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::M)
    4. {
    5. ui->setupUi(this);
    6. msg.setText(QString::number("what returns the X or Y of the window?"));
    7. msg.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I searched a lot. There where topics about setting the position, not getting it.
    I also tried geometry().x(), but it always returns 0 !!!
    Please help me. I need this.
    Best regards...

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get QMainWindow position

    QWidget::mapToGlobal()
    Thanks :-)

  3. The following user says thank you to prasad_N for this useful post:

    sh_erfan (15th August 2015)

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

    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

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

    sh_erfan (15th August 2015)

  6. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get QMainWindow position

    You could also override showEvent(), which is called only once when the window is first shown (and in the final position, unlike moveEvent()). If you need to know where the window is at all times, then you'll need moveEvent() as well, but only need to remember the position at the last time it is called. If the window is moved manually by the user, there will be a moveEvent for every small change of the position.

  7. The following user says thank you to d_stranz for this useful post:

    sh_erfan (15th August 2015)

  8. #5
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get QMainWindow position

    @d_stranz: Overriding showEvent() was my first attempt, but it did not show the expected result. Adding showEvent() override, I get the following output when running the application:
    moveEvent: x = 0, y = 0, width = 400, height = 300
    showEvent: x = 0, y = 0, width = 400, height = 300
    moveEvent: x = 404, y = 23, width = 400, height = 300
    However, if I explicitly set the window position in the CTOR by
    Qt Code:
    1. QRect r = geometry();
    2. r.setX(100); r.setY(100);
    3. setGeometry(r);
    To copy to clipboard, switch view to plain text mode 
    the output changes to
    moveEvent: x = 100, y = 100, width = 300, height = 200
    showEvent: x = 100, y = 100, width = 300, height = 200
    For me it looks like the last moveEvent in the first example gets triggered by the window manager, which moves the window to a position where it is visible including the title bar.

    Moving the window around gives me a moveEvent notification when I drop the window at the final position. I only get one additional moveEvent.

    The testing above was done using Qt 5.4.1 on OpenSuse 13.2.

    Next I ran the same program under Windows 7 with Qt 4.7. Here things change. Without explicit setting of geometry I get a single moveEvent and a single showEvent call both showing the same window position. When I now move the window around with the mouse, I get moveEvent updates on every small change of position.

    Best regards
    ars
    Last edited by ars; 15th August 2015 at 09:28. Reason: reformatted to look better

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

    sh_erfan (15th August 2015)

  10. #6
    Join Date
    Aug 2015
    Posts
    8
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get QMainWindow position

    Honestly I didn't get how to achieve this using QWidget::mapToGlobal(QPoint). I think this can be very straight forward but I would be glad if you say a short example with it.

    Quote Originally Posted by prasad_N View Post
    QWidget::mapToGlobal()
    Honestly I didn't get how to achieve this using QWidget::mapToGlobal(QPoint). I think this can be very straight forward but I would be glad if you say a short example with it.


    Added after 9 minutes:


    Thanks to all
    I used all replies to find the solution.
    actually I want to read position at start time from file, If file not exist, I set it to an arbitrary position like (50,50). When closing, I write current position to the file to get it next time and show the window in prev position. I use closeEvent to achieve this. The reason I get the correct position is that setGeometry() is called at start up, even if the file not exist.
    this is the code:

    Qt Code:
    1. M::M(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::M)
    4. {
    5. ui->setupUi(this);
    6.  
    7. if(QFile::exists("c.ccf"))
    8. {
    9. QFile f("c.ccf");
    10. f.open(QIODevice::ReadOnly);
    11. QString str = f.readAll();
    12. f.close();
    13.  
    14. QStringList lst = str.split(",");
    15. int x = QString(lst.at(0)).toInt(), y = QString(lst.at(1)).toInt();
    16.  
    17. QRect r = geometry();
    18. r.setX(x);
    19. r.setY(y);
    20. setGeometry(r);
    21. }
    22. else
    23. {
    24. QFile f("c.ccf");
    25. f.open(QIODevice::WriteOnly);
    26. f.close();
    27.  
    28. QRect r = geometry();
    29. r.setX(50); r.setY(50);
    30. setGeometry(r);
    31. }
    32. }
    33.  
    34. void M::closeEvent(QCloseEvent *e)
    35. {
    36. QRect r = geometry();
    37.  
    38. QFile f("c.ccf");
    39. f.open(QIODevice::WriteOnly);
    40. QTextStream out(&f);
    41. out << QString::number(r.x()) + "," + QString::number(r.y()) << endl;
    42. f.close();
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sh_erfan; 15th August 2015 at 15:39.

  11. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get QMainWindow position

    Quote Originally Posted by sh_erfan View Post
    Honestly I didn't get how to achieve this using QWidget::mapToGlobal(QPoint). I think this can be very straight forward but I would be glad if you say a short example with it.
    [/CODE]
    Sorry I misunderstand your question, i thought you are asking for Screen positions w.r.t window.


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6.  
    7. qDebug() << "Pos = " << w.mapToGlobal(w.pos()); // will give you screen position w.r.t main window.
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.