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. Main Window Repositioning

Main Window Repositioning

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.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.
  • J Offline
    J Offline
    jdarnold
    wrote on last edited by
    #1

    We save the position of the window when our app was closed and then restore it when you run it again. However, if the screen resolution has changed, it can end up off the screen. I've tried the following code and it almost works:

    @ // restore size and location
    settings->beginGroup("MainWindow");
    QSize size(settings->value("size", QSize(340, 600)).toSize());
    QPoint pos(settings->value("pos", QPoint(200, 200)).toPoint());
    settings->endGroup();

    resize(size);
    QDesktopWidget* desktop = QApplication::desktop();
    int screenwidth(desktop->frameGeometry().width());
    int screenheight(desktop->frameGeometry().height());
    int windowwidth(QMainWindow::frameGeometry().width());
    int windowheight(QMainWindow::frameGeometry().height());
    
    if ( (pos.x()+windowwidth) > screenwidth)
        pos.setX(screenwidth - windowwidth);
    if ( (pos.y()+windowheight) > screenheight)
        pos.setY(screenheight - windowheight);
    
    move(pos);
    

    @

    The problem is that the window is still offscreen a little bit. Nothing major but it sure looks like the frame still isn't getting accounted for. If I stop it in the debugger, I see that the 'size' height and width are the same as windowheight and windowwidth, which doesn't sound possible. If I'm reading the docs right, the size I resize to can't be the same as the frameGeometry size, as the former doesn't include decorations but the latter does.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you repositiopn it, but you don't resize it, that is the problem.
      x should never be smaller then 0 and with should not be bigger than screenwidth.
      same applies to y and heigth.

      if you adjust by these, it should work:

      @
      bool adjustToScreen(QRect& rRect)
      {
      QDesktopWidget* desktop = QApplication::desktop();
      QRect rcDesktop = desktop->frameGeometry();

      // if the original rect is not completly inside the desktop rectangle, it must be adjusted
      if(!rcDesktop.contains(rRect))
      {
          rRect.setLeft(qMax(rcDesktop.left(), rRect.left()));
          rRect.setTop(qMax(rcDesktop.top(), rRect.top()));
          rRect.setRight(qMin(rcDesktop.right(), rRect.right()));
          rRect.setBottom(qMin(rcDesktop.bottom(), rRect.bottom()));
          return true;
      }
      
      return false;
      

      }

      void MainWindow::restore()
      {
      QSettings settings;
      // restore size and location
      settings.beginGroup("MainWindow");
      QSize size(settings.value("size", QSize(340, 600)).toSize());
      QPoint pos(settings.value("pos", QPoint(200, 200)).toPoint());
      settings.endGroup();

      resize(size);
      move(pos);
      
      QRect rc = frameGeometry();
      if(adjustToScreen(rc))
      {
          move(rc.topLeft());
          QSize ncSize = frameGeometry().size() - geometry().size();
          resize(rc.size() - ncSize);
      }
      

      }

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jdarnold
        wrote on last edited by
        #3

        But I don't want to resize it again. I should just be able to move the window more precisely onto the desktop.

        It is nice looking code, but I don't think the rRect repositionings work. I just want to move the window back onto the screen.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zester
          wrote on last edited by
          #4

          You could use libqxt

          LibQxt is an extension library for Qt providing a suite of cross-platform utility classes to add functionality not readily available in the Qt toolkit by Qt Development Frameworks, Nokia.

          http://dev.libqxt.org/libqxt/wiki/Home

          http://libqxt.bitbucket.org/doc/tip/qxtscreen.html
          The QxtScreen class provides access to screen settings.
          Note: Currently supported platforms are X11 and Windows.

          QSize QxtScreen::resolution () const
          Returns the current resolution.

          You could get the screen resolution and set the size/position of your window accordingly.

          You may have no choice but to resize depending on the end users screen resolution. My current monitor is a 15inch flat screen. I get a max resolution of 1024, 768 and 800, 600 window takes up most of the desktop.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Hi jdarnold,

            if you don't want to resize, you could just use the move command, and let the resize out.

            adjustToScreen(...)
            checks, that the given rectangle is inside the current screen.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            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