Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Windows Message Loop and Qt Application Exec?
Forum Updated to NodeBB v4.3 + New Features

How to use Windows Message Loop and Qt Application Exec?

Scheduled Pinned Locked Moved General and Desktop
23 Posts 7 Posters 14.0k Views 3 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.
  • Y YJCN
    19 Sept 2018, 13:02

    @arablau I meet the same problem. Could you please tell me how to solve it?

    A Offline
    A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on 19 Sept 2018, 13:42 last edited by
    #4

    Hi @YJCN,

    have you read the (still existing, wow!) links @SGaist recommended?

    You might be interested by QWidget's "winEvent":http://qt-project.org/doc/qt-4.8/qwidget.html#winEvent or QCoreApplication's "winEventFilter":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#winEventFilter

    Regards

    Qt has to stay free or it will die.

    Y 1 Reply Last reply 20 Sept 2018, 03:38
    3
    • A aha_1980
      19 Sept 2018, 13:42

      Hi @YJCN,

      have you read the (still existing, wow!) links @SGaist recommended?

      You might be interested by QWidget's "winEvent":http://qt-project.org/doc/qt-4.8/qwidget.html#winEvent or QCoreApplication's "winEventFilter":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#winEventFilter

      Regards

      Y Offline
      Y Offline
      YJCN
      wrote on 20 Sept 2018, 03:38 last edited by
      #5

      @aha_1980 Thanks for your reply. I have read the links. In Qt5, the winEvent has been changed to nativeEvent. This is my code.But it doesn't work.

      
      bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
      {
          //Q_UNUSED(eventType);
          MSG* msg = reinterpret_cast<MSG*>(message);
          cout<<"in nativeEvent:"<<msg->message<<endl;
          if(msg->message==275)
          {
              //GetMessage(msg, NULL, NULL, NULL);
              TranslateMessage(msg);
              cout << "TranslateMessage" << endl;
              DispatchMessage(msg); // Ends up calling objectEventFnc
              cout << "DispatchMessage" << endl;
              return true;
          }
              return false;
      }
      
      J 1 Reply Last reply 20 Sept 2018, 04:19
      0
      • Y YJCN
        20 Sept 2018, 03:38

        @aha_1980 Thanks for your reply. I have read the links. In Qt5, the winEvent has been changed to nativeEvent. This is my code.But it doesn't work.

        
        bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
        {
            //Q_UNUSED(eventType);
            MSG* msg = reinterpret_cast<MSG*>(message);
            cout<<"in nativeEvent:"<<msg->message<<endl;
            if(msg->message==275)
            {
                //GetMessage(msg, NULL, NULL, NULL);
                TranslateMessage(msg);
                cout << "TranslateMessage" << endl;
                DispatchMessage(msg); // Ends up calling objectEventFnc
                cout << "DispatchMessage" << endl;
                return true;
            }
                return false;
        }
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 20 Sept 2018, 04:19 last edited by jsulm
        #6

        @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

        But it doesn't work

        What exactly doesn't work?
        "Note: Events are only delivered to this event handler if the widget is has a native Window handle." from http://doc.qt.io/qt-5/qwidget.html#nativeEvent

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

        Y 1 Reply Last reply 20 Sept 2018, 06:29
        2
        • J jsulm
          20 Sept 2018, 04:19

          @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

          But it doesn't work

          What exactly doesn't work?
          "Note: Events are only delivered to this event handler if the widget is has a native Window handle." from http://doc.qt.io/qt-5/qwidget.html#nativeEvent

          Y Offline
          Y Offline
          YJCN
          wrote on 20 Sept 2018, 06:29 last edited by
          #7

          @jsulm I use the Canon EDSDK to control the EOS 800D. I want to download image from camera. after taking picture. In EDSDK, the download is handled by callback function. I test in command line programm ,and there must be this code after sending a take picture command. Otherwise, the programm can't enter the callback funtion.

          MSG msg = { };
          while (GetMessage(&msg, NULL, 0, 0))
          {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
          }
          

          There is the reason I find in Google.

          If you're using the EDSDK on Windows, you have to have a Windows message loop in your main thread, otherwise callbacks won't happen. (This is because the EDSDK uses the obsolete COM STA threading model instead of real threads.)
          

          https://social.microsoft.com/Forums/en-US/a431209c-be99-4394-88b3-59879cb790a4/objecteventhandler-callback-nevercalled?forum=Offtopic
          https://stackoverflow.com/questions/16839640/edsdk-callbacks-not-working/18624865

          M 1 Reply Last reply 20 Sept 2018, 06:46
          0
          • Y YJCN
            20 Sept 2018, 06:29

            @jsulm I use the Canon EDSDK to control the EOS 800D. I want to download image from camera. after taking picture. In EDSDK, the download is handled by callback function. I test in command line programm ,and there must be this code after sending a take picture command. Otherwise, the programm can't enter the callback funtion.

            MSG msg = { };
            while (GetMessage(&msg, NULL, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            

            There is the reason I find in Google.

            If you're using the EDSDK on Windows, you have to have a Windows message loop in your main thread, otherwise callbacks won't happen. (This is because the EDSDK uses the obsolete COM STA threading model instead of real threads.)
            

            https://social.microsoft.com/Forums/en-US/a431209c-be99-4394-88b3-59879cb790a4/objecteventhandler-callback-nevercalled?forum=Offtopic
            https://stackoverflow.com/questions/16839640/edsdk-callbacks-not-working/18624865

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 20 Sept 2018, 06:46 last edited by
            #8

            @YJCN
            Hi
            in case that nativeEvent simply cannot work with decade-old COM crap (1998)
            there is always the options of using a plain c++ app that will handle the downloading / save to file and
            you simply call it with QProcess from Qt GUI app.

            Y 1 Reply Last reply 20 Sept 2018, 06:54
            3
            • M mrjj
              20 Sept 2018, 06:46

              @YJCN
              Hi
              in case that nativeEvent simply cannot work with decade-old COM crap (1998)
              there is always the options of using a plain c++ app that will handle the downloading / save to file and
              you simply call it with QProcess from Qt GUI app.

              Y Offline
              Y Offline
              YJCN
              wrote on 20 Sept 2018, 06:54 last edited by
              #9

              @mrjj The Canon 's demo is using MFC. Why MFC could use?

              J M 2 Replies Last reply 20 Sept 2018, 07:10
              0
              • Y YJCN
                20 Sept 2018, 06:54

                @mrjj The Canon 's demo is using MFC. Why MFC could use?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 20 Sept 2018, 07:10 last edited by
                #10

                @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

                Why MFC could use?

                Because it is Microsofts own GUI framework using the Windows event loop...

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

                1 Reply Last reply
                2
                • Y YJCN
                  20 Sept 2018, 06:54

                  @mrjj The Canon 's demo is using MFC. Why MFC could use?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 20 Sept 2018, 07:20 last edited by
                  #11

                  @YJCN
                  MFC is very old too :)

                  Anyway i wonder with that line

                  if(msg->message==275)

                  means ? where did u get this magic number from ?

                  Y 1 Reply Last reply 20 Sept 2018, 07:40
                  0
                  • M mrjj
                    20 Sept 2018, 07:20

                    @YJCN
                    MFC is very old too :)

                    Anyway i wonder with that line

                    if(msg->message==275)

                    means ? where did u get this magic number from ?

                    Y Offline
                    Y Offline
                    YJCN
                    wrote on 20 Sept 2018, 07:40 last edited by
                    #12

                    @mrjj
                    This my CMD programm code

                    int main (){       
                            EdsCameraRef camera;
                    	EdsUInt32 tv;
                            init_camera(camera);
                    	//start_liveview(camera);
                    	//TakePicture(camera);
                    	
                    	take_photo(camera,1,1000);
                    	//TakePhoto();
                    	//dispose(camera);
                    	cout << "before message" << endl;
                    	MSG msg;
                    	while (GetMessage(&msg, NULL, NULL, NULL))
                    	{
                    		cout << "in while" << " msg.message:"<<msg.message<<endl;
                    		TranslateMessage(&msg);
                    		cout << "TranslateMessage" << endl;
                    		DispatchMessage(&msg); // Ends up calling objectEventFnc
                    		cout << "DispatchMessage" << endl;
                    		break;
                    	}
                    	cout << "after message" << endl;
                            EdsCloseSession(camera);
                    	cout << "EdsCloseSession(camera);";
                    	EdsTerminateSDK();
                    	system("pause");
                            return 0;
                    

                    This is output
                    0_1537429196448_3c672381-54c0-4382-832e-91e484ab0433-image.png

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 20 Sept 2018, 08:11 last edited by mrjj
                      #13

                      HI
                      ahh, so 275 is the ID for what SDK is doing.
                      So you call
                      init_camera(camera); in main window also ?
                      (and take_photo(camera,1,1000);
                      and camera is a member variable in main window so it doesn't run out of scope?

                      but you never see 275 ? in natvieEvent?

                      Y 1 Reply Last reply 20 Sept 2018, 09:18
                      0
                      • M mrjj
                        20 Sept 2018, 08:11

                        HI
                        ahh, so 275 is the ID for what SDK is doing.
                        So you call
                        init_camera(camera); in main window also ?
                        (and take_photo(camera,1,1000);
                        and camera is a member variable in main window so it doesn't run out of scope?

                        but you never see 275 ? in natvieEvent?

                        Y Offline
                        Y Offline
                        YJCN
                        wrote on 20 Sept 2018, 09:18 last edited by
                        #14

                        @mrjj Yes. I put the code(init_camera(camera) and take_photo(camera,1,1000) ) in Button's slot function, after taking picture, the programm can't enter callback function(handleObjectEvent) and there is no 275 in natvieEvent.

                        M 1 Reply Last reply 20 Sept 2018, 09:30
                        0
                        • Y YJCN
                          20 Sept 2018, 09:18

                          @mrjj Yes. I put the code(init_camera(camera) and take_photo(camera,1,1000) ) in Button's slot function, after taking picture, the programm can't enter callback function(handleObjectEvent) and there is no 275 in natvieEvent.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 20 Sept 2018, 09:30 last edited by
                          #15

                          Hi
                          and just to be 100% sure
                          camera is NOT a local variable in Button's slot function, but in the .h file as a true member of
                          main maindiw (in .h file) ?
                          (im checking its not a death by scope issue)

                          If unsure what i ask, please show full Button's slot function. :)

                          Y 1 Reply Last reply 20 Sept 2018, 10:20
                          0
                          • M mrjj
                            20 Sept 2018, 09:30

                            Hi
                            and just to be 100% sure
                            camera is NOT a local variable in Button's slot function, but in the .h file as a true member of
                            main maindiw (in .h file) ?
                            (im checking its not a death by scope issue)

                            If unsure what i ask, please show full Button's slot function. :)

                            Y Offline
                            Y Offline
                            YJCN
                            wrote on 20 Sept 2018, 10:20 last edited by
                            #16

                            @mrjj This is Button's slot function code

                            void MainWindow::on_pushButton_2_clicked()
                            {
                                EdsCameraRef camera;
                                init_camera(camera);
                                //start_liveview(camera);
                                //TakePicture(camera);
                                take_photo(camera,1,1000);
                                //TakePhoto();
                                //dispose(camera);
                                cout << "before message" << endl;
                                MSG msg;
                                while (::GetMessage(&msg, NULL, NULL, NULL))
                                {
                                    TranslateMessage(&msg);
                                    DispatchMessage(&msg); // Ends up calling objectEventFnc
                                    break;
                                }
                                cout << "after message" << endl;
                                EdsCloseSession(camera);
                                cout << "EdsCloseSession(camera);";
                                EdsTerminateSDK();
                            }
                            
                            
                            J 1 Reply Last reply 20 Sept 2018, 10:36
                            0
                            • Y YJCN
                              20 Sept 2018, 10:20

                              @mrjj This is Button's slot function code

                              void MainWindow::on_pushButton_2_clicked()
                              {
                                  EdsCameraRef camera;
                                  init_camera(camera);
                                  //start_liveview(camera);
                                  //TakePicture(camera);
                                  take_photo(camera,1,1000);
                                  //TakePhoto();
                                  //dispose(camera);
                                  cout << "before message" << endl;
                                  MSG msg;
                                  while (::GetMessage(&msg, NULL, NULL, NULL))
                                  {
                                      TranslateMessage(&msg);
                                      DispatchMessage(&msg); // Ends up calling objectEventFnc
                                      break;
                                  }
                                  cout << "after message" << endl;
                                  EdsCloseSession(camera);
                                  cout << "EdsCloseSession(camera);";
                                  EdsTerminateSDK();
                              }
                              
                              
                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 20 Sept 2018, 10:36 last edited by jsulm
                              #17

                              @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

                              while (::GetMessage(&msg, NULL, NULL, NULL))
                              {
                              TranslateMessage(&msg);
                              DispatchMessage(&msg); // Ends up calling objectEventFnc
                              break;
                              }

                              You execute this loop only once (because of the break).
                              Shouldn't you call break after you got the message you need?

                              @mrjj camera is local :-) but I don't thin this is the issue here.

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

                              M J 2 Replies Last reply 20 Sept 2018, 10:59
                              4
                              • J jsulm
                                20 Sept 2018, 10:36

                                @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

                                while (::GetMessage(&msg, NULL, NULL, NULL))
                                {
                                TranslateMessage(&msg);
                                DispatchMessage(&msg); // Ends up calling objectEventFnc
                                break;
                                }

                                You execute this loop only once (because of the break).
                                Shouldn't you call break after you got the message you need?

                                @mrjj camera is local :-) but I don't thin this is the issue here.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 20 Sept 2018, 10:59 last edited by
                                #18

                                @jsulm
                                I agree, it should not run out of scope if it works like we think :)
                                But you are right, maybe it just get first MSG as then it breaks.

                                1 Reply Last reply
                                0
                                • J jsulm
                                  20 Sept 2018, 10:36

                                  @YJCN said in How to use Windows Message Loop and Qt Application Exec?:

                                  while (::GetMessage(&msg, NULL, NULL, NULL))
                                  {
                                  TranslateMessage(&msg);
                                  DispatchMessage(&msg); // Ends up calling objectEventFnc
                                  break;
                                  }

                                  You execute this loop only once (because of the break).
                                  Shouldn't you call break after you got the message you need?

                                  @mrjj camera is local :-) but I don't thin this is the issue here.

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 20 Sept 2018, 11:39 last edited by
                                  #19

                                  @YJCN

                                  @jsulm said in How to use Windows Message Loop and Qt Application Exec?:

                                  Shouldn't you call break after you got the message you need?

                                  Indeed, the loop picks up the first message and that's it. What makes you think that message is the right/only one which needs to be processed this way? It could be any old message....

                                  Y 1 Reply Last reply 21 Sept 2018, 01:41
                                  1
                                  • J JonB
                                    20 Sept 2018, 11:39

                                    @YJCN

                                    @jsulm said in How to use Windows Message Loop and Qt Application Exec?:

                                    Shouldn't you call break after you got the message you need?

                                    Indeed, the loop picks up the first message and that's it. What makes you think that message is the right/only one which needs to be processed this way? It could be any old message....

                                    Y Offline
                                    Y Offline
                                    YJCN
                                    wrote on 21 Sept 2018, 01:41 last edited by
                                    #20

                                    @jsulm @JonB @mrjj Yes, you are right !!! I comment the "break" . It works well!! THANK YOU!☺☺☺☺☺☺☺☺☺☺

                                    J 1 Reply Last reply 21 Sept 2018, 06:52
                                    3
                                    • Y YJCN
                                      21 Sept 2018, 01:41

                                      @jsulm @JonB @mrjj Yes, you are right !!! I comment the "break" . It works well!! THANK YOU!☺☺☺☺☺☺☺☺☺☺

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 21 Sept 2018, 06:52 last edited by
                                      #21

                                      @YJCN
                                      You have to do more than just comment out the break, if that's what you're saying. If that's all you do the ::GetMessage() loop will never exit, and your code will never return from on_pushButton_2_clicked(), e.g. you cannot press it again to take another photo. Presumably you need to find out whatever is the final message your camera issues on taking a photo and exit the loop once that has been received.

                                      Y 1 Reply Last reply 22 Sept 2018, 07:17
                                      2
                                      • J JonB
                                        21 Sept 2018, 06:52

                                        @YJCN
                                        You have to do more than just comment out the break, if that's what you're saying. If that's all you do the ::GetMessage() loop will never exit, and your code will never return from on_pushButton_2_clicked(), e.g. you cannot press it again to take another photo. Presumably you need to find out whatever is the final message your camera issues on taking a photo and exit the loop once that has been received.

                                        Y Offline
                                        Y Offline
                                        YJCN
                                        wrote on 22 Sept 2018, 07:17 last edited by
                                        #22

                                        @JonB Yes. I just use a five times for loop to solve it . ^_^

                                        J 1 Reply Last reply 22 Sept 2018, 08:33
                                        0
                                        • Y YJCN
                                          22 Sept 2018, 07:17

                                          @JonB Yes. I just use a five times for loop to solve it . ^_^

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 22 Sept 2018, 08:33 last edited by
                                          #23

                                          @YJCN
                                          I don't know if your response is serious or amused. Doing it "five times" is very flaky. Isn't there a particular message number you get which you need to wait for to indicate it's now OK to leave the loop?

                                          1 Reply Last reply
                                          2

                                          13/23

                                          20 Sept 2018, 08:11

                                          • Login

                                          • Login or register to search.
                                          13 out of 23
                                          • First post
                                            13/23
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved