How to implement nativeEvent() in my library?
-
Hi,
I create a C++ library with Qt6, and I try to use nativeEvent() in my library to detect WM_DEVICECHANGE,
but it's failed. I found nothing triggers nativeEvent() in my library.
If I create an application with Qt6, and use nativeEvent() in my application,
nativeEvent() can be trigged, and it can receive WM_DEVICECHANGE.My question is, how can I implement nativeEvent() in my library, and work successfully?
Any advice will be kindly appreciated. -
@Wunian said in How to implement nativeEvent() in my library?:
int argc = 0;
You're passing one argument, so argc must be 1.
If your app is crashing run through debugger and check stack trace to see why it is crashing.
Also, sprintf(argv[0], "%s", c_str); is wrong - argv contains one pointer pointing to nowhere. Please check https://cplusplus.com/reference/cstdio/sprintf/ -
Do you have a running event loop in your app? Do you instantiate your object which should catch the events properly?
-
@Christian-Ehrlicher
Thanks for your reply.
Do you mean, use QEventLoop in my library, and catch WM_DEVICECHANGE ?
Have a nice day. -
You need a running Q(Core)Application.
-
@Christian-Ehrlicher
Thanks for your suggestion.
I will try it, and let you know results later.
Have a nice day. -
@Christian-Ehrlicher
Hi,
My code in my library is below,// refer to https://doc.qt.io/qt-6/qapplication.html#details
int argc = 0;
char* argv[1];
char c_str[] = { "-no-gui" };
sprintf(argv[0], "%s", c_str);
pApp = new QCoreApplication(argc, argv);I create a Qthread in my lib, and in run(), I call pApp->exec();
but when pApp->exec() is calling, my program is crashed, no matter QT += gui or QT -= gui in my lib pro file.On the other hand, I can not override nativeEvent() in my library.
// refer to https://doc.qt.io/qt-6/qthread.html#exec
I try another way in my lib as below,
create a Qthread, and in run(), I call thread's exec(),
and call thread's exit() when my thread stops.
My program is working fine, but I can not receive WM_DEVICECHANGE due to nativeEvent() can not be overrided.May I know if I misunderstand something?
Thanks. -
@Wunian said in How to implement nativeEvent() in my library?:
int argc = 0;
You're passing one argument, so argc must be 1.
If your app is crashing run through debugger and check stack trace to see why it is crashing.
Also, sprintf(argv[0], "%s", c_str); is wrong - argv contains one pointer pointing to nowhere. Please check https://cplusplus.com/reference/cstdio/sprintf/ -
@jsulm
Hi,
Thanks for your reply and help.
I found a solution like this, https://copyprogramming.com/howto/detected-new-usb-device-connected-disconnected-on-qt
I made a class and when I plug/unplug my USB camera, there can receive WM_DEVICECHANGE in my class.
I will also try what I mentioned in my library.
Have a nice day. -