Results 1 to 5 of 5

Thread: QCameraImageCapture is saving image only on program exit

  1. #1
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QCameraImageCapture is saving image only on program exit

    Recently I've been testing my Qt 5.5 C++ (Win7) program on one older laptop and found a weird bug, namely I'm displaying camera image and on some action (when letter P is pressed on keyboard) save that image in Pictures folder. Everything works fine (on P click image is saved) until I start new thread (for doing some background process). After the new thread is started, on "take a picture" event nothing happens (image is not visible in Pictures folder; onImageError[/ is not called) and after I close the application all images that are being captured are saved.

    I'm not sure if I made myself clear so here is one more time steps I perform:
    Open app -> take a photo -> photo is saved -> start new thread -> take couple of pictures (they are not saved) -> close app -> app closed (ALL pictures are saved)

    The important part I have to mention is that all object I use (QCamera, QCameraImageCapture and thread that I start) are pointers.
    And I am not 100% sure that starting this thread cause the issue but every time I start the thread, images are not saved.

    Here are source that I am using:

    Capturing camera image:
    Qt Code:
    1. void MyClass::onCameraSeleced(QCameraInfo camera_info) {
    2. camera_ = new QCamera(camera_info);
    3. pture(camera_);
    4. camera_image_capture_->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    5.  
    6. camera_image_capture_ = new QCameraImageCa
    7. // connect to capture signals
    8. connect(camera_image_capture_, SIGNAL(imageCaptured(int, const QImage&)),
    9. this, SLOT(onImageCaptured(int, const QImage&)));
    10. connect(camera_image_capture_, SIGNAL(imageSaved(int, const QString&)),
    11. this, SLOT(onImageSaved(int, const QString&)));
    12. connect(camera_image_capture_, SIGNAL(error(int,QCameraImageCapture::Error,QString)), this,
    13. SLOT(onImageError(int,QCameraImageCapture::Error,QString)));
    14.  
    15. camera_->setCaptureMode(QCamera::CaptureStillImage);
    16. camera_->setViewfinder(ui->cameraWidget);
    17. camera_->start();
    18. }
    19.  
    20. void MyClass::takeScreenShot(){
    21. camera_->searchAndLock();
    22.  
    23. camera_image_capture_->capture(name);
    24.  
    25. camera_->unlock();
    26. }
    27.  
    28. MyClass::~MyClass()
    29. {
    30. if(camera_ != NULL)
    31. {
    32. camera_->unload();
    33. delete camera_;
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    Starting new thread that cause this issue:
    Qt Code:
    1. my_thread_ = new MyThread();
    2.  
    3. QThreadPool::globalInstance()->start(my_thread_);
    To copy to clipboard, switch view to plain text mode 

    MyThread:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. while(run_)
    4. {
    5. .
    6. .
    7. .
    8. QThread::msleep(static_speed_yaw_data_->getTimeInterval());
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QCameraImageCapture is saving image only on program exit

    Method MyThread::run() is blocking thread event loop. Use standard QThread::run() and create a method fired by QTimer with static_speed_yaw_data_->getTimeInterval() period.

  3. #3
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QCameraImageCapture is saving image only on program exit

    Quote Originally Posted by Lesiok View Post
    Method MyThread::run() is blocking thread event loop. Use standard QThread::run() and create a method fired by QTimer with static_speed_yaw_data_->getTimeInterval() period.
    Thread which captures image is different from MyThread. Do I still need to do stuff you mentioned above? (it's not really clear to me why is image not captured and why should it be with our response)

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCameraImageCapture is saving image only on program exit

    You probably shouldn't call "MyThread" like that since it is obviously a QRunnable and not a QThread.

    Or maybe better make it an actual thread, the while loop suggests that you want to control it, not hand over to some thread pool.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QCameraImageCapture is saving image only on program exit

    You were right!

    I have no idea how that part slipped me.

    After changing QRunnable to QThread everything worked (tested it today).

    Thanks!

Similar Threads

  1. why is QCameraImageCapture always not ready windows 7?
    By josentop in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2014, 16:14
  2. Database program exit when function called
    By micamica in forum Newbie
    Replies: 8
    Last Post: 13th July 2011, 17:07
  3. program exit itself when create dialog with QSystemTrayIcon
    By jim2212001 in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2010, 11:35
  4. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.