Results 1 to 16 of 16

Thread: Signals and Slots - why won't this constellation not work

  1. #1
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Signals and Slots - why won't this constellation not work

    Hey guys,
    I've been working with Qt for a few weeks now, still I can't get this specific Signal/Slot/Constellation running.
    I've got a class, "Analytic", which is a QObject (defined in Analytics.h) and has got a SLOT called display(), which is called at the end of another function ( in Analytics.cpp).
    Now I've got a Mainwindow (defined in MWindow.h) which has got the public SLOT draw_layer() (defined in MWindow.cpp).
    What I additionally do is

    Analytics *Analy = new Analytics();
    connect(Analy, SIGNAL(display()), this, SLOT(draw_layer()));

    this is both happening in the main function of my MWindow.cpp

    To sum it up: I've got 4 files -> 2 cpp and 2 h. In Header/File 1 I've got a signal, and in Header/File 2 I've got the public SLOT. Qt doesn't give me an error when compiling or while running, but the SLOT is just not triggered, although it works as a function.
    Thanks in advance,
    Daniel

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Signals and Slots - why won't this constellation not work

    Show us your code! One guess: did you use the Q_OBJECT macro and did you emit the signal properly?

  3. #3
    Join Date
    Jun 2010
    Posts
    23
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Signals and Slots - why won't this constellation not work

    Hey,

    your display() Slot has to be a Signal. This Signal musst be emitted if your Event is processed.
    Stelios: What the hell are you laughing at?
    Astinos: Well, you had to say it!
    Stelios: What?
    Astinos: "Fight in the shade"!
    -------------------------------------------
    If my answer has helped you, i would appreciate it if you use the thanks button.

  4. #4
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals and Slots - why won't this constellation not work

    Yeah, I would check that display() slot is defined under as a signal and that you include the Q_OBJECT macro in your class definition and that you emit your signal. (See below)
    Qt Code:
    1. class Analyic : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Analytic();
    6. void process_something()
    7. {
    8. // process something here
    9. emit signalDisplay();
    10. }
    11. signals:
    12. void signalDisplay();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Amos
    Qt Programmer Extraordinaire

    Current Work:
    Ripxx Sports Measurement Device - www.ripxx.com
    (Featured in MYTHBUSTERS on 2010-05-19 in S08E08)

  5. #5
    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: Signals and Slots - why won't this constellation not work

    Most likely you forgot to emit the display() signal... or if you emit it, check your call/connection to the function/slot in witch you emit the signal.

  6. #6
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    That was display()-thing was a typo. It is a SIGNAL


    Here is my code. I'll just post the relevant parts, but you'll habe to live with the fact, that the naming of my files/variables is german:

    DGM_Analyse.h:

    Qt Code:
    1. #ifndef DGM_ANALYSE
    2. #define DGM_ANALYSE
    3. #include "Mainheader.h"
    4.  
    5. class DGM_Analytik:public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. DGM_Analytik(QObject* = 0);
    10.  
    11. signals:
    12. void entw_anzeigen();
    13.  
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------
    DGM_Analyse.cpp:
    Well, a lot of functions in here, so the on line about the signal:


    Qt Code:
    1. emit entw_anzeigen();
    To copy to clipboard, switch view to plain text mode 

    ----------------------------------
    Hauptfenster.h: (MainWindow)

    Qt Code:
    1. #ifndef HAUPTFENSTER_H
    2. #define HAUPTFENSTER_H
    3.  
    4. #include "DGM_Analyse.h"
    5.  
    6.  
    7.  
    8. class Hauptfenster:public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Hauptfenster();
    14.  
    15.  
    16. public slots:
    17. void Entw_Layer();
    18. };
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    -----------------------------------------
    and finally Hauptfenster.cpp:

    Qt Code:
    1. Hauptfenster::Hauptfenster()
    2. {
    3.  
    4. ...
    5. Analytik = new DGM_Analytik(this);
    6. connect(Analytik, SIGNAL(entw_anzeigen()), this, SLOT(Entw_Layer()));
    7. ...
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    Hope that helped.
    Daniel
    Last edited by wysota; 13th June 2010 at 12:26. Reason: missing [code] tags

  7. #7
    Join Date
    Jun 2010
    Posts
    23
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Signals and Slots - why won't this constellation not work

    You still have to emit your signal!
    Stelios: What the hell are you laughing at?
    Astinos: Well, you had to say it!
    Stelios: What?
    Astinos: "Fight in the shade"!
    -------------------------------------------
    If my answer has helped you, i would appreciate it if you use the thanks button.

  8. #8
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    Didn't I do so by
    "emit entw_anzeigen();"?
    in the DGM_Analyse.cpp?

  9. #9
    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: Signals and Slots - why won't this constellation not work

    In file DGM_Analyse.cpp, the emit entw_anzeigen(); is within a function or slot (most likely, because you probably need to display() when the user does something... ) so check if the function is called or that slot gets connected (with button click or whatever user action it should be connected with).

  10. #10
    Join Date
    Jun 2010
    Posts
    23
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Signals and Slots - why won't this constellation not work

    Quote Originally Posted by FlashMuller View Post
    Didn't I do so by
    "emit entw_anzeigen();"?
    in the DGM_Analyse.cpp?
    Yes, but i can't see in which case you emit this signal. So it looks like you did not call this in the case you would like.
    Stelios: What the hell are you laughing at?
    Astinos: Well, you had to say it!
    Stelios: What?
    Astinos: "Fight in the shade"!
    -------------------------------------------
    If my answer has helped you, i would appreciate it if you use the thanks button.

  11. #11
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    Zlatomir is right,
    the emit command is inside a function which is a slot itself. Anyway, this works: The Slot-function is being called, doing its job and then emitting the signal. I did a Step-by-step Debug and when reaching the "emit-command" first the moc-file is being opened
    Qt Code:
    1. void DGM_Analytik::entw_anzeigen()
    2. {
    3. QMetaObject::activate(this, &staticMetaObject, 1, 0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    and then Qt jumps through the QObject.cpp file. It breaks at this point:

    Qt Code:
    1. if (!sender->d_func()->isSignalConnected(signal_index))
    2. return; // nothing connected to these signals, and no spy
    To copy to clipboard, switch view to plain text mode 

    which of course tells me that the connection doesn't seem to work, but doesn't really tell me how to fix it.
    Last edited by wysota; 13th June 2010 at 12:28. Reason: missing [code] tags

  12. #12
    Join Date
    Mar 2009
    Location
    Tennessee, US
    Posts
    41
    Thanks
    3
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals and Slots - why won't this constellation not work

    In the debug console window, it should state if the connection was made or give an error when the connect() function is called.
    Amos
    Qt Programmer Extraordinaire

    Current Work:
    Ripxx Sports Measurement Device - www.ripxx.com
    (Featured in MYTHBUSTERS on 2010-05-19 in S08E08)

  13. #13
    Join Date
    Jun 2010
    Posts
    23
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Signals and Slots - why won't this constellation not work

    It looks right for me, so you have to post your whole code, or we can only guess along.
    Stelios: What the hell are you laughing at?
    Astinos: Well, you had to say it!
    Stelios: What?
    Astinos: "Fight in the shade"!
    -------------------------------------------
    If my answer has helped you, i would appreciate it if you use the thanks button.

  14. #14
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    It should :-)
    It doesn't give an error while debugging (QObject: No such Slot or Signal) and it doesn't give any feedback when it returns from the QObject.cpp.
    Daniel

  15. #15
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    In the attachment you will find the four files. You don't have a working programm with that, but you can try to catch up with me like that. Hope the variable-names aren't a too big problem.
    Thanks

    P.S.: Please no comments about the rest of my programming ;-)
    Attached Files Attached Files

  16. #16
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - why won't this constellation not work

    Noone got an idea? (in other ords: PUSH )

Similar Threads

  1. Signals & Slots!
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2010, 01:50
  2. Problem Getting User-Defined Signals/Slots to Work
    By Radiotelephone in forum Newbie
    Replies: 2
    Last Post: 3rd August 2009, 07:14
  3. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  4. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  5. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49

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.