Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is it possible to intercept maximizing the main window?



  • Is there a way to intercept a QMainWindow being maximized, and change behaviour?

    I want main window to still have the usual native window "maximize" button for user to click, but I want it to set size to a particular value rather than actually maximizing it. Linux & Windows.

    (Why? Well, before you challenge me: during development I allow specification of a desktop size which relates to some other (smaller) screen device I wish to support, so that I can emulate it and see what things look like. I'd like this convenient way to be able to set it to the maximum size specified for that desktop. Note that just setting a fixed size is not acceptable: I must still be able to resize, at least to make it smaller then the emulated desktop.)

    I don't know whether one can do this --- intercept native maximumization button, and then change the event such that it does not really maximize but instead resizes to a certain size?

    Or, it occurs to me, maybe I have seen in other native apps that they are able to "limit" the main window's maximum size to something less than the desktop (how?) and then I wouldn't have to intercept any event?

    EDIT: I can see that QMainWindow::setMaximumSize() works for drag-resizing, that might be usable but I'd still like to be able to limit when the user clicks the maximize button, which ignores that setting and fills the whole real desktop.

    EDIT2: I have tried (under Linux, no access to Windows) both changeEvent() & eventFilter(). I receive the WindowStateChange maximize events but no amount of accepting/ignoring/returning true actually "cancels" the maximization/minimization. See e.g. https://www.qtcentre.org/threads/12300-Unable-to-handle-WindowStateChange-events. In fact I seem to receive both of these after I see the window get maximized, is the maximize-from-window-button something which gets pre-handled by the native windowing system before it gets passed to Qt?



  • So the conclusion is: no, from Qt (as opposed to some native code) you cannot "intercept" window maximization in the way I mean, i.e. before the windowing system does the actual maximization: Qt only sends you the signal after that, to react on.



  • @JonB said in Is it possible to intercept maximizing the main window?:

    I have seen in other native apps that they are able to "limit" the main window's maximum size to something less than the desktop (how?)

    You are talking about this?! Software DesktopCoral.

    If ignoring the event doesnt work, I dont know how they have done it and if it is even possible in Qt. I fear, the Window Manager will kick in every time and set the window size maximized to desktop.

    @JonB said in Is it possible to intercept maximizing the main window?:

    In fact I seem to receive both of these after I see the window get maximized

    This might be a little dirty, but when the eventHandler intercepts AFTER the window is maximized by OS, why not try to set your size immediately after that? Like this



  • @Pl45m4

    AFTER the window is maximized by OS, why not try to set your size immediately after that

    Because I first see the window redrawn to fill my real desktop, and then I make it normal again and it gets redrawn at the emulated size. Which would not look good if I needed this in production :)

    ATM that is what I am doing because I can't find (and I've done a lot of Googling) any way to stop the windowing system from maximizing before it gets to Qt. I want to leave this issue open for a bit in case someone else knows better....



  • So the conclusion is: no, from Qt (as opposed to some native code) you cannot "intercept" window maximization in the way I mean, i.e. before the windowing system does the actual maximization: Qt only sends you the signal after that, to react on.


Log in to reply