Results 1 to 4 of 4

Thread: Signal and Slot Problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Signal and Slot Problem

    Just when I thought I had signals and slots figured out ... not so.
    I have one class (editlog) that sends a signal to a slot in the mainwindow, and it works fine.
    So I set up another one from a different class (openlog), and ... nothing, no response.

    Here is the code that works ok:

    Qt Code:
    1. // part of editlog.h
    2. signals:
    3. void addLookup(int);
    4.  
    5. // part of editlog.cpp
    6. void EditLog::on_pushButton_clicked()
    7. {
    8. emit addLookup(::logID);
    9. }
    10.  
    11. // part of mainwindow.h
    12. public:
    13. EditLog *el;
    14.  
    15. public slots:
    16. void updateLog(int);
    17.  
    18. // part of mainwindow.cpp
    19.  
    20. el = new EditLog;
    21. // Signal connection from EditLog class
    22. connect(el, SIGNAL(addLookup(int)), this, SLOT(updateLog(int)));
    23.  
    24. void MainWindow::updateLog(int newID)
    25. {
    26. // code updates log ok
    27. }
    To copy to clipboard, switch view to plain text mode 

    Here is the code that seems to do nothing:
    Qt Code:
    1. // part of openlog.h // this is a dialog
    2.  
    3. signals:
    4. void callOpenLog(QString);
    5.  
    6. // part of openlog.cpp
    7.  
    8. void OpenLog::on_pbOpen_clicked()
    9. {
    10. QString openLog = ui->cbLogName->currentText();
    11. emit callOpenLog(openLog);
    12. close(); // the dialog window
    13. }
    14.  
    15. // part of mainwindow.h
    16. public:
    17. OpenLog *ol;
    18.  
    19. // part of mainwindow.cpp
    20. ol = new OpenLog;
    21. connect(ol, SIGNAL(callOpenLog(QString)), this, SLOT(openLog(QString)));
    22.  
    23. void MainWindow::openLog(QString logName) // never called
    24. {
    25. // code would open the log if the signal was received
    26. }
    To copy to clipboard, switch view to plain text mode 

    Could this have something to do with the fact that the signal from the class that works comes from a dialog window that does not get closed after the signal is sent and the one that does not work comes from a dialog class where the window gets closed right after the signal is sent. If so, how to make it work? Or what else is wrong?
    Last edited by waynew; 5th June 2010 at 01:12. Reason: code error

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Signal and Slot Problem

    Well, just try without close(); and see what happens.

    I'm not sure about this, but your widget might be deleted on close(), if it works, you can use hide(); and the widget will be deleted by the parent.

  3. #3
    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: Signal and Slot Problem

    Dumb mistakes I typically make... Has MainWindow::openLog() been declared as a slot? Are any error messages regarding the connect() call output at run time?

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal and Slot Problem - SOLVED

    Well, I tried the hide() on the dialog instead of close(). Same result, no action.
    Sorry Chris, I was afraid I would forget to post some part of the code. Yes, openLog(QString) is declared as a public slot in mainwindow.h, however I have forgotten that in the past too.

    What really puzzles me is that the signal from editlog works perfectly. But the structure between the techniques used in editlog and openlog seem to be identical. Got to be some simple mistake.

    UPDATE: problem solved. Yep, it was a simple mistake.
    the openlog dialog was created on the stack. when I switched it to the heap and moved the connect statement to the code where it is created, all works fine.
    Thanks for your ideas.
    Last edited by waynew; 5th June 2010 at 13:38. Reason: Problem solved

Similar Threads

  1. signal / slot problem
    By franco.amato in forum Newbie
    Replies: 13
    Last Post: 8th December 2009, 18:10
  2. Problem with New Slot/Signal in QTDesigner4.4.3
    By durgarao in forum Qt Tools
    Replies: 1
    Last Post: 27th December 2008, 12:39
  3. signal-slot problem with QVector
    By stefan in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2008, 13:58
  4. problem with signal/slot mechanismus
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 7th March 2008, 19:05
  5. problem with signal/slot
    By ihoss in forum Newbie
    Replies: 2
    Last Post: 24th August 2007, 22:59

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.