Results 1 to 15 of 15

Thread: use signal&slot between two tabs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: use signal&slot between two tabs

    I hope I did something wrong. Maybe you can help me solve my old problem. I post my code here. My app is to display the camera output images in QGLWidget.

    MyThread: in its run method, it does the while-loop
    Qt Code:
    1. run()
    2. {
    3. while(flag)
    4. {
    5. IplImage *lp_color = t_camera.get_image();
    6. myQLWidget->setCurrentImageIpl(lp_color);
    7. cvReleaseImage(&lp_color);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    MyQLWidget: in charge of updating the texture when it receives any image.
    I am using SIGNAL & SLOT in MyQLWidget, because in method setCurrentImageIpl(), I cannot directly call updateGL(). This is because OpenGl & Thread issue... So in setCurrentImageIpl() I emit a SIGNAL to a SLOT, the SLOT will do updateGL() for me.
    Qt Code:
    1. MyQLWidget::setCurrentImageIpl()
    2. {
    3.  
    4. ...(set image)
    5. imageData = ...;
    6. update = true;
    7. emit newData();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    newData is connect to SLOT updateImage()

    Qt Code:
    1. MyQLWidget::updateImage()
    2. {
    3. this->updateGL();
    4. }
    5.  
    6. MyQLWidget::paintGL()
    7. {
    8. if(update)
    9. glTexSubImage2D = (.....imageData);
    10. }
    To copy to clipboard, switch view to plain text mode 

    During displaying the first few frames that grasped by camera, my program crashes. Not every time. I don't know where is the problem. When it crashes, the reason is that the imageData is not set jet, so when update texture it crashes.

    I set breakpoint, and found that in the first few steps, ...emit... executes twice, but updateImage() only once. Maybe this causes the crash, I am not sure.

    Hard to explain what I did in my code here.
    Last edited by stella1016; 25th July 2008 at 18:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by stella1016 View Post
    I hope I did something wrong. Maybe you can help me solve my old problem. I post my code here. My app is to display the camera output images in QGLWidget.

    MyThread: in its run method, it does the while-loop
    Qt Code:
    1. run()
    2. {
    3. while(flag)
    4. {
    5. IplImage *lp_color = t_camera.get_image();
    6. myQLWidget->setCurrentImageIpl(lp_color);
    7. cvReleaseImage(&lp_color);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    This is bad. You can't access widgets from other threads and I can't stress that more. You should have emitted a signal from the thread (from the thread and not the thread object!) that would be caught by a slot in the widget.

    So in setCurrentImageIpl() I emit a SIGNAL to a SLOT, the SLOT will do updateGL() for me.
    This is wrong. You are emitting a signal on behalf of the widget from a worker thread. Qt checks which thread owns the sending and receiving objects, sees that it is the GUI thread, so makes a direct function call to updateGL() from the worker thread which is wrong and will cause segmentation faults sooner or later.


    During displaying the first few frames that grasped by camera, my program crashes.
    That's exactly what I mean
    Last edited by wysota; 25th July 2008 at 20:45.

  3. #3
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by wysota View Post
    This is bad. You can't access widgets from other threads and I can't stress that more. You should have emitted a signal from the thread (from the thread and not the thread object!) that would be caught by a slot in the widget.

    So in setCurrentImageIpl() I emit a SIGNAL to a SLOT, the SLOT will do updateGL() for me.
    This is wrong. You are emitting a signal on behalf of the widget from a worker thread. Qt checks which thread owns the sending and receiving objects, sees that it is the GUI thread, so makes a direct function call to updateGL() from the worker thread which is wrong and will cause segmentation faults sooner or later.



    That's exactly what I mean
    Thanks for your replay.
    I don't fully understand what you mean...So how can I make things right?
    Last edited by wysota; 25th July 2008 at 20:47.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: use signal&slot between two tabs

    Read about signals and multithreading, that's for a start. Then you need to make sure that:
    1. you don't access any widget method from non-GUI thread directly
    2. queued connections are used for signal-slot connections between threads (and I don't mean passing the optional argument to connect() but to make sure Qt will choose queued connections by itself).

Similar Threads

  1. Replies: 2
    Last Post: 23rd July 2012, 08:42
  2. QTabWidget with same tabs
    By Djony in forum Qt Programming
    Replies: 20
    Last Post: 24th December 2011, 12:20
  3. How to make "west" tabs horizontal?
    By plumbum in forum Qt Programming
    Replies: 2
    Last Post: 7th June 2007, 10:32
  4. Switching off all tabs in QTabWidget
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 30th August 2006, 17:10
  5. Removing Tabs
    By NewGuy in forum Qt Programming
    Replies: 6
    Last Post: 22nd July 2006, 22:46

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
  •  
Qt is a trademark of The Qt Company.