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. Find the active QScreen of any QWidget

Find the active QScreen of any QWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 7.2k Views
  • 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.
  • R Offline
    R Offline
    Richard
    wrote on 3 Jan 2019, 08:12 last edited by
    #1

    Hi, Is there an easy way to find a widget's active QScreen it resides on, obviously in a multi display environment. I am using Qt5.8 on windows 10.

    I have extended the desktop with with an additional monitor which has lower resolution then my laptop.
    This gives me some scaling issues with QPicture I am trying to work around.
    I tried the function windowsHandle() which has a function screen but this function returns zero, propably because my widget is not a top level widget but instead part of an MDI child.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Jan 2019, 08:54 last edited by
      #2

      Hi
      Could you not you use
      http://doc.qt.io/qt-5/qdesktopwidget.html#screenNumber
      "Returns the index of the screen that contains the largest part of widget"

      1 Reply Last reply
      4
      • R Offline
        R Offline
        Richard
        wrote on 3 Jan 2019, 10:50 last edited by
        #3

        Hi, Thanks for your suggestion. I tried this but this always returns 0. This is correct though, I extended my desktop so there is always one virtual screen.
        But then again QDesktopWidget::screenCount() return 2. So this is a bit odd.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Richard
          wrote on 3 Jan 2019, 10:53 last edited by
          #4

          I found this in the docu:

          "virtualDesktop : const bool
          This property holds if the system manages the available screens in a virtual desktop.
          For virtual desktops, screen() will always return the same widget. The size of the virtual desktop is the size of this desktop widget."

          So... yep virtualDeskTop() returns true.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Jan 2019, 11:01 last edited by
            #5

            Hi
            Ok, i think you need to loop over the screens and their geometry and
            check that against the apps x,y to see on which screen it is placed.
            if its x are bigger than screen 0 width, then its on screen 1.

            Notice, that apps. x might be negative. At least thats the case when placing
            remote desktop windows and second monitor is on the left. . I didn't test
            what Qt reports in this case.

            Technically its one big screen so hence screenNumber reports correctly.
            Sorry, i missed the "extended" word in your description. :)

            1 Reply Last reply
            2
            • R Offline
              R Offline
              Richard
              wrote on 3 Jan 2019, 11:40 last edited by
              #6

              This works ok. But not every widget has a windowHandle (maybe olny top level ones?) So then i grab the parent to check it.

              QScreen* getActiveScreen(QWidget* pWidget) const
              		{
              			QScreen* pActive = nullptr;
              
              			while (pWidget)
              			{
              				auto w = pWidget->windowHandle();
              				if (w != nullptr)
              				{
              					pActive = w->screen();
              					break;
              				}
              				else
              					pWidget = pWidget->parentWidget();
              			}
              
              			return pActive;
              		}
              
              M 1 Reply Last reply 3 Jan 2019, 11:49
              3
              • R Richard
                3 Jan 2019, 11:40

                This works ok. But not every widget has a windowHandle (maybe olny top level ones?) So then i grab the parent to check it.

                QScreen* getActiveScreen(QWidget* pWidget) const
                		{
                			QScreen* pActive = nullptr;
                
                			while (pWidget)
                			{
                				auto w = pWidget->windowHandle();
                				if (w != nullptr)
                				{
                					pActive = w->screen();
                					break;
                				}
                				else
                					pWidget = pWidget->parentWidget();
                			}
                
                			return pActive;
                		}
                
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 3 Jan 2019, 11:49 last edited by
                #7

                Hi
                ok so for a top level widget/window, ->screen()
                actually knows which screen even if its a virtual screen/extended.
                That is good to know :)
                Its explained here whom have >windowHandle();
                http://doc.qt.io/qt-5/qwidget.html#windowHandle

                1 Reply Last reply
                1

                3/7

                3 Jan 2019, 10:50

                • Login

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