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. QGuiApplication::screenAdded signal never emitted
Forum Updated to NodeBB v4.3 + New Features

QGuiApplication::screenAdded signal never emitted

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 901 Views 2 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by graniteDev
    #1

    I'm trying to detect screens being plugged and unplugged from a system. I've been trying to use QGuiApplication::screenAdded and QGuiApplication::screenRemoved signals, but they these signals are never emitted.

    I can use a timer to periodically check the screen count from the QGuiApplication, and that works, it always shows the correct number of screens connected, but I don't get any signal emitted when I plug and unplug screens from the computer

    Also very frustrating, documentation states that "The QGuiApplication object is accessible through the instance() function, which returns a pointer equivalent to the global qApp pointer." However, I tried QGuiApplication::instance() and it gave me a pointer to a QCoreApplication which does not have the signals or methods that I need. My workaround is in the main.cpp to do this:

    int main(int argc, char *argv[])
    {
        QGuiApplication q(argc, argv);
        DesktopManager w(&q);
    
        return q.exec();
    }
    

    And then take that pointer that I get and assign it to a member variable in my main class like so:

    DesktopManager::DesktopManager(QGuiApplication *qga, QObject *parent)
        : QObject(parent),
          m_connected(false),
          m_prevScreenCount(1),
          m_idealSize(1920, 1080)
    {
        m_guiApplication = qga;
        connect(m_guiApplication, &QGuiApplication::screenAdded, this, &DesktopManager::checkScreenCount);
        connect(m_guiApplication, &QGuiApplication::screenRemoved, this, &DesktopManager::checkScreenCount);
        checkScreenCount();
    

    As I expect, the checkScreenCount() method is run on startup, but never again. I can plug and unplug screens as much as I want, it's never run again. If your wondering if something is wrong with the pointer, it appears to work just fine as checkScreenCount() uses:

    QList<QScreen*> screenList = m_guiApplication->screens();
    

    where m_guiApplication is pointing to our QGuiApplication object, and that returns a list of screens, so I know the pointer is working as expected.

    So my two questions here are 1) how do I get those signals to actually work correctly? 2) does this have anything to do with the fact that the QGuiApplication::instance() doesn't return what the documentation says it should?

    kshegunovK 1 Reply Last reply
    0
    • G graniteDev

      I'm trying to detect screens being plugged and unplugged from a system. I've been trying to use QGuiApplication::screenAdded and QGuiApplication::screenRemoved signals, but they these signals are never emitted.

      I can use a timer to periodically check the screen count from the QGuiApplication, and that works, it always shows the correct number of screens connected, but I don't get any signal emitted when I plug and unplug screens from the computer

      Also very frustrating, documentation states that "The QGuiApplication object is accessible through the instance() function, which returns a pointer equivalent to the global qApp pointer." However, I tried QGuiApplication::instance() and it gave me a pointer to a QCoreApplication which does not have the signals or methods that I need. My workaround is in the main.cpp to do this:

      int main(int argc, char *argv[])
      {
          QGuiApplication q(argc, argv);
          DesktopManager w(&q);
      
          return q.exec();
      }
      

      And then take that pointer that I get and assign it to a member variable in my main class like so:

      DesktopManager::DesktopManager(QGuiApplication *qga, QObject *parent)
          : QObject(parent),
            m_connected(false),
            m_prevScreenCount(1),
            m_idealSize(1920, 1080)
      {
          m_guiApplication = qga;
          connect(m_guiApplication, &QGuiApplication::screenAdded, this, &DesktopManager::checkScreenCount);
          connect(m_guiApplication, &QGuiApplication::screenRemoved, this, &DesktopManager::checkScreenCount);
          checkScreenCount();
      

      As I expect, the checkScreenCount() method is run on startup, but never again. I can plug and unplug screens as much as I want, it's never run again. If your wondering if something is wrong with the pointer, it appears to work just fine as checkScreenCount() uses:

      QList<QScreen*> screenList = m_guiApplication->screens();
      

      where m_guiApplication is pointing to our QGuiApplication object, and that returns a list of screens, so I know the pointer is working as expected.

      So my two questions here are 1) how do I get those signals to actually work correctly? 2) does this have anything to do with the fact that the QGuiApplication::instance() doesn't return what the documentation says it should?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @graniteDev said in QGuiApplication::screenAdded signal never emitted:

      1. how do I get those signals to actually work correctly?

      Not sure about that. You'd have to probably dig into the QPA layer to get your answer, maybe the window manager doesn't report the state changes ...

      1. does this have anything to do with the fact that the QGuiApplication::instance() doesn't return what the documentation says it should?

      No, because it does exactly what it supposed to. QCoreApplication::instance() returns a pointer to the application object, but you're going to need to cast it to use it (it's derived from QCoreApplication in QGuiApplication). You can use it like this:

      QGuiApplication * ptr = qobject_cast<QGuiApplication *>(qApp);
      if (!ptr)
          ; // Not a gui application

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What OS are you working on ?
        What version of Qt is it ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

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