Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to implement nativeEvent() in my library?
Qt 6.12 LTS is out! See what's new in the release blog

How to implement nativeEvent() in my library?

Scheduled Pinned Locked Moved Solved Qt 6
8 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wunian
    wrote on last edited by
    #1

    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.

    Christian EhrlicherC 1 Reply Last reply
    0
    • W Wunian

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @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/

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      2
      • W Wunian

        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.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Do you have a running event loop in your app? Do you instantiate your object which should catch the events properly?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        W 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Do you have a running event loop in your app? Do you instantiate your object which should catch the events properly?

          W Offline
          W Offline
          Wunian
          wrote on last edited by
          #3

          @Christian-Ehrlicher
          Thanks for your reply.
          Do you mean, use QEventLoop in my library, and catch WM_DEVICECHANGE ?
          Have a nice day.

          Christian EhrlicherC 1 Reply Last reply
          0
          • W Wunian

            @Christian-Ehrlicher
            Thanks for your reply.
            Do you mean, use QEventLoop in my library, and catch WM_DEVICECHANGE ?
            Have a nice day.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You need a running Q(Core)Application.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            W 2 Replies Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              You need a running Q(Core)Application.

              W Offline
              W Offline
              Wunian
              wrote on last edited by
              #5

              @Christian-Ehrlicher
              Thanks for your suggestion.
              I will try it, and let you know results later.
              Have a nice day.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                You need a running Q(Core)Application.

                W Offline
                W Offline
                Wunian
                wrote on last edited by
                #6

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

                jsulmJ 1 Reply Last reply
                0
                • W Wunian

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @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/

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  W 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @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/

                    W Offline
                    W Offline
                    Wunian
                    wrote on last edited by
                    #8

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

                    1 Reply Last reply
                    0
                    • W Wunian has marked this topic as solved on

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt
                    • Unsolved