Results 1 to 4 of 4

Thread: why doesn't signal gets emiited?

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default why doesn't signal gets emiited?

    i dont understand why are my signals not working in this multi threaded app

    file.h:
    Qt Code:
    1. #include <QObject>
    2.  
    3.  
    4. class FThread;
    5.  
    6. class File : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. File(QObject *parent = 0);
    11. FThread *fthread;
    12. public slots:
    13. void print();
    14.  
    15.  
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

    fthread.h

    Qt Code:
    1. #include <QThread>
    2.  
    3.  
    4. class File;
    5. class FThread : public QThread
    6. {
    7. Q_OBJECT
    8. public:
    9.  
    10.  
    11. FThread(QObject *parent = 0);
    12. virtual void run();
    13. File *f;
    14. void sampleFunc();
    15. signals:
    16. void done();
    17.  
    18.  
    19. };
    To copy to clipboard, switch view to plain text mode 
    file.cpp:

    Qt Code:
    1. #include "file.h"
    2. #include <QDebug>
    3. #include "fthread.h"
    4.  
    5. File::File(QObject *parent) :
    6. QObject(parent)
    7. {
    8.  
    9. connect(fthread,SIGNAL(done()),this,SLOT(print()));
    10.  
    11. }
    12. void File::print(){
    13. qDebug()<<"printing from File object";
    14. fthread.exit();
    15. }
    To copy to clipboard, switch view to plain text mode 
    fthread.cpp

    Qt Code:
    1. #include "fthread.h"
    2. #include "file.h"
    3. #include <QDebug>
    4.  
    5. FThread::FThread(QObject *parent) :
    6. QThread(parent)
    7. {
    8.  
    9. f=new File();
    10.  
    11.  
    12. }
    13.  
    14. void FThread::run(){
    15.  
    16. sampleFunc();
    17. this->exec();
    18.  
    19. }
    20.  
    21. void FThread::sampleFunc()
    22. {
    23. emit done(); //<<--------------SIGNAL SHOULD BE EMITTED FROM HERE
    24. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QDebug>
    3. #include "fthread.h"
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. qDebug()<<"App started";
    9. FThread fthread;
    10. fthread.start();
    11. fthread.wait();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    NOTE:

    i get following output:

    App started
    The program has unexpectedly finished.
    ...path/Thread exited with code 0

    means it is exiting without error


    any help please!!!!!!!
    Last edited by naturalpsychic; 26th January 2011 at 15:36.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't signal gets emiited?

    You are emitting your signal from the thread before QApplication event loop is running (before a.exc()) so that signal never gets processed by the application, and thus the slot is not called.
    I am not sure why the application ends however - need to think...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: why doesn't signal gets emiited?

    read this, it should help.
    See also.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: why doesn't signal gets emiited?

    Quote Originally Posted by naturalpsychic View Post
    i dont understand why are my signals not working in this multi threaded app
    Have a look at the comments I have inserted into your code snippets
    file.h:
    Qt Code:
    1. #include <QObject>
    2.  
    3.  
    4. class FThread;
    5.  
    6. class File : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. File(QObject *parent = 0);
    11. FThread *fthread;
    12. // ^^^^^ Where does this pointer ever get an instance of FThread attached to it?
    13. public slots:
    14. void print();
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 

    file.cpp:
    Qt Code:
    1. #include "file.h"
    2. #include <QDebug>
    3. #include "fthread.h"
    4.  
    5. File::File(QObject *parent) :
    6. QObject(parent)
    7. {
    8.  
    9. connect(fthread,SIGNAL(done()),this,SLOT(print()));
    10. // ^^^^^ you use fthread here but is has not been initialised either 0 (NULL)
    11. // or to be a valid pointer.
    12. // On the way to crash city
    13.  
    14. }
    15. void File::print(){
    16. qDebug()<<"printing from File object";
    17. fthread.exit();
    18. // ^^^^^ given that fthread is a pointer I am surprised this even compiles
    19. }
    To copy to clipboard, switch view to plain text mode 

    NOTE:

    i get following output:

    App started
    The program has unexpectedly finished.
    ...path/Thread exited with code 0

    means it is exiting without error
    Which part of "The program has unexpectedly finished." means "exiting without error"?

    Have you single-stepped the program to see at what point the program terminates unexpectedly?

Similar Threads

  1. Connect signal/signal in Qt Designer
    By jlemaitre in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 16:53
  2. signal mapping on pushbutton signal clicked
    By wagmare in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 08:54
  3. Signal-Signal Connections Between Threads
    By PhilippB in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2008, 19:27
  4. signal of QListWidget doesn't work
    By vito49 in forum Qt Programming
    Replies: 10
    Last Post: 1st October 2008, 06:07
  5. QUrlOperator doesn't emit finished signal
    By hayati in forum Qt Programming
    Replies: 16
    Last Post: 26th March 2007, 21:25

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.