Qt World Summit: Submit your Presentation

Screen Change detection not working

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

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

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

  • I have found a partial solution wherein the return value of the following line changes when the app is moved between screens(monitors):

    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.

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

  • @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:


    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*)));


Log in to reply