Results 1 to 19 of 19

Thread: How to send message to Desktop Widget

  1. #1
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question How to send message to Desktop Widget

    Hi all,
    I want to send a KeyDown message to the desktop widget which i got through QApplication::desktop(). Here is the code snippet:



    // Also tried with - Qt::Key_SysReq

    QKeyEvent keydownEvent(QEvent::KeyPress, Qt::Key_Print, Qt::AltModifier);
    QDesktopWidget * desktopWidget = qApp->desktop();

    qApp->sendEvent(desktopWidget, &keydownEvent);

    bool bDownAccepted = keydownEvent.isAccepted();
    QKeyEvent keyupEvent(QEvent::KeyRelease, Qt::Key_Print, Qt::AltModifier);
    QApplication::sendEvent(desktopWidget, &keyupEvent);
    bool bUpAccepted = keyupEvent.isAccepted();



    Here my assumption was that this code should send an "Alt + PrintScreen" button press to the desktop widget which in effect should dump the "active window image" to the ClipBoard. But it is not happening in my case.
    There is nothing copied to the ClipBoard and also bDownAccepted , bUpAccepted value is always false which means that DesktopWidget is not accepting the event.


    My aim is just to capture the image of active window on the screen using QT.

    can anybody tell whats wrong with the above code.
    Any help would be appreciated.

    Thnx in advance.

    Shalabh

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to send message to Desktop Widget

    Quote Originally Posted by shyam prasad View Post
    My aim is just to capture the image of active window on the screen using QT.
    QPixmap::grabWidget()
    If you need a generic Windows capturer, use its native API without any hacks with keyboard shortcuts. Qt won't help you with that - Qt events are different than native WinAPI events.

  3. The following user says thank you to wysota for this useful post:

    shyam prasad (17th April 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    Try using postEvent instead of sendEvent. But now you won't be able to see if it was accepted. Also you need to create the events on the heap.

    Regards

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

    shyam prasad (17th April 2007)

  6. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    Here my assumption was that this code should send an "Alt + PrintScreen" button press to the desktop widget which in effect should dump the "active window image" to the ClipBoard.
    In addition to what wysota said -
    I don't think this assumption of yours is correct.
    I would be surprized if the desktop it self was resposible for reacting to the key combination - and in this case dump a screen shot to the clip board.
    I would expect it to be done by the windowing system, not by a window.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    shyam prasad (17th April 2007)

  8. #5
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: How to send message to Desktop Widget

    Hi All,
    Thanx a lot to all of you for such a quick response.
    QPixmap::grabWidget()

    is not going to help me out since QT never gives me ID of windows which are not QT's widget except desktop widget.

    Suppose if i want to take snap shot of notepad opened on my system then how it can be done using grabWidget() or grabWindow()???
    I tried them but it doesn't works for me.
    If you know how can i take snapshot of notepad or any app then please tell me
    with some descriptive code.

    Well it might be possible that my assumption are not right then anybody here knows that how can i achieve ii using QT's cross platform library.

    Any help would be appreciated.
    Thnx in advance.

  9. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    grabWidget() works only for QWdgets.
    I guess you'll have to do a little search on MSDN for this one

  10. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget


  11. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to send message to Desktop Widget

    QPixmap::grabWindow() takes a native window system identifier (WId).
    J-P Nurmi

  12. #9
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: How to send message to Desktop Widget

    Hi,
    Thnx Marcel and Jpn for your valuable suggestions.
    But the thing is that i want use this capture screen technology on MAC, Linux, and Windows.
    So i thought that QT have some support for it but i haven't found anything useful yet.

    "Jpn" although grabWindow() can work for me but the whole thing is that how can i know in my QT app that which is active window and how do i get winId of the active window.

    And "Marcel" the link you had given me is a useful one i want to use my app across many platforms including MAC, Linux and Windows.



    Thnx again for your kind help.

  13. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to send message to Desktop Widget

    Quote Originally Posted by shyam prasad View Post
    "Jpn" although grabWindow() can work for me but the whole thing is that how can i know in my QT app that which is active window and how do i get winId of the active window.
    I'm afraid you need a platform dependent solution for that.

    Something like XGetInputFocus(), GetActiveWindow() and so on..
    J-P Nurmi

  14. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    Did u happen to see the Screen capture example in the Qt demos ?? U can find it under Desktop->Screen capture in the demo....

    it uses something like...
    originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    am not sure how portable is this.

    hope this helps...
    Last edited by aamer4yu; 18th April 2007 at 06:21.

  15. #12
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: How to send message to Desktop Widget

    Hi,
    Thanks to all for your valuable suggestions.
    I know that how it can be done using Windows APIs.
    But i haven't done anything in MAC or Linux.
    So i don't know how to implement function which can capture active window.
    I am not aware of MAC and Linux specific APIs.
    And also i don't have any link or documentation like MSDN for Mac and Linux systems.

    If anybody have any idea how can i write this function for Mac and Linux then please help me out.

    In Window GetWindowFromPoint() gives me the handle of window which i clicked using mouse. and then everything is easy. But i don't know how i can do this using MAC and Linux specific APIs.

    Also in my first post i asked whom i should sent this "Alt + PrtScn" message so that it will just dump the Active Window image to the clipboard. I think if i can do this then it will ease my work.

    Or can anyone show me the code snippet to capture active window on Mac and Linux, especially Mac OS.

    Waiting for valuable suggestions from all of you. Thanks for your interest. I think there are many people who wants to do the same thing.

    Thanks in advance.
    Shalabh............

  16. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    And also i don't have any link or documentation like MSDN for Mac and Linux systems.
    For mac: developer.apple.com
    For linux: the X server api documentation www.x.org

    regards

  17. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    from Qt docs...
    WId QWidget::winId () const
    Returns the window system identifier of the widget.
    Portable in principle, but if you use it you are probably about to do something non-portable. Be careful.
    what does it mean by portable in principle ??
    is this function portable or non portable ??

  18. #15
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    It is portable! But in principle

  19. #16
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    OK...
    so in principle.. its portable !!

  20. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to send message to Desktop Widget

    It means that it is portable by itself, but if you use it, you are doing something which is not portable, so portability of the winId() method is not important as it is surrounded by an unportable code.

  21. #18
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    hmm... understood somewhat,,,
    tell me one thing more...
    will the code of screnshot example of the Qt demo run on Windows, Linux, and Mac ?? i may understand better by this answer

  22. #19
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send message to Desktop Widget

    I just tested it on Mac and it works... But it captures the entire screen... You cannot choose a window to be captured

Similar Threads

  1. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  2. Replies: 4
    Last Post: 10th March 2007, 18:01
  3. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  4. Replies: 4
    Last Post: 24th March 2006, 22:50
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.