Results 1 to 6 of 6

Thread: [Signals & Slots] Custom class' signal not detected

  1. #1
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default [Signals & Slots] Custom class' signal not detected

    Hello All,

    I've looked into how to write a class with QObject's inheritance so I can use signals as I need feedback from said class. The trouble is, I'm not sure if I defined the signal correctly, as it differs from the Signals & Slots example.

    My code is as follows:

    Qt Code:
    1. class worker : public QObject{
    2. Q_OBJECT
    3. public:
    4. worker();
    5.  
    6. void execute (bool isExecuting){
    7. if (isExecuting==true){
    8. //do stuff
    9. emit scanUpdate(); //differs from the example's ValueChanged(int newValue)
    10. }
    11. else return;
    12. }
    13.  
    14. signals:
    15. void scanUpdate();
    16. }
    17.  
    18. class mainClass : public QMainWindow, Ui::MainWindow
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. mainClass() : QMainWindow()
    24. {
    25. setupUi(this);
    26. lib=new worker;
    27. connect(lib,SIGNAL(scanUpdate()),this,SLOT(pbScanUpdate()));
    28.  
    29. while(1){ lib->execute(true); }
    30.  
    31. }
    32. worker *lib;
    33. public slots:
    34. void pbScanUpdate(){
    35. //do stuff which can only occur after lib->execute has been called
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    This compiles ok with the Q_OBJECT declarations, but I never see pbScanUpdate() running. What could I be missing?


    Thanks in advance,
    Mr_Cloud
    Last edited by Mr_Cloud; 26th July 2012 at 07:58.

  2. The following user says thank you to Mr_Cloud for this useful post:

    zhy282289 (26th July 2012)

  3. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [Signals & Slots] Custom class' signal not detected

    You are missing "slot:" keyword before "void pbScanUpdate()" declaration/definition
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #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: [Signals & Slots] Custom class' signal not detected

    You are also ignoring the warning messages in your program output that will be telling you that there is no slot named pbScanUpdate().

  5. #4
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Signals & Slots] Custom class' signal not detected

    Sorry you're right. I copy-pasta'd wrong, there is actually public slots: declaration before void pbScanUpdate(). Top post edited.

  6. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Signals & Slots] Custom class' signal not detected

    Are you sure the "emit"-statement gets called?

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [Signals & Slots] Custom class' signal not detected

    It will not compile!

    You better get the code compiling and then organize the code such that you can at-least insert break-points on individual line. Your code will work perfectly if you fix the "worker" constructor (implementation is missing, how come it will compile), and class terminating ";" (It will for sure not compile).

    Also note that you have while(1) loop in ctor, you will never be able to see the window pop up. Breakpoint is the only way you can see it work.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Signals Slots and Class Pointers
    By Atomic_Sheep in forum Newbie
    Replies: 18
    Last Post: 7th September 2012, 10:08
  2. Custom QTreeWidgetItem class and signals&slots issue
    By devla in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2012, 01:15
  3. Access a class without using Signals/Slots
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2010, 12:14
  4. Replies: 12
    Last Post: 23rd June 2008, 09:05
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.