[Qt-interest] Detection of new drives

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Mar 17 16:41:09 CET 2009


Tiago Correia wrote on Tuesday, March 17, 2009 3:23 PM:

> I've made a small experiment and I extended QApplication (it could be
> QCoreApplication) and implemented just 2 methods. I just made it
> possible to check when a USB or storage device is plug in the
> computer. The only problem I found now, but didn't bother to solve is
> that it message is received 3 times    

Hi,

I just quickly browsed your code, and it looks very similar to mine which I came up with end of last year, when I brought up the same question (and got nice answers on this list - thanks ;). In fact, I also observed that sometimes I get multiple "Device arrived" events from Windows - I don't have a solution for that either.

Since someone just asked me in private very recently, referring to that qt-interest discussion, I attach the source code of my  'MediaInfo'  utility class here once again :)

The basic usage is as follows: In your QMainWindow derived class:

#ifdef Q_OS_WIN
bool TheMainWindow::winEvent(MSG *message, long *result)
{
	Q_UNUSED(result);

	// forward the event to the MediaInfo filter
	return MediaInfo::getInstance().filterMessage(message);
}
#endif

This is the only known drawback of my solution: obviously the only place (given my solution) I succesfully receive the win32 events is in the QMainWindow derived class, in the winEvent() method.

Actually I register the window handle of the QApplication::activeWindow() which might be a bit dangerous, since it might be another window  than the QMainWindow derived one (see MediaInfo::connectNotify in MediaInfo_Win32.cpp) - but in practice I found out that this seems to work pretty well for me. 

I wasn't able to receive these events anywhere else (for example in my own 'MediaInfo' class, so the exisiting application code wouldn't need to be changed at all). But since this is only a small change to the QMainWindow derived class I can live with that. 

Since Tiago was able to do the same with a QCoreApplication based application I will have a look at his solution where he receives these events :)


The rest is pretty straight-forward: whoever is interested connects to the signals

  MediaInfo::deviceArrived(), MediaInfo::deviceRemoved()

which also pass along the path on which the device has been mapped/removed from. The 'MediaInfo' has an internal "signal connect/disconnect" counter and will "register"/"unregister" with the Win32 API (registerDeviceInterfaceToHwnd(), UnregisterDeviceNotification()). That's it.

My own usage scenario is a "Find photo" dialog: Whenever it pops up (because a "photo" could not be found with the given path) I connect to the "deviceArrived" signal, and as long as this file chooser dialog is open I react to USB arrival events, in that I try to find the photo on that medium (USB stick, CD-ROM, ...). If found (and succesfully matched the "photo criterias" such as image size, embedded thumbnail etc.) I automatically close the "choose file path" dialog and continue with the "photo loading process" (it is all in the context of yet another album generator, as you might have guessed by now :)

Comments and "success stories" about my "MediaInfo" class welcome :)


Note that the "Unix" part is not yet implemented, but I understand that at least on Linux the most comfortable way to go is with D-Bus, by connecting to the "system" bus. Anyone has experience with that? Would that also work on the MacOSX (assuming that DBus works there also)? And who knows, maybe the DBus Windows port would also work :) Anyone did that on Windows with DBus? To my knowledge the "Windows port" is still not merged into the main DBus branch, right?


Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MediaInfo_Win32.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090317/5b3b0044/attachment.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MediaInfo.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090317/5b3b0044/attachment-0001.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MediaInfo.h
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090317/5b3b0044/attachment.h 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MediaInfo_Unix.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090317/5b3b0044/attachment-0002.pl 


More information about the Qt-interest-old mailing list