Results 1 to 5 of 5

Thread: Connecting to a signal that does not have a pointer to the class

  1. #1
    Join Date
    Apr 2008
    Posts
    73
    Thanks
    11
    Thanked 7 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Connecting to a signal that does not have a pointer to the class

    Hi again,
    I'm having problems connecting to a class that does not have a pointer.
    I created an instance of a class and I thought that I didn't need it to be a pointer, so I will just create the object in my header file. But when I attempt to connect a signal from that instance the compiler throws an error:

    Qt Code:
    1. xBeeComms comms(); // Object
    2. ...
    3. connect( &comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) ); // In the constructor
    To copy to clipboard, switch view to plain text mode 

    The error I get is:
    Qt Code:
    1. ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&messageGenerator::comms’
    To copy to clipboard, switch view to plain text mode 

    So I change the code to:
    Qt Code:
    1. connect( &messageGenerator::comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) );
    To copy to clipboard, switch view to plain text mode 
    And I now get the error:
    Qt Code:
    1. error: no matching function for call to ‘messageGenerator::connect(xBeeComms (messageGenerator::*)(), const char [30], messageGenerator* const, const char [30])’
    To copy to clipboard, switch view to plain text mode 
    Any idea's how to get it working?
    Best Regards,
    Phil Winder
    www.philwinder.com

  2. #2
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connecting to a signal that does not have a pointer to the class

    The problem with just a simple object (as you do create it) is that it will be destroyed when you will leave the current block (probably your method here).

    So, you ask Qt to connect a signal from a object that is going to be destroyed.

    What you could write might be something like:
    Qt Code:
    1. #
    2. xBeeComms *comms = xBeeComms(); // Object
    3. #
    4. ...
    5. #
    6. connect( comms, SIGNAL( dataReceived( QbyteArray * ) ), this, SLOT( receivedData( QByteArray * ) ) ); // In the constructor
    7.  
    8. delete comms; // <- really if you need it to be destroyed
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2008
    Posts
    73
    Thanks
    11
    Thanked 7 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting to a signal that does not have a pointer to the class

    Sorry, its declared in the header file, meaning that it should be available for the entire life of the class. And since thats the only amount of time I require it for, thats ok.

    I know I could just create a pointer to that class and create a new object on the heap with new, but I dont think we should have to?

    Phil
    Best Regards,
    Phil Winder
    www.philwinder.com

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to a signal that does not have a pointer to the class

    The problem is with the declaration.
    xBeeComms comms(); // Object
    Its NOT an object.... It is a function prototype... telling compiler that comms is a function of return type xBeeComms.

    Just write it simply as -
    xBeemComms comms;


  5. #5
    Join Date
    Apr 2008
    Posts
    73
    Thanks
    11
    Thanked 7 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting to a signal that does not have a pointer to the class

    Huh, silly me! I got an error because I didnt provide a default parameter so I automatically put the brackets in for some reason. Sorry!

    Thanks!
    Best Regards,
    Phil Winder
    www.philwinder.com

Similar Threads

  1. Replies: 12
    Last Post: 18th September 2008, 15:04
  2. pointer in a class
    By mickey in forum General Programming
    Replies: 23
    Last Post: 26th May 2008, 15:52
  3. send own class reference throught signal
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 10th January 2008, 21:55
  4. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  5. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37

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.