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. QT update video frame cause stuck?
Forum Updated to NodeBB v4.3 + New Features

QT update video frame cause stuck?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 1.1k Views
  • 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.
  • Puppy BearP Offline
    Puppy BearP Offline
    Puppy Bear
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • Puppy BearP Puppy Bear

      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()
      }
      }

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #5

      @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 any while (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 BearP JonBJ 2 Replies Last reply
      1
      • Puppy BearP Puppy Bear

        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?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #2

        @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.

        1 Reply Last reply
        3
        • Puppy BearP Offline
          Puppy BearP Offline
          Puppy Bear
          wrote on last edited by Puppy Bear
          #3

          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()
          }
          }

          eyllanescE JonBJ 2 Replies Last reply
          0
          • Puppy BearP Puppy Bear

            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()
            }
            }

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #4

            @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.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            2
            • Puppy BearP Puppy Bear

              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()
              }
              }

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #5

              @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 any while (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 BearP JonBJ 2 Replies Last reply
              1
              • Puppy BearP Offline
                Puppy BearP Offline
                Puppy Bear
                wrote on last edited by Puppy Bear
                #6
                This post is deleted!
                Puppy BearP 1 Reply Last reply
                0
                • Puppy BearP Puppy Bear

                  This post is deleted!

                  Puppy BearP Offline
                  Puppy BearP Offline
                  Puppy Bear
                  wrote on last edited by
                  #7
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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 any while (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 BearP Offline
                    Puppy BearP Offline
                    Puppy Bear
                    wrote on last edited by
                    #8

                    @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);
                    	}
                    }
                    
                    jsulmJ J.HilkJ 2 Replies Last reply
                    0
                    • Puppy BearP Puppy Bear

                      @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);
                      	}
                      }
                      
                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Puppy BearP 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @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 any while (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.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #10

                        @Puppy-Bear

                        @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.

                        1 Reply Last reply
                        0
                        • Puppy BearP Puppy Bear

                          @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);
                          	}
                          }
                          
                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #11

                          @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.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @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.

                            Puppy BearP Offline
                            Puppy BearP Offline
                            Puppy Bear
                            wrote on last edited by
                            #12

                            @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.

                            jsulmJ 1 Reply Last reply
                            0
                            • Puppy BearP Puppy Bear

                              @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.

                              jsulmJ Online
                              jsulmJ Online
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @Puppy-Bear said in QT update video frame cause stuck?:

                              void app0::cropimg() {
                              qApp->processEvents(QEventLoop::AllEvents, 100);

                              processEvents() is not needed

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

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