How to detect a Windows event in Qt? - c++

I am trying to build an app to detect Windows events, in particular events related to multimedia (playing video, playing audio and images).
For instance, if Windows Media Player is opened, the related event should be detected.

There is no 'events' for that.
You can detect the lauches of media players (by winapi ::FindWindow) or image viewers.

I don't think it's possible to do this with QT's built in functions alone. You'll have to use the Windows API. Depending on what you actually want to do this can get quite complicated.
If you just want to check if a certain application has been started yet, you could use the FindWindow function. I'd suggest to use a qt timer to create signals that you can use to check if the Window has opened yet.
QTimer::singleShot(200, this, SLOT(checkForMediaPlayer()));
Just add this to your QObject along with the checkForMediaPlayer member function that'll do whatever you want once the MediaPlayer has been detected.

Related

Need Qt QPA and X11 clarification

I tried to understand Qts platform handling for hours now, but I dont get it. For my hotkey handling I currently use a mixture of X11extras for getting display, xlib for key conversions and xcb for the Qt native event handling. Three libs to link. And where does the undocumented QPA play a role here? Now I wonder if this is necessary. I need some clarification here. I am using qt 5.4. What is the way to go for the future?
For Qt, you shouldn't need to do any native platform coding for key events unless you're using native windows. Read about QEvent and the event functions in QWidget.
Use QWidget::nativeEvent() or QCoreApplication::installNativeEventFilter() if you need direct access to raw X11 events. Native event filters at the application level are processed for every event and have more of a performance impact than subclassing QWidget and reimplementing nativeEvent().
Read about QWindow::fromWinId() and QWidget::createWindowContainer() if you need to embed a native window as a child widget.

How to know the name/ID of window in focus

How to know the name/ID of window in focus specifically in OpenCV.
Is there any event handling/callback or windows api that make this possible if it is not possible with OpenCV.
I am working on Windows 7 and 8.1.
I want to do something similar to (but not limited to):
If a window is in focus, and some event like mouse or key press happens,
then update that particular window
.
As suggested by Kris, there exists a solution on window. Use windows api to take handle of active window as using:
HWND WINAPI GetActiveWindow(void);
Now use GetWindowText() function to extract out title, if any. It may not be portable. There should exist similar api for other OS.
first of all, I don't know if windows or any other api can help.
I have an idea to take screen-shot, somehow, and then use opencv itself for image processing. Assumption is that the focused window will be on the top and most focused so we can see the name easily. We can process color to separate focused window from others, if any. And using OCR to extract windows name.

QT How to embed an application into QT widget

