Results 1 to 11 of 11

Thread: Can't seem to get global screen position

  1. #1

    Question Can't seem to get global screen position

    Hi guys,

    I've got a little problem: A little app of mine has a screenshot of the current desktop set as background. The application always runs in fullscreen. It all works just perfectly on a single monitor. However, on a dual-head system I run into some problems getting the right part of the screenshot to be set as background.

    More details: I've got my primary monitor on the right and a secondary monitor on the left. I simply cannot detect reliably what screen I'm running the app on or what global position the app has. Some code:

    Qt Code:
    1. // Getting the screenshot
    2. QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());
    3.  
    4. // Here's one problem: Getting the current screen size (see note after code snippet).
    5. globScreenRect = QApplication::desktop()->screenGeometry();
    6.  
    7. // The QPixmap bg is the new background pixmap, containing screen and an overlay
    8. QPainter painter(&bg);
    9. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    10. int x = 0;
    11. int y = 0;
    12. int w = screen.width();
    13. int h = screen.height();
    14. if(QApplication::desktop()->numScreens() == 2) {
    15. // Detecting if app is run on primary or secondary screen and adjust screenshot accordingly
    16. if(globScreenRect.x() != 0) {
    17. x = w-globScreenRect.width();
    18. w = globScreenRect.width();
    19. } else {
    20. w = globScreenRect.width();
    21. }
    22.  
    23. }
    24. painter.drawPixmap(0, 0, overlay.width(), overlay.height(), screen,x,y,w,h);
    25. painter.drawPixmap(overlay.rect(), overlay);
    26. painter.end();
    To copy to clipboard, switch view to plain text mode 
    The problem seems to be detecting the current active screen size. "screenGeometry()" always returns the geometry of the primary screen (i.e. QRect(1024,0 1366x768) ). I could get the geometry of the secondary screen simply by using "screenGeometry(1)", but how do I know if the app is running on the primary or secondary screen?

    If I try to find out the current global position of the app by "this->pos().x()" or even "mapToGlobal(this->pos()).x()", it always gives me "0", i.e. the absolute top left position... According to the Qt Doc: "mapToGlobal - Translates the widget coordinate pos to global screen coordinates.", but it somehow doesn't do that...?

    Am thankful for any help

    huilui

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by huilui View Post
    More details: I've got my primary monitor on the right and a secondary monitor on the left. I simply cannot detect reliably what screen I'm running the app on or what global position the app has.
    Is QDesktopWidget::primaryScreen() any use? The documentation is inconsistent: it says that property “holds the index of the screen that is configured to be the primary screen on the system”; but in the general description for QDesktopWidget it says, “For an application, the screen where the main widget resides is the primary screen. This is stored in the primaryScreen property.”

    (Lord knows what the main widget is supposed to mean.)

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,731
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can't seem to get global screen position

    QDesktopWidget::screenNumber() with your main window should tell you which screen the majority of your main window is on. Compare that return to the QDesktopWidget::primaryScreen() and you know whether you are on the primary screen or not.

  4. #4

    Default Re: Can't seem to get global screen position

    Thanks guys for the ideas. I added this line to the code:
    Qt Code:
    1. qDebug() << QApplication::desktop()->primaryScreen() << "-" << QApplication::desktop()->screenNumber() << "-" << QApplication::desktop()->screenCount();
    To copy to clipboard, switch view to plain text mode 

    However, no matter what screen I run the app on, I always get the following output:
    0 - 0 - 2

    It just doesn't make sense...

  5. #5
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't seem to get global screen position

    well, it kinda makes sense when you read:

    “For an application, the screen where the main widget resides is the primary screen. This is stored in the primaryScreen property.”
    - Coises.

    now you have to check that the sizes of the different screens always correspond correctly?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by huilui View Post
    Thanks guys for the ideas. I added this line to the code:
    Qt Code:
    1. qDebug() << QApplication::desktop()->primaryScreen() << "-" << QApplication::desktop()->screenNumber() << "-" << QApplication::desktop()->screenCount();
    To copy to clipboard, switch view to plain text mode 

    However, no matter what screen I run the app on, I always get the following output:
    0 - 0 - 2

    It just doesn't make sense...
    Per ChrisW67’s suggestion, what happens if you replace QApplication::desktop()->screenNumber() with QApplication::desktop()->screenNumber(widget), where widget is a pointer to your window widget? (See: QDesktopWidget::screenNumber)

    Similarly, note that QDesktopWidget::availableGeometry can take a QWidget* argument.

  7. #7

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by amleto View Post
    well, it kinda makes sense when you read:

    “For an application, the screen where the main widget resides is the primary screen. This is stored in the primaryScreen property.”
    - Coises.

    now you have to check that the sizes of the different screens always correspond correctly?
    But then I should get different screen resolutions for the primary screen each time. But the following always returns "QRect(1024,0 1366x768)"...
    Qt Code:
    1. QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen())
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by Coises View Post
    Per ChrisW67’s suggestion, what happens if you replace QApplication::desktop()->screenNumber() with QApplication::desktop()->screenNumber(widget), where widget is a pointer to your window widget? (See: QDesktopWidget::screenNumber)

    Similarly, note that QDesktopWidget::availableGeometry can take a QWidget* argument.
    Hm, when I try
    Qt Code:
    1. QApplication::desktop()->screenNumber(this)
    To copy to clipboard, switch view to plain text mode 
    I get a slightly different output to the qDebug() statement: "0 - 1 - 2". But again, it's the exact same no matter what screen I start the app on...
    I'll have a look at QDesktopWidget::availableGeometry() later-on when I'm back home... hopefully that'll work!!

  8. #8
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by huilui View Post
    But again, it's the exact same no matter what screen I start the app on...
    For me, that would be where I give up, conclude that Qt has missed (again), and just work out how to do it with the native API for whatever operating system(s) this will run on.

    Based only on my quite limited personal experience, I’d put the odds at 50-50 that you’ll never find a pure Qt solution, so this where it become more time/cost-effective to forget Qt and use the native API while you still have a few shreds of sanity left.

  9. #9

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by Coises View Post
    For me, that would be where I give up, conclude that Qt has missed (again), and just work out how to do it with the native API for whatever operating system(s) this will run on.

    Based only on my quite limited personal experience, I’d put the odds at 50-50 that you’ll never find a pure Qt solution, so this where it become more time/cost-effective to forget Qt and use the native API while you still have a few shreds of sanity left.
    Hm, I think you're right. It might be better to stop messing around with Qt trying to get that to work and just do it without Qt... It'l probably take a good while, but then it will (hopefully) work at least.

    Thanks to all of you guys for your help

  10. #10
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't seem to get global screen position

    Quote Originally Posted by huilui View Post
    It'l probably take a good while, but then it will (hopefully) work at least.
    If it’s Windows, see the topics under:

    Multiple Display Monitors

    especially the example here:

    Positioning Objects on a Multiple Display Setup

    and the references here:

    Multiple Display Monitors Functions

    I only have one monitor here, so I can’t tell you if all that works as advertised. I imagine Qt is attempting to use those same functions. (To get an HWND from a Qt window, use QWidget::winId.)

  11. #11

    Default Re: Can't seem to get global screen position

    Thanks for the links coises!! However, my app is used mostly in Linux (don't know of anybody who tried to run it in Windows yet)...

Similar Threads

  1. Replies: 2
    Last Post: 26th April 2009, 07:34
  2. Mouse position on screen
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 14:22
  3. Setting manually cursor position on multi screen systems
    By irreriwan in forum Qt Programming
    Replies: 0
    Last Post: 4th March 2008, 09:47
  4. How to know current global mouse position?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2007, 19:25
  5. positioning a window at a specific screen position
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2006, 20:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.