Important: Please read the Qt Code of Conduct -

Strange initial dialogue location?

  • While testing the initial stages of converting a window from MFC to Qt, I discovered that the initial global location for my About dialogue that was invoked from that window was really "strange". I had deleted the saved geometry for About, and the dialogue was displayed very near the top left of the screen. I added code see where Qt was putting it at the time it was about to show it (ShowEvent):

    QByteArray ba = settings.value("Dialogs/About/geometry").toByteArray();
    if (!ba.isEmpty())
    	QPoint point = mapToGlobal(QPoint(0, 0)); 
             etc ..

    This gave me x = -129, y = 72 which strikes as a very strange location.

    Is this normal behaviour?

  • Lifetime Qt Champion


    Can you show how you store this information as well ?

  • Huh - I didn't store the QPoint value - I looked at the variable in the debugger.


  • Lifetime Qt Champion

    You are restoring the geometry, so you did store something at some point, didn't you ?

  • Please ignore the if (!ba.isEmpty) {} - it is the ELSE leg I'm concerned about (IOW before any geometry has been saved). The DEFAULT location of the window/widget is "odd" as it has positioned to -129, 72 which seems unusual to me.

  • bump ...

  • @Perdrix
    OK, I found your question confusing. You want us to ignore the code you posted about restoreGeometry(), your question is simply "why does mapToGlobal(QPoint(0, 0)) return -129, 72, is that right?

    mapToGlobal() is a method of QWidget

    Translates the widget coordinate pos to global screen coordinates.

    so what is your this widget? Is it the About box (I'm guessing so)?

    One thought: probably not right, but is it possible that this box does not have a sensible position until after it has been shown (first time)? Could you recall that code from somewhere after is has popped up and see if it gives same values?

    [At the moment I am fighting with (what seems to me) strange behaviour of mapToGlobal() result in the x but not the y direction for a widget. I am finding the x coordinate "moves" further to the right as x increases, yet the y position remains correct. I can't believe mapToGlobal() isn't correct, as I'm sure it gets used a lot, but I don't understand what's going on. Just saying....]

  • Yes indeed I'm trying to understand why the DEFAULT widget location is so weird - an x value of -129 is really crazy, and the y is not very logical either. Yes, "this" is the About box pointer.

    The code in the else leg has code to centre on the user application because the default location was so strange:

    		// Get NATIVE windows ultimate parent
    		HWND hParent = GetDeepStackerDlg(nullptr)->m_hWnd;
    		RECT r;
    		GetWindowRect(hParent, &r);
    		QSize size = this->size();
    		int top = (( + (r.bottom - / 2) - (size.height() / 2));
    		int left = ((r.left + (r.right - r.left) / 2) - (size.width() / 2));
    		move(left, top);

    which isn't pretty but does work.


Log in to reply