Unsolved QGuiApplication::screenAdded signal never emitted
-
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?
-
@graniteDev said in QGuiApplication::screenAdded signal never emitted:
- 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 ...
- 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 fromQCoreApplication
inQGuiApplication
). You can use it like this:QGuiApplication * ptr = qobject_cast<QGuiApplication *>(qApp); if (!ptr) ; // Not a gui application
-
Hi,
What OS are you working on ?
What version of Qt is it ?