Results 1 to 7 of 7

Thread: Show webcam through Qt - progam doesn't respond to any user input

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    Default Show webcam through Qt - progam doesn't respond to any user input

    Hi all,
    I try to get the output of my webcam with opencv and display it through qt. Therefore I convert the IplImage into a QImage and call the function setPixmap() to draw the image onto a QLabel. Afterwards I repaint the QLabel to update it. Generally the output works fine but the program isn't responsive to any command. I can't close it by push esc or clicking on the "X" to close the window. My last option is to break the program by "ctrl-alt-del".

    Does anybody has a hint or know where the problem lie?
    I would be very excited for any help.
    Thanks in advance and kind regards,
    Saul

    Thats my whole code:
    Qt Code:
    1. // main.cpp
    2.  
    3. #include "webcam.h"
    4. #include <QtGui/QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. Webcam w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // webcam.h
    2.  
    3. #ifndef WEBCAM_H
    4. #define WEBCAM_H
    5.  
    6. #include <QtGui/QMainWindow>
    7. #include "ui_webcam.h"
    8. #include "cv.h"
    9. #include "highgui.h"
    10.  
    11. class Webcam : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Webcam(QWidget *parent = 0, Qt::WFlags flags = 0);
    17. ~Webcam();
    18.  
    19. private:
    20. Ui::WebcamClass ui;
    21.  
    22. public slots:
    23. void showWebcam();
    24. };
    25.  
    26. #endif // WEBCAM_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // webcam.cpp
    2.  
    3. #include "webcam.h"
    4.  
    5. Webcam::Webcam(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8.  
    9. connect(ui.actionStart, SIGNAL(triggered()), this, SLOT(showWebcam()));
    10. }
    11.  
    12. Webcam::~Webcam() { }
    13.  
    14. void Webcam::showWebcam()
    15. {
    16. IplImage *image;
    17. CvCapture *capture;
    18. capture = cvCaptureFromCAM(0);
    19.  
    20. int esc = 0;
    21. while(true)
    22. {
    23. image = cvQueryFrame(capture);
    24.  
    25. cvCvtColor(image, image,CV_BGR2RGB);
    26. QImage qimg((const uchar *)image->imageData, image->width, image->height, QImage::Format_RGB888);
    27. ui.label->setPixmap(QPixmap::fromImage(qimg));
    28. ui.label->repaint();
    29.  
    30. esc = cvWaitKey(10);
    31. if (esc == 27) break;
    32. }
    33. cvReleaseCapture(&capture);
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    In your showWebcam() method, you never give the Qt event loop the chance to process any other events, so that is why your GUI is unresponsive. You need to insert a call to QCoreApplication:: processEvents() somewhere in your loop.

    I am guessing that is the reason why you call label->repaint() instead of label->update() - since update() puts a paint event on the event stack and your loop never lets the stack get processed, the label never gets painted.

    After adding the QCoreApplication:: processEvents() call, change the label->repaint() to a label->update() and it should all work fine (and make the label a "good player" in the Qt world again).

  3. #3
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    Hi d_stranz,
    thank you very much for your response! I changed my code as you wrote. Hope it is correct now. At least it works and I'am really happy =)
    Cheers,
    Saul

    By the way: the program will not execute the break command if I press ESC.

    Qt Code:
    1. void Webcam::showWebcam()
    2. {
    3. IplImage *image;
    4. CvCapture *capture;
    5. capture = cvCaptureFromCAM(0);
    6.  
    7. int esc = 0;
    8. while(true)
    9. {
    10. image = cvQueryFrame(capture);
    11.  
    12. cvCvtColor(image, image,CV_BGR2RGB);
    13. QImage qimg((const uchar *)image->imageData, image->width, image->height, QImage::Format_RGB888);
    14. ui.label->setPixmap(QPixmap::fromImage(qimg));
    15. ui.label->update();
    16.  
    17. QCoreApplication::processEvents();
    18.  
    19. esc = cvWaitKey(10);
    20. if (esc == 27) break;
    21. }
    22. cvReleaseCapture(&capture);
    23. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    Hi, its me again.
    I just want to ask, if it's conform to use QCoreApplication:: processEvents(); directly in my loop? Or is it unsuitable to use it this way?
    I'm a little bit self-conscious in this way, yet. Would be very thankful for a "Yes" or a "NO" =)

    Are there any other possibilities to realize a webcam output?
    Ok, the first one I tried is the QLabel. Think it could be done with Phonon::VideoPlayer, too.
    Are there even more possibilities?
    Kind regards,
    Saul
    Last edited by Saul; 11th July 2011 at 20:07.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    It is OK to use QCoreApplication:: processEvents() in a loop.

    On the other hand, if you are forced to do that in order to get a responsive application, it is generally a sign that your application design could be improved.

    Usually, it is better to try to implement a signal / slot or multithreaded design so that compute-intensive work gets done in a different thread from the GUI, and communicates to the GUI thread using signals (something like "frameReady").

    You might be able to use Phonon, but I am not familiar with it so someone else will have to answer that question.

  6. #6
    Join Date
    Jun 2011
    Posts
    4

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    OK, thanks again d_stranz!
    I will do my best. Bought a Qt book and found a chapter about threads yesterday. Armed and ready for the fight now

    But I have just one last Question and Problem. If I close the Program the webcam is still running. My function to break the loop by push the button ESC does not work.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Show webcam through Qt - progam doesn't respond to any user input

    I've posted a simple Qt + OpenCv example here, you may want to check it out. You don't need any separate threads or processEvents() calls to get responsive ui during webcam capture and simple image processing, just use QTimer.

Similar Threads

  1. Http request doesn't respond when the host is local
    By dineshkumar in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 13:09
  2. User Input using QString
    By kumarpraveen in forum Newbie
    Replies: 4
    Last Post: 9th July 2010, 07:49
  3. QGraphicsScene doesn't respond to mousePressEvent
    By Affenbrotbaum in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2010, 19:56
  4. problem with reading input from user
    By ketan007 in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2009, 10:51
  5. QSplitter only resize from user input?
    By winston2020 in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 15:50

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.