I don't have much experience, but I can see what can go wrong here:
for (;;) {
cap >> image;
//conversion from Mat to QImage
Mat dest;
cvtColor(image, dest,CV_BGR2RGB);
QImage image1= QImage((uchar*) dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);
//show Qimage using QLabel
myLabel.setPixmap(QPixmap::fromImage(image1));
myLabel.show();
//imshow("camera",image);
//if (waitKey(30)>= 0) break;
}
You are doing this in dead loop - it will cause your QLabel to update itself infinitely, so you may not see anything. Also, if uncommenting waitKey
is helping you, that pretty much mean that you are converting data to QImage
well, but something other is broken.
Note that a.exec()
will never execute, as you will be stuck in the loop, but I guess this was enough for hitting the concept.
In order not to stuck event loop, you need to create QTimer
and every x milliseconds to update your widget:
class VideoWindow: public QWidget
{
Q_OBJECT
public:
VideoWindow(QWidget* parent = 0): QWidget(parent), cap(0)
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updatePicture()));
timer->start(20);
}
public slots:
void updatePicture()
{
cap >> image;
//conversion from Mat to QImage
Mat dest;
cvtColor(image, dest,CV_BGR2RGB);
QImage image1 = QImage((uchar*) dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);
//show Qimage using QLabel
setPixmap(QPixmap::fromImage(image1));
}
private:
QTimer * timer;
VideoCapture cap;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
VideoWindow w;
w.show();
app.exec();
return 0;
}