Results 1 to 4 of 4

Thread: deactivate hook with qobject

  1. #1
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default deactivate hook with qobject

    Hi i'm connecting to my external class with qobject to check mouse event's . But when i try to deactivate the hook with emit instance().event(); doesnt seem's to work the problem seem this is not call signal. Obviously when i do this:
    Qt Code:
    1. mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
    2. UnhookWindowsHookEx(mouseHook);
    To copy to clipboard, switch view to plain text mode 

    But my problem is when i do qobject::connect(..instance..signal). The signal never is called?
    Code:
    main.cpp:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include "mouselogger.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
    10. [](){
    11.  
    12. });
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 


    mouse.cpp:

    Qt Code:
    1. #include "mouselogger.h"
    2. #include <QDebug>
    3.  
    4. MouseLogger &MouseLogger::instance()
    5. {
    6. static MouseLogger _instance;
    7. return _instance;
    8. }
    9.  
    10. MouseLogger::MouseLogger(QObject *parent) : QObject(parent)
    11. {
    12. HINSTANCE hInstance = GetModuleHandle(NULL);
    13. // Set hook
    14. //mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
    15. // Check hook is correctly
    16. //if (mouseHook == NULL) {
    17. // qWarning() << "Mouse Hook failed";
    18. //}
    19. }
    20.  
    21. LRESULT CALLBACK MouseLogger::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
    22. {
    23. Q_UNUSED(Code)
    24.  
    25. // Having an event hook, we nned to cast argument lParam
    26. // to the structure of the mouse is the hook.
    27. MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
    28.  
    29. // Next, we check to see what kind of event occurred,
    30. // if the structure is not a pointer to nullptr
    31. if(pMouseStruct != nullptr) {
    32. switch (wParam) {
    33. case WM_MOUSEMOVE:
    34. qDebug() << "WM_MOUSEMOVE";
    35. break;
    36. case WM_LBUTTONDOWN:
    37. qDebug() << "WM_LBUTTONDOWN";
    38. break;
    39. case WM_LBUTTONUP:
    40. qDebug() << "WM_LBUTTONUP";
    41. break;
    42. case WM_RBUTTONDOWN:
    43. qDebug() << "WM_RBUTTONDOWN";
    44. break;
    45. case WM_RBUTTONUP:
    46. qDebug() << "WM_RBUTTONUP";
    47. break;
    48. case WM_MBUTTONDOWN:
    49. qDebug() << "WM_MBUTTONDOWN";
    50. break;
    51. case WM_MBUTTONUP:
    52. qDebug() << "WM_MBUTTONUP";
    53. break;
    54. case WM_MOUSEWHEEL:
    55. qDebug() << "WM_MOUSEWHEEL";
    56. break;
    57. default:
    58. break;
    59. }
    60. emit instance().mouseEvent();
    61.  
    62. }
    63.  
    64. // After that you need to return back to the chain hook event handlers
    65. return CallNextHookEx(NULL, Code, wParam, lParam);
    66. }
    To copy to clipboard, switch view to plain text mode 

    mouse.h:

    Qt Code:
    1. #ifndef MOUSELOGGER_H
    2. #define MOUSELOGGER_H
    3.  
    4. #include <QObject>
    5. #include <windows.h>
    6.  
    7. class MouseLogger : public QObject
    8. {
    9. Q_OBJECT
    10. Q_DISABLE_COPY(MouseLogger)
    11. public:
    12. static MouseLogger &instance();
    13. explicit MouseLogger(QObject *parent = nullptr);
    14. virtual ~MouseLogger(){}
    15.  
    16. // Static method that will act as a callback-function
    17. static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);
    18. signals:
    19. // The signal, which will report the occurrence of an event
    20. void mouseEvent();
    21. void Eventos();
    22.  
    23. public slots:
    24.  
    25.  
    26. private:
    27. // hook handler
    28. HHOOK mouseHook;
    29. };
    30.  
    31. #endif // MOUSELOGGER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deactivate hook with qobject

    So, what exactly is the problem you are facing?

    Can't be the processing of the signal as your lambda is empty.

    What is your reason for the native event handling code?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: deactivate hook with qobject

    ok solved one question qobject it's only for connect two objects but i can't call my signal with qobject::connect...? sorry again!
    Last edited by SirJonas; 24th October 2016 at 14:22.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: deactivate hook with qobject

    Quote Originally Posted by SirJonas View Post
    ok solved one question
    Since you haven't even asked a question you are now at -1 open questions.

    [QUOTE=SirJonas;294805]
    qobject it's only for connect two objects[quote]
    You can connect any number of QObject with signal/slot connections, two is one of the many cases.

    Quote Originally Posted by SirJonas View Post
    i can't call my signal with qobject::connect...? sorry again!
    Connections have nothing to do with emitting a signal.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 15th October 2012, 13:14
  2. Replies: 6
    Last Post: 10th October 2011, 11:34
  3. hook resizeEvent
    By prashant in forum Qt Programming
    Replies: 6
    Last Post: 7th September 2009, 11:35
  4. How to deactivate the marking in a QTable?
    By mantra in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2008, 15:51
  5. hook
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 29th August 2006, 19:53

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.