Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

Thread: native event of child window

  1. #21
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    I'm a little confused. Are you assuming I've sub-classed QMainWindow so that I can re-implement showEvent() and add to it's .cpp? If so, I haven't. It sounds like this is something I need to do in order to follow your suggestions. Is that correct?

  2. #22
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    Sorry, I re-read your first post and see that you've subclassed QFrame. Put the code in that QFrame subclass instead of subclassing QMainWindow.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #23
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    I tried it starting at the subclassed QFrame so that it gets it and all its children and still the only events I can get are
    23 Focus about to change
    9 Focus out
    8 Focus in
    207 InputMethodQuery
    103 WindowBlocked
    104 WindowUnblocked
    PE obviously has it set up so that something is eating all the events. MFC accesses the events through PreTranslateMessage. Does Qt have an equivalent? I haven't been able to find one.
    From what I understand of Qt, it just doesn't make sense that eventFilter isn't catching it.
    Last edited by TonyInSoMD; 28th December 2016 at 17:50.

  4. #24
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    Huh. About the only other thing I can suggest is to install an event filter using QCoreApplication::installNativeEventFilter(). You'll have to derive a class from QAbstractNativeEventFilter and implement the QAbstractNativeEventFilter::nativeEventFilter() method. Be sure to return "false" from the method, otherwise the messages will be eaten by your filter and never get to the rest of your app.

    If this is successful at trapping messages from the PE window, then you could use this filter to create your own QEvents and post them to the event loop for handling by your QFrame.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    TonyInSoMD (3rd January 2017)

  6. #25
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    Thanks, subclassing QAbstractNativeEventFilter and installNativeEventFilter worked. I doubt that this is what you meant, but this is what I did.
    Subclassing QAbstractNativeEventFilter:
    Qt Code:
    1. #include<QAbstractNativeEventFilter>
    2. #include <QObject>
    3.  
    4. class CCustomEventFilter : public QObject, public QAbstractNativeEventFilter
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. CCustomEventFilter(QObject *parent = 0);
    10. ~CCustomEventFilter();
    11.  
    12. virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
    13.  
    14. signals:
    15. void ShiftKeyDown();
    16. void ShiftKeyUp();
    17. };
    18.  
    19. .cpp file
    20. #include "ccustomeventfilter.h"
    21. #include "windows.h"
    22.  
    23. standard default constructor that doesn't do anything
    24.  
    25. bool CCustomEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
    26. {
    27. if (eventType == "windows_generic_MSG")
    28. {
    29. MSG *pMsg = (MSG*)message;
    30. if (pMsg->message == WM_KEYDOWN)
    31. {
    32. if (pMsg->wParam == VK_SHIFT)
    33. {
    34. emit ShiftKeyDown();
    35. }
    36. }
    37. else if (pMsg->message == WM_KEYUP)
    38. {
    39. if (pMsg->wParam == VK_SHIFT)
    40. {
    41. emit ShiftKeyUp();
    42. }
    43. }
    44. }
    45. return false;
    46. }
    To copy to clipboard, switch view to plain text mode 

    Then in my MainWindow program (CRat);
    Qt Code:
    1. in CRat.h
    2. #include "ccustomeventfilter"
    3.  
    4. CCustomEventFilter *m_pKeyFilter;
    5.  
    6. slots:
    7. void OnKeyShiftDown();
    8. void OnKeyShiftUp();
    9.  
    10. in CRat.cpp
    11.  
    12. in constructor
    13. m_pKeyFilter = new CCustomEventFilter();
    14. qApp->installNativeEventFilter(m_pKeyFilter);
    15. connect(m_pKeyFilter, SIGNAL(ShiftKeyDown()), this, SLOT(OnKeyShiftDown()));
    16. connect(m_pKeyFilter, SIGNAL(ShiftKeyUp()), this, SLOT(OnKeyShiftUp()));
    17.  
    18. in body of code
    19. void CRat::OnKeyShiftDown()
    20. {
    21. set graph to vertical scroll
    22. }
    23.  
    24. void CRat::OnKeyShiftUp()
    25. {
    26. set graph to horizontal scroll
    27. }
    To copy to clipboard, switch view to plain text mode 

    I tried to find examples where a subclass native event filter sent out an event or something that could act on MainWindow objects (in my case, graph windows) but couldn't find any. All the examples I could find just showed how to print out a deBug() line when it hit the event you wanted. That doesn't tell me how to get it to do anything in the program I'm working from. This was the only way I could think of to make it either throw an event or emit a signal that MainWindow could use. It works, but there has GOT to be a prettier way to do it.
    Anyway, without all your help d_stranz I would have never gotten it and I thank you from the bottom of my heart.

    Tony

  7. #26
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    Thanks, subclassing QAbstractNativeEventFilter and installNativeEventFilter worked. I doubt that this is what you meant, but this is what I did.
    No, that's what I did mean, but the implementation was left as an exercise for the reader... Looks like you have been successful at generating proof of concept.

    Shouldn't you be also checking to make sure that the window handle matches that of the PE window (or whichever child window) that is generating (some of) the messages? I would think that your native event filter would be called for -all- windows in your app (Qt and non-Qt) since it is installed at the app level and isn't restricted to any particular window. You wouldn't want the shift key to affect the PE control no matter where it is pressed in your app.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #27
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    Shouldn't you be also checking to make sure that the window handle matches that of the PE window (or whichever child window) that is generating (some of) the messages?
    I think I mentioned in an earlier post that I haven't done any work with the windows API. Thanks for clueing me in on the windows handle. It wasn't until you mentioned it that I got to looking at the MSG struct and saw that it gave you the HWND of the window that generated the message. Now I can double check to make sure it was the PE graph that sent the message. Thanks! I had the signal/slot send/receive the HWND as a parameter and then checked it against the HWND of the PE graph in the MainWindow program (CRat) before making any changes. This thread has been a lot of help to me and I hope it helps some one else in the future that has a similar problem getting messages.

  9. #28
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: native event of child window

    I had the signal/slot send/receive the HWND as a parameter
    I think it would be more efficient to check it in the native filter so you can decide not to send the signal at all instead of sending the signal for every window. Since you have derived a class from QAbstractNativeEventFilter, you can add an HWND as a member variable of that class, set it to the PE control's HWND, and check it first thing when the event comes in. If it doesn't match, then you can immediately return false instead of firing off the signal. If you have more than one of these PE controls in your app, you could make the member variable a collection of HWNDs (a QHash< HWND > maybe).

    This thread has been a lot of help to me and I hope it helps some one else in the future that has a similar problem getting messages.
    Glad to help. I spent years drinking the Windows Koolade before learning about Qt, so it's good to dust off the old brain cells once in a while to remember some of that stuff.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. The following user says thank you to d_stranz for this useful post:

    TonyInSoMD (2nd May 2017)

  11. #29
    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: native event of child window

    Quote Originally Posted by TonyInSoMD View Post
    I tried to find examples where a subclass native event filter sent out an event or something that could act on MainWindow objects (in my case, graph windows) but couldn't find any.
    You can create instances of Qt event classes and use QCoreApplication::postEvent() or QCoreApplication::sendEvent() to send them to a receiver object.

    However using signals as you did looks fine as well and easier in this case.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 9th August 2016, 16:37
  2. Native parent window for QWidget (IPreviewHandler)
    By Kevsoft in forum Qt Programming
    Replies: 3
    Last Post: 19th July 2015, 13:24
  3. Replies: 5
    Last Post: 17th September 2013, 06:45
  4. Replies: 11
    Last Post: 4th June 2008, 07:22

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.