Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Positioning QMainWindow on Windows 10 Fails

Positioning QMainWindow on Windows 10 Fails

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by ModelTech
    #1

    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();
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomma
      wrote on last edited by tomma
      #2

      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.

      1 Reply Last reply
      2
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        Thanks for the hint on using saveGeometry/restoreGeometry :)

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          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));
          	}
          ...
          }
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved