Thanks again Villep,
I followed the viki article & then added your code to the project. But this time it returns error on the line:
Code:
root->setContextProperty("eventFilter", &eventFilter);
it says: error: invalid use of incomplete type 'struct QDeclarativeContext'
Can you tell whats gone wrong?
Here are the eventfilter.h, eventfilter.cpp and main.cpp files:
main.cpp:
Code:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "eventfilter.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
EventFilter eventFilter;
app.installEventFilter(&eventFilter);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.setMainQmlFile(QLatin1String("qml/mySnake2/Game.qml"));
viewer.showExpanded();
QDeclarativeContext *root = viewer.rootContext();
root->setContextProperty("eventFilter", &eventFilter);
return app.exec();
}
eventfilter.h:
Code:
#ifndef EVENTFILTER_H
#define EVENTFILTER_H
#include <QObject>
class EventFilter : public QObject
{
Q_OBJECT
public:
explicit EventFilter(QObject *parent = 0);
signals:
void activeChanged(bool active);
public slots:
};
#endif // EVENTFILTER_H
eventfilter.cpp :
Code:
#include "eventfilter.h"
bool EventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ApplicationDeactivate) {
emit activeChanged(false);
return true;
}
if (event->type() == QEvent::ApplicationActivate) {
emit activeChanged(true);
return true;
}
return QObject::eventFilter(obj, event);
}