Positioning QMainWindow on Windows 10 Fails



  • I use QSettings to store and load the position and size of my application window. On Windows 10, the position is however not restored correctly: my application window is put on the screen in such a way that I cannot see the window top and main menu any more, while it wasn't there when I closed the application previously. Only by resizing the application window in such a way that Windows recognizes it as if I want it to be as heigh as the screen is, I get the window top and menu back. I didn't experience this phenomena on OSX, Ubuntu or Windows 7. I use Qt 5.5.1 with MSVC 2013 (32 bit) to get the release version for windows of my application that has this behaviour. Any suggestions what could be the problem?

    I use the following code to store the application window size and position in my QMainWindow class:

        QSettings Settings(...);
        Settings.beginGroup("General");
        Settings.setValue("XPosition", x());
        Settings.setValue("YPosition", y());
        Settings.setValue("Width", width());
        Settings.setValue("Height", height());
        Settings.endGroup();
    

    The following code is used in my QMainWindow class to restore its size and position:

        QSettings Settings(...);
        Settings.beginGroup("General");
        setMinimumSize(800,600);
        setGeometry(Settings.value("XPosition").toInt(), Settings.value("YPosition").toInt(),
                    Settings.value("Width").toInt(), Settings.value("Height").toInt());
        Settings.endGroup();
    


  • You should use QWidgets save/restoreGeometry() which handles correct geometry and automaticly avoids restoring window off-screen. Also with QMainWindow you can save states of toolbars using save/restoreState().

    With Windows 10, issue can be caused by old Qt version.



  • Thanks for the hint on using saveGeometry/restoreGeometry :)



  • Based on the Qt help the member setGeometry excludes the window frame where the x() and y() members includes the window frame. This could be the reason you loose the top part of your window (not sure why it is a problem only on Win10 though).

    This is what I use to save and restore the position of a Qt window. I haven't had any issues with it (including on Win10) that I noticed at least.

    void Mainwindow::closeEvent(
    	QCloseEvent 						*Event)
    {
    	d_Settings->setValue("Mainwindow_Position", pos());
    	d_Settings->setValue("Mainwindow_Size", size());
    ...
    }
    
    void Mainwindow::Mainwindow(...)
    {
    	QPoint								Position;
    	QSize								Size;
    	QRect								DesktopRect;
    	QRect								WidgetRect;
    
    	Position = d_Settings->value("Mainwindow_Position", QPoint(30,30)).toPoint();
    	Size = d_Settings->value("Mainwindow_Size", QSize(800, 509)).toSize();
    
    	DesktopRect = QApplication::desktop()->availableGeometry();
    	
    	WidgetRect.moveTopLeft(Position);
    	WidgetRect.setSize(Size);
    
    	WidgetRect = WidgetRect & DesktopRect;  // intersect
    
    	if(WidgetRect.isValid())
    	{
    		this->resize(WidgetRect.size());
    		this->move(WidgetRect.topLeft());
    	}
    	else // default size and position
    	{
    		this->resize(QSize(800, 509));
    		this->move(QPoint(30,30));
    	}
    ...
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.