QDialog position in MacOS and Windows
-
Hi developers,
I am using Qt5.7.1 writing program on both Windows and MacOS. The program uses QDialog to alert user to do something. There will be picture in the middle of screen, I need to move the QDialog away from the picture, but I am suffering that QDialog::setGeometry() is not working the same way on MacOS and Windows. If I simply use the following code:QDialog message(this); message.exec();
The message dialog will on the middle of top of the screen on MacOS but is in the middle of screen on Windows. This will make my picture be covered by the message dialog, no matter the message dialog is in the middle of top of screen or middle (center) of the scree. Therefore I use setGeometry() or move() to move the dialog as following:
// Get geometry of main widget (this) first, say main_rect, the main widget spans the whole screen with flag (Window|FramelessWindowHint|WindowStaysOnTopHint) and state (WindowFullScreen) QDialog message(this); QRect rect(main_rect.left(), main_rect.top() + (main_rect.height() / 3), message.width(), message.height()); message.setWindowFlags(Dialog|FramelessWindowHint|WindowStaysOnTopHint|CustomizeWindowHint); message.setGeometry(rect); message.exec();
On MacOs, the message dialog still stays at the same position (middle of top of screen) but on Windows, the message dialog moves to middle of left of screen as I desired.
However, on MacOS, if I add show() and then move() as following, I can see the message dialog appears on the middle of the top of screen and then "jump" to the middle of left of the screen. On Windows, the box did move to left top of screen.
QDialog message(this); QRect rect(main_rect.left(), main_rect.top() + (main_rect.height() / 3), message.width(), message.height()); message.setWindowFlags(Dialog|FramelessWindowHint|WindowStaysOnTopHint|CustomizeWindowHint); message.setGeometry(rect); message.show(); message.move(rect.left(), rect.top()); // any point will do message.exec();
Does anyone has such situation about QDialog? Thanks in advance.
-
@Gilbert-Hsu said in QDialog position in MacOS and Windows:
QDialog message(this);
What happens if you do not set parent (remove "this")?
-
@jsulm Thanks for you reply.
Without setting parent, the message dialog can be positioned as what I desired, but when the program loses focus, the message dialog is disappear (only the full screen base widget left on screen). I need the dialog to stay on top of the screen (stick to the full screen base widget). AFAIK, the QDialog::exec() is Application Modal in default, which is true when the application gets focus, but I've tried Qt::WindowModal as well as Qt::ApplicationModal, they both act the same, when the program loses focus, the message dialog disappears or is covered by the base full screen widget. -
@Gilbert-Hsu exec() should actually force the dialog to be always on top of the application window. Try tor remove your setWindowFlags() call to see whether this makes a difference.
-
@jsulm Not mentioned the style, there's no difference between removing setWindowFlags() and not setting parent to the message dialog. The message dialog is gone when the application loses focus and back again when I click on the application. I mean this is on Xcode over MacOS.
-
Hi,
AFAIR, window modal dialogs on macOS always have the same position as they are tied to the windows they are modal from. Qt follows correctly the macOS human interface guidelines .
-
@SGaist Yes, I agree that the window modal dialogs on MacOS always have the same position in order to comply with MacOS UI. AFAIK, I can only think of QDialog to wait user to click button so that the program continues. If the QDialog cannot be moved, do you have any suggestions about how to popup a "dialog" and wait for user action?
-
Do you mean you want to have the application event loop continuing as normal ?
If so, then use the open slot rather than exec. You will have to adapt the logic a bit for that.
If not, then I am not sure I am following your requirements. macOS users do not expect to have blocking dialogs other that the usual you have seen in the guidelines. -
@SGaist No, the application event loop should wait for user to click button on the message dialog to continue. I quit trying to repositioning the QDialog, I change to simply use Object and QEventLoop to achieve this goal. Thanks for your answer :)