PDA

View Full Version : How to send message to Desktop Widget



shyam prasad
17th April 2007, 14:15
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. :confused:
Any help would be appreciated.

Thnx in advance. :)

Shalabh

wysota
17th April 2007, 14:20
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.

marcel
17th April 2007, 14:21
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

high_flyer
17th April 2007, 14:46
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.

shyam prasad
17th April 2007, 15:39
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. :)

marcel
17th April 2007, 15:42
grabWidget() works only for QWdgets.
I guess you'll have to do a little search on MSDN for this one :)

marcel
17th April 2007, 15:46
Actually, try this:
http://www.codeproject.com/tools/WindowScraper_Utility.asp

jpn
17th April 2007, 15:54
QPixmap::grabWindow() takes a native window system identifier (WId).

shyam prasad
17th April 2007, 16:17
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. :)

jpn
17th April 2007, 16:56
"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() (http://tronche.com/gui/x/xlib/input/XGetInputFocus.html), GetActiveWindow() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getactivewindow.asp) and so on..

aamer4yu
18th April 2007, 06:12
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...

shyam prasad
18th April 2007, 08:07
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. :confused:

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............

marcel
18th April 2007, 08:32
And also i don't have any link or documentation like MSDN for Mac and Linux systems.


For mac: developer.apple.com (http://www.qtcentre.org/forum/developer.apple.com)
For linux: the X server api documentation www.x.org (http://www.qtcentre.org/forum/www.x.org)

regards

aamer4yu
18th April 2007, 09:51
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 ?? :confused:
is this function portable or non portable ??

marcel
18th April 2007, 09:54
It is portable! But in principle :)

aamer4yu
18th April 2007, 11:04
OK...
so in principle.. its portable !! :D
:crying: :crying: :confused:

wysota
18th April 2007, 11:15
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.

aamer4yu
18th April 2007, 11:34
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 :D

marcel
18th April 2007, 12:00
I just tested it on Mac and it works... But it captures the entire screen... You cannot choose a window to be captured