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



  • 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


  • Lifetime Qt Champion

    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



  • 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!


  • Moderators

    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.


Log in to reply
 

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