Results 1 to 2 of 2

Thread: Signals & Slots problem

  1. #1
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    3

    Default Signals & Slots problem

    Hi,
    I have some kind of loop using signals & slots. My application crashed when program do 3580 iterations on Qt4.8. I can't understand why crash. Please kindly help.

    Qt Code:
    1. //main.cpp
    2. int main(int argc, char *argv[])
    3. {
    4. ...
    5. MainWindow w;
    6. Second s;
    7. QObject::connect(&w,SIGNAL(go()),&s,SLOT(go()));
    8. QObject::connect(&s,SIGNAL(back()),&w,SLOT(back()));
    9. ...
    10. }
    11.  
    12.  
    13. //mainwindow.h
    14. class MainWindow : public QMainWindow
    15. {
    16. ...
    17. private:
    18. int VALUE;
    19. int MAX_VALUE;
    20.  
    21. private slots:
    22. void on_pushButton_clicked();
    23.  
    24. public slots:
    25. void back();
    26.  
    27. signals:
    28. void go();
    29. };
    30.  
    31.  
    32. //mainwindow.cpp
    33. void MainWindow::on_pushButton_clicked()
    34. {
    35. VALUE = 0;
    36. MAX_VALUE = 10000;
    37.  
    38. emit go();
    39. }
    40.  
    41. void MainWindow::back()
    42. {
    43. qDebug() << VALUE;
    44.  
    45. if(VALUE<MAX_VALUE)
    46. {
    47. VALUE++;
    48. emit go();
    49. }
    50. }
    51.  
    52.  
    53. //second.h
    54. class Second : public QObject
    55. {
    56. ...
    57. signals:
    58. void back();
    59.  
    60. public slots:
    61. void go();
    62. };
    63.  
    64.  
    65. //second.cpp
    66. void Second::go()
    67. {
    68. emit back();
    69. }
    To copy to clipboard, switch view to plain text mode 
    test2.zip

    Thanks;

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Signals & Slots problem

    When the sender and receiver of signal are in this same thread emitting signal is this same like calling procedure. So You have stack overflow. Read more about QObject::connect and use last parameter to create queued connection.

  3. The following user says thank you to Lesiok for this useful post:

    cupersuper (10th November 2015)

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Problem with signals and slots
    By ggdev001 in forum Qt Programming
    Replies: 6
    Last Post: 19th February 2013, 17:46
  3. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  4. Problem using SIGNALS/SLOTS
    By JimDaniel in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2007, 04:59
  5. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20

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.