In our project we have three independent applications, and we have to develop a QT control application that controls these three applications. The main window will be seperated to three sub windows - each one display another one application.
I thought to use QX11EmbedWidget and QX11EmbedContainer widgets, but two problems with that:
The QX11Embed* is based on X11 protocol and I dont know if it's supported on non-x11 systems like Windows OS.
Since QT 5 these classes are not existing, and the QT documentation doesn't mention why.
So that I dont know whether to use it or not - I'll be happy to get an answers.
In addition, I see that the QT 5.1 contains QWidget::createWindowContainer(); function that in some posts it looks like this should be the replacement to the X11Embed. Can anyone please explian me more how can I use this function to create a QT widget that will run another application (a Calculator for example) inside its?
I have searched a lot in Google, and didn't find answers to my Qs.
Can anyone please help me? Am I on the right way?
Thanks!
If all three independent applications are written with Qt, and you have their source, you should be able to unify them just through the parenting of GUI objects in Qt.
http://qt-project.org/doc/qt-4.8/objecttrees.html
http://qt-project.org/doc/qt-4.8/widgets-and-layouts.html
http://qt-project.org/doc/qt-4.8/mainwindows-mdi.html
If you don't have access to them in that way, what you are talking about is like 3rd party window management. It is kind of like writing a shell, like Windows Explorer, that manipulates the state and the size of other window applications.
Use a program like Spy++ or AutoIt Spy for Windows and the similar ones for other OS's, and learn the identifying markings of your windows you want to control, like the class, the window title, etc. Or you can launch the exe yourself in a QProcess::startDetached() sort of thing.
http://qt-project.org/doc/qt-5.1/qtcore/qprocess.html#startDetached
Then using the OS dependent calls control the windows. The Qt library doesn't have this stuff built in for third party windows, only for ones under the QApplication that you launched. There are a lot of examples of doing things like this by AutoHotKey, or AHK. It is a scripting language that is made for automating a lot of things in the windows environment, and there is port for Mac as well (though I haven't tried the mac port myself).
So in the end you are looking at finding your window probably with a call like this:
#include <windows.h>
HWND hwnd_1 = ::FindWindow("Window_Class", "Window Name");
LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE); // to query the state of the window
Then manipulate the position and state of the window like so:
::MoveWindow(hwnd_1, x, y, width, height, TRUE);
::ShowWindow(hwnd_1, SW_SHOWMAXIMIZED);
You can even draw widgets on top of the windows you are controlling if you set your window flags correctly for the windows you are manipulating.
transparent QLabel with a pixmap
Cannot get QSystemTrayIcon to work correctly with activation reason
Some gotchas that come up in Windows when doing all of this, is finding out the quirks of the Windows UI when they set the Display scaling different from what you expect, and if you want to play nice with the Task bar, and handling all the modal windows of your programs you are manipulating.
So overall, it is do-able. Qt will make a nice interface for performing these commands, but in the end you are looking at a lot of work and debugging to get it in a beautiful, reliable, window manager.
Hope that helps.
I never tried it myself, but from the docs in Qt 5.1 I would try QWindow::fromId(WId id), which gives you a QWindow, which should be embeddable with createWindowContainer:
QWindow * QWindow::fromWinId(WId id) [static] Creates a local
representation of a window created by another process or by using
native libraries below Qt.
Given the handle id to a native window, this method creates a QWindow
object which can be used to represent the window when invoking methods
like setParent() and setTransientParent(). This can be used, on
platforms which support it, to embed a window inside a container or to
make a window stick on top of a window created by another process.
But no guarantee. :-)

Popup notifiers in C/C++

I've been working on a project that will need a notifier in the system tray (sorry, "System Notification Area"). It will be a simple app that just generates popup notifications when it receives a message via a Zeromq socket.
I am not having any luck finding anything other than .NET resources and examples. Does anyone have a sample in C/C++?
I would start with this section of MSDN: Notifications and the Notification Area.
Then I'd check the NotificationIcon Sample in the Windows SDK.
What framework are you using? There should probably be several implementations for MFC, but there might different implementation for WTL and other frameworks. If you want to use the Windows API with no object orientation - well, you won't need any wrapper library then, but you can look at these libraries for example.
Here's one that has MFC and non-MFC version from CodeProject:
http://www.codeproject.com/KB/shell/systemtray.aspx
What you want here is probably ShowBalloon() function, which shows a balloon notification, but I'm pretty sure you must create a tray icon for that (can't have a notification balloon without having a tray icon).

Passing the "enter key" event to flash player in an ATL Window?

I have a Flash player (flash9.ocx) embedded in an ATL window and have coded functionality into the swf to respond to the return/enter key being pressed.
Works fine from the standalone swf player but as soon as its played from within my embedded player it doesn't execute. It's as if my window is getting in the way somehow?
Is there any way to pass the keypress through to the player?
FYI, there isn't anything to weird in place on the form.
Thanks!
I'm not VC++ developer, but I use Flash a lot.
Though not sure, it seems that the embedded player doesn't have the focus. Make sure you've got this part covered on the Flash side of things:
the stage exists ( you movie is properly initialized)
you set the KeyboardEvent listener to the stage.
You could use the FocusManager to make sure you've got the focus.
I don't know if you can pass the focus from you app to the SWF OLE through some tabIndex or something.
If still this doesn't work you can try using the External Interface to add callbacks from your app to flash player ( basically call and actionscript function from your app ).
This was achieved through fscommand before, but External Interface seems to be the thing to use now.
Good luck!
I don't use flash a lot but i'm a C++ programmer. =)
Let's see if I can help you.
I believe that your application is catching all the events before your flash movie. I don't know if there is a better way to do this but you could listen for any keyboard event on your form and use the SetVariable of your ActiveX component to set a variable inside Flash. Then, in the Flash Movie you could set a watch for any changes on this variable and trigger your Enter event.
Hope this helps.