PDA

View Full Version : help with QAbstractNativeEventFilter



alee
2nd October 2013, 19:48
hi,

i am trying to catch native events on the windows platform, to communicate with an hid usb device.... i have successfully done so using the nativeEvent method in QWidget....now i am trying to create a generic class that can be added to any QWidget or QMainWindow.

for this pupose i am trying to derive a class from QAbstractNativeEventFilter .... Override the virtual nativeEventFilter and instantiate this class in the QWidget of my choice....yet i am unable to recieve any events so far.....

i am new to qt and havent had much experience using abstract classes.....i am attaching a sample below....using this example the code compiles and the widget window appears but the nativeeventfilter method is not called. any help would be highly appreciated.....

usbworker.h

#ifndef USBWORKER_H
#define USBWORKER_H

#include <QAbstractNativeEventFilter>

class usbworker : public QAbstractNativeEventFilter
{
public:
usbworker();
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);

};

#endif // USBWORKER_H

usbworker.cpp

#include "usbworker.h"

usbworker::usbworker()
{

}


bool usbworker::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
Q_DECL_OVERRIDE
{
qDebug("i came here");
return false;
}


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <usbworker.h>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
usbworker uw;

private:
Ui::Widget *ui;
};

#endif // WIDGET_H


widget.cpp

#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}

Widget::~Widget()
{
delete ui;
}


main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}


i am using qt 5.1.0

alee
2nd October 2013, 22:39
i have sorted how to do this.....:D

it was said in the documentation that, the filter needs to installed on the application object.....i tried finding what that meant and found no results, at least none that i could understand....then i started browsing through the documention of QAbstractEventDispatcher and their i found an indirect hint. i followed up on it and, viola!!! it worked....

the changes needed to be made to main.cpp below is the reivsed code. i needed to installNativeEventFilter on the application instance in the main file



#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
a.installNativeEventFilter(&w.uw); //this installs the instance uw of the usbworker inside the widget w, on the application instance a.
w.show();

return a.exec();
}


i have further modified my usbworkerclass to inherit from QObject and QAbstractNativeEventFilter. since the later is an abstract class(an interface) it is ok to inherit multiple.(correct me if i am wrong).....

it would be great if the documentation of the abstract class be made a bit clearer....or probably its just me who did not understand it.