QWidget::mapToGlobal()
QWidget::mapToGlobal()
Thanks :-)
sh_erfan (15th August 2015)
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:Running the application produces the following output:Qt Code:
{ std::cout << "moveEvent: "; std::cout << "x = " << r.x() << ", y = " << r.y() << ", width = " << r.width() << ", height = " << r.height() << std::endl; }To copy to clipboard, switch view to plain text mode
The line with CTOR shows the coordinates received when calling geometry in the contructor. Note that there are two calls to moveEvent().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
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
sh_erfan (15th August 2015)
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.
sh_erfan (15th August 2015)
@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:
However, if I explicitly set the window position in the CTOR bymoveEvent: 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
the output changes toQt Code:
r.setX(100); r.setY(100); setGeometry(r);To copy to clipboard, switch view to plain text mode
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.moveEvent: x = 100, y = 100, width = 300, height = 200
showEvent: x = 100, y = 100, width = 300, height = 200
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
sh_erfan (15th August 2015)
Bookmarks