Reputation: 176
I want to filter the USB device insert/removal events in my Qt application. So I need to use installNativeEventFilter()
to install a native even filter (derived from QAbstractNativeEventFilter
) to my app. The problem is I need emit some signals from my event filter, and connect them to the slots in my app. So, if I can inherent from both QApplication
and QAbstractNativeEventFilter
, then both the signals and slots are in the same class, and it's more convenient to connect them. But is the code below safe or not?
class QApp : public QApplication, public QAbstractNativeEventFilter
{
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE;
signals:
void deviceChanged();
...
};
QApp a(argc, argv);
a.installNativeEventFilter( &a );
Upvotes: 2
Views: 1952
Reputation: 98435
Why would you want to derive from QApplication
? To emit signals, all you need is to derive from QObject
:
class NativeEventFilter : public QObject, public QAbstractNativeEventFilter {
Q_OBJECT
...
public:
Q_SIGNAL void signal1();
explicit NativeEventFilter(QObject * parent = 0) : QObject(parent) {}
bool nativeEventFilter(const QByteArray & et, void * msg, long * result) override {
...
};
...
};
int main(int argc, char ** argv) {
QApplication app(argc, argv);
NativeEventFilter filter;
app.installNativeEventFilter(&filter);
...
return app.exec();
}
Upvotes: 5