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. How to move a window from one monitor to another in a multi-monitor environment
Qt 6.11 is out! See what's new in the release blog

How to move a window from one monitor to another in a multi-monitor environment

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 12.6k 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.
  • E Offline
    E Offline
    EricRFMA
    wrote on last edited by
    #1

    Hello assembled wise-ones...

    I have a "window" (specifically, a QDialog), that I want to reposition programmatically, on a system with multiple monitors. I found several classes that will tell you about multiple monitors, such as QApplication, QGUIApplication and QDesktop. I've opted for QApplication::desktop(), 'cause the documentation told me so ;) This gives me a QDesktopWidget object.

    The doc for QDesktopWidget says:

    bq.
    Applications can use this class to obtain information that can be used to save window positions, or to place child widgets and dialogs on one particular screen.

    The part I haven't been able to figure out is the "...place child widgets and dialogs on one particular screen." part.

    I can see where I can get screen numbers and/or QScreen's for any/all screens, but I haven't seen any way to set which screen a widget (using the term generically) should appear. The only method I've found that moves a widget is move and that only takes a QPoint as an argument.

    I'm aware that there's also a virtualDesktop property, but I'm not sure how this enters into the mix. QDesktop->availableGeometry only gives me the dimensions of my primary screen (I'm an a Mac, in case that makes any difference)

    Can anyone shed any light on this? Am I missing something? Is this even possible?

    Thanks!

    -Eric

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can use availableGeometry giving the screen number where you want to move your widget, then move it e.g. using the center for the returned rectangle

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EricRFMA
        wrote on last edited by
        #3

        thanks SGaist!

        Two followup questions...

        (1) Do screen numbers start at zero?
        (2) So, the coordinates returned by availableGeometry determine which screen as well as where on the screen, just via (x,y) coordinates? As opposed to using a combination of screen number and coordinates on that screen?

        Just want to make sure I'm clear on the details :)

        Thanks again!

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Screen numbers start at 0. An index of -1 will give you the default screen.

          Lets say you have 2 hd monitors side by side.
          There are 3 different rectangles returned by different QDesktopWidget methods:

          • screen(index)->rect() will give you a rectangle (0,0,1920x1080) on both indices
          • screenGeometry(index) will give you (0,0,1920x1080) on first and (1920,0,1920x1080) on the second
          • availableGeometry(index) is the actual usable space on the screen, so for example on Windows the space without the taskbar and any pinned sidebars. On mac I suppose this would be the space without the top app bar(whatever you hipsters call it :) ), so this will be something like (0,40,1920x1040) and (1920,40,1920x1040) depending on the size and placement of bars.

          So to move a window to another screen you pick the screen you want it on, get its available geometry and use move with a point inside that rectangle.
          You can figure out which monitor is to the left, bottom etc. from the default one by analyzing these rectangles.

          1 Reply Last reply
          1

          • Login

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