Results 1 to 2 of 2

Thread: help with QAbstractNativeEventFilter

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question help with QAbstractNativeEventFilter

    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
    Qt Code:
    1. #ifndef USBWORKER_H
    2. #define USBWORKER_H
    3.  
    4. #include <QAbstractNativeEventFilter>
    5.  
    6. class usbworker : public QAbstractNativeEventFilter
    7. {
    8. public:
    9. usbworker();
    10. virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);
    11.  
    12. };
    13.  
    14. #endif // USBWORKER_H
    To copy to clipboard, switch view to plain text mode 

    usbworker.cpp
    Qt Code:
    1. #include "usbworker.h"
    2.  
    3. usbworker::usbworker()
    4. {
    5.  
    6. }
    7.  
    8.  
    9. bool usbworker::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
    10. Q_DECL_OVERRIDE
    11. {
    12. qDebug("i came here");
    13. return false;
    14. }
    To copy to clipboard, switch view to plain text mode 

    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <usbworker.h>
    6.  
    7. namespace Ui {
    8. class Widget;
    9. }
    10.  
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Widget(QWidget *parent = 0);
    17. ~Widget();
    18. usbworker uw;
    19.  
    20. private:
    21. Ui::Widget *ui;
    22. };
    23.  
    24. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4.  
    5. Widget::Widget(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. Widget::~Widget()
    13. {
    14. delete ui;
    15. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    i am using qt 5.1.0
    Last edited by alee; 2nd October 2013 at 20:14. Reason: updated contents

  2. #2
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Cool Re: help with QAbstractNativeEventFilter

    i have sorted how to do this.....

    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

    Qt Code:
    1. #include "widget.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. a.installNativeEventFilter(&w.uw); //this installs the instance uw of the usbworker inside the widget w, on the application instance a.
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.