Detec U-Disk Insert and Remove
-
@Zuobai
Assuming this code, whatever it is, is what you want to detect the situation, remove anything in it other than the core detection of the insertion/removal of the disk and then use that. You do not need aQWidget
or any UI for that, and anythingQWidget
is not compatible with a QML application. -
@JoeCFD
Since the OP is stuck on"QWidget: Cannot create a QWidget without QApplication".
would it not make sense for them to look at the code and remove whatever is there for a widget, presumably just to display some result, given they are in a QML application? I should be surprised if wanting to detect "the insertion and removal of the drive letter" required any kind ofQWidget
, it just seems an unnecessary thing to try to support. -
@JonB
Thank you for your reply,In order to better discuss this issue, I will attach the code that I have processed here, I removed all the interface code, only keeping the core part that identifies the USB flash drive insertion, and then there is the situation I mentioned earlier, "QWidget: Cannot create a QWidget without QApplication" because I am rewriting a function that inherits from "QWidget" and my question is how I can integrate this code into my QML interface project.// Udisk.Cpp char UDISKScanner::FirstDriveFromUDisk(ulong unitmask) { char i; for( i = 0 ; i < 26 ; i++) { if( unitmask & 0x1) { break; } unitmask = unitmask >> 1; } return (i+'A'); } bool UDISKScanner::nativeEventFilter(const QByteArray &eventType, void *message, long *result) { MSG* msg = reinterpret_cast<MSG*>(message); int msgType = msg->message; if (msgType == WM_DEVICECHANGE) { PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam; switch (msg->wParam) { case DBT_DEVICEARRIVAL: if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; if (lpdbv->dbcv_flags == 0) { QString strUSBDisk = QString(this->FirstDriveFromUDisk(lpdbv->dbcv_unitmask)); emit sigUDiskCome(strUSBDisk); } } break; case DBT_DEVICEREMOVECOMPLETE: if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME) { PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb; if (lpdbv->dbcv_flags == 0) { QString strUSBDisk = QString(this->FirstDriveFromUDisk(lpdbv->dbcv_unitmask)); emit sigUDiskRemove(strUSBDisk); } } break; case DBT_DEVNODES_CHANGED: break; default: break; } } return QWidget::nativeEvent(eventType, message, result); }
-
nativeEvent is indeed a QWidget based virtual function and not part of QObject. so you're out of luck on the direct translation to QML
but, my guess is a NativeEventFilter on your QCoreApplication should do the trick.
-
@J-Hilk
Thank you for your reply,
Indeed, after I discovered that the "nativeEvent" inherits from the "QWidget", I tried something else, as you said, using the "NativeEventFilter" that inherits from the "QCoreApplication ", but the problem remained. How can I modify and adjust it