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. Screen Change detection not working

Screen Change detection not working

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 3.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.
  • faiszalkhanF Offline
    faiszalkhanF Offline
    faiszalkhan
    wrote on last edited by
    #1

    I am drawing graphics items in my application and hence it is important to know if the screen has changed so that I can recalculate width and height and repaint the items on the scene. To clarify the requirement further, if the user drags the Mainwindow from one screen to another, the app should be triggered about the event to perform mentioned actions.

    Of all the solutions I have found on the forum, nothing seems to work. I have tried the three options(Called in Mainwindow constructor) as listed below but they didn't work either:

    connect(qApp, SIGNAL(primaryScreenChanged(QScreen*)), this, SLOT(myslot(QScreen*)));
    connect(qApp->desktop()->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(myslot(QScreen*)));
    connect(this->window()->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(myslot(QScreen*)));

    I'm using QT 5.9 on Windows 10.

    Would appreciate any help.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      and how have you verified that is is not working? is the slot method being called? is it giving an error about a nonexistent signal or slot? did you add debug code to the slot to see if the slot method is being called?

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      0
      • faiszalkhanF Offline
        faiszalkhanF Offline
        faiszalkhan
        wrote on last edited by
        #3

        @Kent-Dorfman the slot doesn't get called when I switch screens.

        1 Reply Last reply
        0
        • faiszalkhanF Offline
          faiszalkhanF Offline
          faiszalkhan
          wrote on last edited by
          #4

          I have found a partial solution wherein the return value of the following line changes when the app is moved between screens(monitors):
          qApp->desktop()->screenNumber(this);

          I intend to use a timer in the background and custom signal to let the app know when screen is changed. However, I believe this is not the best solution and I have a feeling that a better solution exists which I haven't found.

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            You are correct. That is an ugly hack, even though it works for your purpose. I'd continue to research the cause of the original problem, and post the solution when you figure it out.

            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

            1 Reply Last reply
            1
            • faiszalkhanF Offline
              faiszalkhanF Offline
              faiszalkhan
              wrote on last edited by
              #6

              @Kent-Dorfman so the slot is getting called now. Apparently, mainwindow->window()->windowHandle() returns a null pointer when the widget is not set as a native widget which makes the connection defunct.

              I have set Mainwindow as a native window by setting the following attribute:

              this->setAttribute(Qt::WA_NativeWindow);
              

              windowHandle() doesn't return a null pointer anymore. The slot is called as required with the following connection.

              connect(this->window()->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(sscreenChanged(QScreen*)));
              

              Cheers!

              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