Qt: showMaximized() not working in Windows

5.2k views Asked by At

I am opening a file in my application (in Windows) by double-clicking it. I am passing the file name as received through argument to my open logic. There I am calling showMaximized(), but it is not getting picked up. The window that opens up is not maximized and defaultly located at the top-left corner of the screen.

Note that all this logic flow is through main() and hence showMaximized() is probably getting called before the event loop starts. Is this stopping the showMaximized() to work properly? If yes, how to solve this?

I also tried using QTimer::singleShot(0,...,...) (so as to let the event loop start) but this has even stopped the launching of the non-maximized screen.

PS: In Mac the screen is getting maximized as there it happens through event (QEvent::FileOpen)

4

There are 4 answers

1
adzm On

The very first time a process calls ShowWindow, the show command is ignored and uses the command provided in the STARTUPINFO structure (which should correspond with the nCmdShow parameter in WinMain).

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx

This surprising behavior has a tendency to manifest itself in problems like the one you describe. You may be able to resolve it by simply issuing QT's showMaximized call twice if you don't want to use any Win32 API calls directly.

0
Ivan Zverev On

So on Windows I used a timer in the Application Windows. I know it is smoehow ugly, but it works...

ApplicationWindow {
    Timer {
       id: fullscreenTimer
       interval: 1000; running: false; repeat: false;
       onTriggered: root.showFullScreen();
    }
    Component.onCompleted: {
       fullscreenTimer.start();
    }
}
0
BryanCasanelli On

My answer is late but I write it in case it helps someone.

I am experimenting the same problem on Windows 11 with PyQt 6.

Initially I used the solution proposed by Vasilyev Eugene but it fails in some cases.

The best solution that I found was to use sigleShot with 1000ms of delay:

QtCore.QTimer.singleShot(1000, self.showMaximized)

The time is almost imperceptible to the final user, and you can experiment with lower values, except 0 apparently.

This solution is for PyQt but it is also applicable for Qt.

1
Vasilyev Eugene On

Simplest workaround is use resize(800,600) before using showMaximized(). I have similar error in Qt 5.7.0 on Windows 8.1.