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. handle keyevent in multi-threading application with Qt+OpenCV
Forum Updated to NodeBB v4.3 + New Features

handle keyevent in multi-threading application with Qt+OpenCV

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.5k 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.
  • R Offline
    R Offline
    rokk
    wrote on last edited by rokk
    #1

    I have a multi-threading application written in C++ with Qt5.7 and OpenNI. It has a main thread that starts a second thread which capture frame from a .oni recording file (asus xtion pro live) does some processing and through the Qt signal-slot mechanism pass the frame to the main thread, which display it using imshow(). What I want to do is to implement a pause key, so pressing for example 'p' the processing pause. I am thinking of something like this:

    void Camera::run(){
      while(!cameraStop && this->device.isValid())
        {
          try {
            if (!buttonPause) {
                getFrame();
                process();
                emit sigFrameImageReady(frame);
                if (cv::waitKey(1)==112){
                  setButtonPause(!(getButtonPause()));
               }
              }
    
          }
          catch(std::exception &ex) {
            std::cerr << "getFrame()" << ex.what() << std::endl;
          }
        }
    }
    

    In this way it doesn't work, I think that's because the frame is displayed by another thread (the main one), the waitKey() here simply blocks the entire process, but if I put it in the main thread, just after imshow() in this way:

    void Process::FrameImageReady(cv::Mat FrameImage)
    {
      if (modedebug)
        cv::imshow("bgr", FrameImage);
      if (cv::waitKey(10)==112){
        cam->setButtonPause(!(getButtonPause()));
      }
    } 
    

    waitkey seems to be ignored (image displaying works fine).. any idea? Perhaps it is better to use a Qt function do it ?
    It is a console application, the GUI part is only for debugging purpose.

    kshegunovK 1 Reply Last reply
    0
    • R rokk

      I have a multi-threading application written in C++ with Qt5.7 and OpenNI. It has a main thread that starts a second thread which capture frame from a .oni recording file (asus xtion pro live) does some processing and through the Qt signal-slot mechanism pass the frame to the main thread, which display it using imshow(). What I want to do is to implement a pause key, so pressing for example 'p' the processing pause. I am thinking of something like this:

      void Camera::run(){
        while(!cameraStop && this->device.isValid())
          {
            try {
              if (!buttonPause) {
                  getFrame();
                  process();
                  emit sigFrameImageReady(frame);
                  if (cv::waitKey(1)==112){
                    setButtonPause(!(getButtonPause()));
                 }
                }
      
            }
            catch(std::exception &ex) {
              std::cerr << "getFrame()" << ex.what() << std::endl;
            }
          }
      }
      

      In this way it doesn't work, I think that's because the frame is displayed by another thread (the main one), the waitKey() here simply blocks the entire process, but if I put it in the main thread, just after imshow() in this way:

      void Process::FrameImageReady(cv::Mat FrameImage)
      {
        if (modedebug)
          cv::imshow("bgr", FrameImage);
        if (cv::waitKey(10)==112){
          cam->setButtonPause(!(getButtonPause()));
        }
      } 
      

      waitkey seems to be ignored (image displaying works fine).. any idea? Perhaps it is better to use a Qt function do it ?
      It is a console application, the GUI part is only for debugging purpose.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Hi,
      Since you've subclassed QThread and you're not using an event loop you're pretty much forced to use the low-level "solution", that is blocking with a primitive. See here for example code with a QSemaphore .

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2

      • Login

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