Results 1 to 15 of 15

Thread: use signal&slot between two tabs

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

    Default use signal&slot between two tabs

    I have a QTabWidget, and two classes, display1 and display2, are two tab members. I want to transfer some data, say a bool flag, between these two tabs:

    Qt Code:
    1. tabWidget = new QTabWidget;
    2. display1 = new Display1 ();
    3. display2= new Display2()
    4. tabWidget->addTab(display1 , tr("Online"));
    5. tabWidget->addTab(display2, tr("Offline"));
    6.  
    7. connect(Display1, SIGNAL(sendout(bool)), Display2, SLOT(receive(bool)));
    To copy to clipboard, switch view to plain text mode 

    In class Display1.h:
    Qt Code:
    1. signals:
    2. void sendout(bool flag);
    To copy to clipboard, switch view to plain text mode 

    In class Display1.cpp, one of its methods does this:
    Qt Code:
    1. bool flag = true;
    2. emit sendout(flag);
    To copy to clipboard, switch view to plain text mode 

    In class Display2.h:
    Qt Code:
    1. public slots:
    2. void receive(bool flag);
    To copy to clipboard, switch view to plain text mode 

    In class Display2.cpp:
    Qt Code:
    1. void Display2::receive(bool flag)
    2. {
    3. printf("I got one message. \n");
    4. }
    To copy to clipboard, switch view to plain text mode 

    I got LINK ERROR:
    Error 6 error LNK2001: unresolved external symbol "protected: void __thiscall Display1::sendout(bool)" (?sendout@Display1@@IAEX_N@Z) Display1.obj

    I have no idea where is the problem....Can anyone help me? Thanks

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

    Default Re: use signal&slot between two tabs

    You didn't moc the header file. Are you using qmake?

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

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by wysota View Post
    You didn't moc the header file. Are you using qmake?

    I have the moc already. This is strange. When I delete the SIGNAL in Display1, everything is running. The problem seems in Display1. But I can't findany error there...

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

    Default Re: use signal&slot between two tabs

    What do you mean you have the moc? Are you compiling and linking the file that moc generates for your class? Did you remember about the Q_OBJECT macro?

  5. The following user says thank you to wysota for this useful post:

    stella1016 (25th July 2008)

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

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by wysota View Post
    What do you mean you have the moc? Are you compiling and linking the file that moc generates for your class? Did you remember about the Q_OBJECT macro?
    Oh, I find out that in the .h files I don't have the configurations any more. Strange, I've set them in the past.... Maybe erased during copy and paste.

    Thanks very much!!!!!!! I think my problem is solved!!!!

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

    Default Re: use signal&slot between two tabs

    I know why... because I changed to release model from debug model
    (I am using visual studio)....

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

    Default Re: use signal&slot between two tabs

    Hi, I have a another question.
    If I want to emit something not simple as bool but my own class instance, this also can be done via SIGNAL & SLOTS?

  9. #8
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by stella1016 View Post
    Hi, I have a another question.
    If I want to emit something not simple as bool but my own class instance, this also can be done via SIGNAL & SLOTS?
    Yes, but just read about Signal and Slots in Docu in case you want e.g. Queued Connection.

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

    Default Re: use signal&slot between two tabs

    I doubt that whether SIGNAL & SLOT can be synchronized...
    In my old project, I need to update a QGLWidget with the new camera output data. I used SIGNAL & SLOT. But sometimes the two SIGNALS emitted, but only one SLOT responds.
    If anyone interested, I can post the code.

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

    Default Re: use signal&slot between two tabs

    Quote Originally Posted by stella1016 View Post
    I doubt that whether SIGNAL & SLOT can be synchronized...
    Signals & slots can be synchronized.

    In my old project, I need to update a QGLWidget with the new camera output data. I used SIGNAL & SLOT. But sometimes the two SIGNALS emitted, but only one SLOT responds.
    This is hardly possible. You must have done something wrong.

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

    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.

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

    Default Re: use signal&slot between two tabs

    why I posted twice..

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    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.

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.