Solved QT update video frame cause stuck?
-
Hi I'm now using a promoted qlabel to update image from an selected region area.
But this would cause the program stuck and I'm wondering what's the reason of it.
Here's my code below:
mainwindow.cpp, --videoviewer2 is the promoted QLabel, update roi is the slot to receive qrect of ROI and crop the image.void app0::updateroi(QRectF roi, double label_w, double label_h) { cout << "updating roi!" << "\n"; double x, y, w, h = 0; QRectF real_roi(QPointF(0,0),QPointF(0,0)); x = roi.x(); y = roi.y(); double x2 = roi.bottomRight().x(); double y2 = roi.bottomRight().y(); w = roi.width(); h = roi.height(); double x_rate = camera_w/ label_w; double y_rate = camera_h/ label_h; cout << "rate x,y " << x_rate << " " << y_rate << "\n"; double ori_x, ori_y, ori_width, ori_height;// ori_x2, ori_y2; ori_x = x_rate * x; ori_y = y_rate * y; ori_width = x_rate * w; ori_height = y_rate * h; real_roi = QRectF(QPointF(ori_x,ori_y),QSizeF(ori_width, ori_height)); //cout << "roi area" << x_rate << " " << y_rate << " " << w_rate << " " << h_rate << "\n"; QImage image = image_update.copy(ori_x,ori_y, ori_width, ori_height);//videoviewer2 ui->videoviewer2->setPixmap(QPixmap::fromImage(image));}
mylabel.cpp promoted label, when mouse pressed get the startpoint, and when it's release emit a signal to send the coordinates to the slot above.
void MyLabel::mouseReleaseEvent(QMouseEvent *e) { m_rectEndPoint = e->pos(); if (mouse_pressed == true) { m_roi = QRectF(m_rectStartPoint, m_rectEndPoint); mouse_released = true; double x_ratio = 0, y_ratio = 0, w_ratio = 0, h_ratio = 0; double roi_width = 0, roi_height = 0; cout << "m_roi "<< m_roi.x() << " " << m_roi.y() << " " << m_roi.width() << " " << m_roi.height() <<"\n"; while (1) { emit updaterate(m_roi, this->width(), this->height()); } } mouse_pressed = false; }
As I know, the signal would only activate once after mouse Release Event, I've tried to add while(true) at first line of the slot or befor emitting the signal both won't work.
The images and camera thead, UI are aborted.
I'm wondering is it the problem of not using a thread? Or am I missing something like sleep(ms) that would cause aborted? -
@Puppy-Bear said in QT update video frame cause stuck?:
slot{
while(1)
{
qApp->processEvents();
msleep()
}
}Even if using
processEvents()
/msleep()
were the right thing to do --- which is not the case --- you should realise that anywhile (1)
would mean the slot never exits. Which would be disastrous, and you should understand why.As @eyllanesc have said, just one plain send of signal. Anything then wrong is another matter.
-
@Puppy-Bear said in QT update video frame cause stuck?:
But this would cause the program stuck and I'm wondering what's the reason of it.
while (1) { emit updaterate(m_roi, this->width(), this->height()); }
What would you expect this to do other than "freeze" a program?
Certainly you only want to emit a signal once.
I don't know what you are trying to achieve which does not work, but if you override
mouseReleaseEvent()
I would expect you to call the base implementation. -
HI, I have notice the problem before, but while I tried to set the slot like while(1){crop the image} and emit the signal only once, this would still cause a stuck, I'm confused that the slot won't take much time to crop the image, is it a must to use another thread? Or maybe should I try QTimer?
Okay I've just found out that i'm missing the processEvents, it should be able to realize to re-activate the slot like this, thanks for advice!
slot{
while(1)
{
qApp->processEvents();
msleep()
}
} -
@Puppy-Bear Using processEvent and sleep is unnecessary. Send the signal only once and you should not have problems, if the slot associated with the signal is not called then there is something wrong with your program.
-
@Puppy-Bear said in QT update video frame cause stuck?:
slot{
while(1)
{
qApp->processEvents();
msleep()
}
}Even if using
processEvents()
/msleep()
were the right thing to do --- which is not the case --- you should realise that anywhile (1)
would mean the slot never exits. Which would be disastrous, and you should understand why.As @eyllanesc have said, just one plain send of signal. Anything then wrong is another matter.
-
This post is deleted! -
This post is deleted! -
@JonB Hi I'm actually trying to crop image from camera capture thread, so it would be a infinite loop, and keep sending it to a preprocess thread.
my code looks like this and it seems running normal right now,
If it's not a nice way to run with, would you please show me how am I able to execute a loop inside a thread's slot?
My code is below:
promoted QLabel:void MyLabel::mouseReleaseEvent(QMouseEvent *e){ if(mouse_released==true){ emit updaterate(m_roi, this->width(), this->height()); }
SLOT in mainwindow.cpp
void app0::updateroi(QRectF roi, double label_w, double label_h) { thread2.start();//sending cropped img to this thread, using movetoThread while(1){ qApp->processEvents(); crop_image_function(); if(image.isNull()==false){ emit sendcropimage(image); //I'm connecting it with my preprocess thread's slot cout << "send crop image" << "\n"; ui->videoviewer2->setPixmap(QPixmap::fromImage(image));//crop from member QImage and showing it } QThread::msleep(30); } }
-
@Puppy-Bear said in QT update video frame cause stuck?:
void app0::updateroi(QRectF roi, double label_w, double label_h)
{
thread2.start();//sending cropped img to this thread, using movetoThread
while(1){Do you understand that updateroi(...) will hang forever because you have an infinite loop inside without any break condition?
Use a QTimer to trigger actions on a regular basis instead of doing strange hacks. -
@JonB said in QT update video frame cause stuck?:
you should realise that any
while (1)
would mean the slot never exits. Which would be disastrous, and you should understand why. -
@Puppy-Bear said in QT update video frame cause stuck?:
thread2.start();//sending cropped img to this thread, using movetoThread
I kind of doubt you're doing any "threading" at all.
-
@JonB Hi Thanks for your reminding, as my limit of experience, I have updated my code below.
timer = new QTimer(this); timer->setInterval(20); timer->start(); void app0::updateroi(QRectF roi, double label_w, double label_h) { timer->setInterval(20); timer->start();} void app0::stop_timer() { timer->stop(); } void app0::cropimg() { qApp->processEvents(QEventLoop::AllEvents, 100); //Do some crop img preprocess here }
@eyllanesc said in QT update video frame cause stuck?:
@Puppy-Bear Using processEvent and sleep is unnecessary. Send the signal only once and you should not have problems, if the slot associated with the signal is not called then there is something wrong with your program.
It's been mentioned that processEvent is unecessary which made me a little confused earlier.
-
@Puppy-Bear said in QT update video frame cause stuck?:
void app0::cropimg() {
qApp->processEvents(QEventLoop::AllEvents, 100);processEvents() is not needed