What does QML call QApplication? As in the C++ application intialization creates a QApplication instance.
-
In C++, in main, we create a QApplication instance/object. I have created an event filter in it to monitor user activity, or more appropriately, lack of activity. (If the app is idle for too long, dialogs and menus are to be closed.) It also has a signal to be emitted when activity is detected.
On the QML side I need to Connect to that object so I can receive the signal. I use this signal to reset a timer. If the timer goes off before being reset, it indicates no activity in the allotted time and any dialogs/menus are closed.
QML appears to have an "Application" object and an "ApplicationWindow" object, but there is no documentation indicating what these are. I've tried enough wild guesses on my own, now I'm hoping someone knows how to refer to the QApplication object in QML so that I can connect and receive the signal.
Thanks in advance.
-
https://stackoverflow.com/questions/53247650/access-application-version-from-within-qml ?
Edit:
Duck Duck Go search string: "qml accessing qapp" -
This won't let you access the actual QGuiApplication instance.
QML appears to have an "Application" object and an "ApplicationWindow" object, but there is no documentation indicating what these are.
I'm very confused by this statement.
Qt.application
's documentation says : "The application object provides access to global application state properties shared by many QML components."Application
in Qt 6 is a singleton meant to replace the above. Its doc says : "The Application singleton exposes a subset of QApplication's properties to QML applications."We can see in both doc the list of properties or signals it exposes. It exposes properties of the QApplication but not the actual instance.
As for
ApplicationWindow
the doc says "ApplicationWindow is a Window which makes it convenient to add a menu bar, header and footer item to the window." so it's just a Window with more features, akin toQMainWindow
in the widgets world.Regarding your original question, you can expose a c++ object like always with the methods mentionned here https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
qmlRegisterSingletonInstance
seems to fit the bill in your use case. -
Got it working.
Initially tried to inherit QApplication and incorporate the eventFilter and the signal to be sent in there. That didn't work.
Then sorted through Grecko's suggestion, which BTW, qmlRegisterSingletonInstance appears nowhere in the link provided, initially trying to make the QApplication object accessible from QML.
That didn't work.Finally got it to work by making the custom object containing the eventFilter and signal accessible from QML (you would think that, installEventFilter, would reparent the object, but then again a custom QApplication that incorporated one didn't work either) and that allowed it to work.