Results 1 to 15 of 15

Thread: How to define a function body to receive external slots?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default How to define a function body to receive external slots?

    I'm of a dead point with this.

    I have a slot defined into a class A.
    Qt Code:
    1. public slots:
    2. void function(long value);
    To copy to clipboard, switch view to plain text mode 
    I have a function into external class B and I want to receive this slot (and I 'd want to receive a slot from any class. )

    I dont know how to define the function body to receive this?
    If this SLOT are using a function that receives a parameter, how have I to define it into this class B function ?
    I have a private Qtimer into class B and I want to connect it to external functions.
    I hope I have explain my problem.
    Thanks
    Last edited by tonnot; 19th April 2011 at 09:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to define a function body to receive external slots?

    What do you mean with "receive" a slot?
    You can "receive" a signal, but you have to call a slot...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: How to define a function body to receive external slots?

    Sorry,

    but I don't understand.
    What do you mean with "receive a slot"??

    Are you sure tha you're not confusing slots with signals?
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to define a function body to receive external slots?

    I have ClassA with a slot (defined to connect to a signal);
    I have a fuction into ClassB that 'receive' this slot, does some things and depending on the result creates a Qtimer and connects it(timer.signal(timeout)) with the slot .
    And I dont know how to write the body of this function to receive a SLOT and later using it to connect to Qtimer::signal(timeout).
    Thanks

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to define a function body to receive external slots?

    I still don't quite get what you want, it sounds like you want to pass a function pointer over a signal, which is possible, but it all sounds very bad.
    Maybe if you explain what you want to have (in terms of functionality, NOT implementation) we can help you with a better implementation.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: How to define a function body to receive external slots?

    Quote Originally Posted by tonnot View Post
    I have a fuction into ClassB that 'receive' this slot, does some things and depending on the result creates a Qtimer and connects it(timer.signal(timeout)) with the slot .
    I still don't understand what do you mean for 'receive'.

    You can connect a signal with the slot if you've a pointer/reference to object aof Class A
    Qt Code:
    1. connect(timer, SIGNAL(timeout()), aPointer, SLOT(function())
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to define a function body to receive external slots?

    I want to have a class with (among other things) a generic function that after some calcs conects a Qtimer with external function.
    I have to declare this external function as a SLOT isn't ? Now, how can I pass this SLOT to the function I want to write ?


    class A
    Qt Code:
    1. public slots:
    2. void actualizamem(long value);
    To copy to clipboard, switch view to plain text mode 
    and I have another function:
    Qt Code:
    1. ClassB().w_timer_mem(500,1000, this, SLOT(A:actualizamem(long)));
    To copy to clipboard, switch view to plain text mode 
    And at aclass B:
    Qt Code:
    1. void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
    2. if (value <1000)
    3. {
    4. QTimer *a_timer = new QTimer();
    5. a_timer->setInterval(interval);
    6. QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
    7. a_timer->start();
    8. }
    To copy to clipboard, switch view to plain text mode 

    It does not work for me:

    Thanks
    Last edited by tonnot; 19th April 2011 at 09:58.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to define a function body to receive external slots?

    I want to have a class with (among other things) a generic function that after some calcs conects a Qtimer with external function.
    Nothing special there.
    I have to declare this external function as a SLOT isn't ?
    only if you want to connect it to signals - do you?
    Now, how can I pass this SLOT to the function I want to write ?
    Again, explain WHY you want to pass it, then we'll see if this is at all needed!
    What is the reason you want to pass a slot pointer?
    Btw, what you are after is called a call back function, which will rarely be needed with Qt because of signals/slots - and it seems you are trying to mix the two.

    EDIT:
    I think I understand now.
    You want dynamic signal/slot connections.
    Have a look here:
    http://doc.qt.nokia.com/qq/qq16-dynamicqobject.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to define a function body to receive external slots?

    Again:
    This line of code writen intro myclass A compiles (but does not work, parenthesis expected ? )
    QObject::connect(ClassB().a_timer,SIGNAL(timeout), this,SLOT(A::actualizamem(long)));

    As you can see I only want to connect a timer with a function, and I want to have all the control of timer on class B, becasue of it I want to convert the last line of code to :

    Qt Code:
    1. void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
    2. if (value <1000)
    3. {
    4. QTimer *a_timer = new QTimer();
    5. a_timer->setInterval(interval);
    6. QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
    7. a_timer->start();
    8. }
    To copy to clipboard, switch view to plain text mode 

    But I dont know how to write the body of this function neither how to call it .
    Thanks four your patience.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to define a function body to receive external slots?

    And if you do:
    Qt Code:
    1. ClassB().w_timer_mem(500,1000, this, SLOT(actualizamem(long)));
    To copy to clipboard, switch view to plain text mode 

    does it work?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: How to define a function body to receive external slots?

    You solution is good but in its code

    Qt Code:
    1. void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
    2. if (value <1000)
    3. {
    4. QTimer *a_timer = new QTimer();
    5. a_timer->setInterval(interval);
    6. QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
    7. a_timer->start();
    8. }
    To copy to clipboard, switch view to plain text mode 

    he try to connect a signal without parameters with a slot with one parameter. I think isn't possible
    A camel can go 14 days without drink,
    I can't!!!

  12. #12
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to define a function body to receive external slots?

    No, I have :

    Incompatible sender/receiver arguments
    QTimer::timeout() --> MainWindow::actualizamem(long)

    Thanks WY

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to define a function body to receive external slots?

    Well, obviously signatures have to match. What value would you expect the slot to receive as its parameter? 4? 8? 15? 16? 24? 42?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: How to define a function body to receive external slots?

    As Mcosta said maybe the parameter is the guilty.
    If I use a function without parameters , it works.
    But I'd like to can send a parameter, simply a long value, and I dont know if it is possible.
    Thanks

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

    Default Re: How to define a function body to receive external slots?

    you connect the signal to a slot without parameters. in this slot, you create your "long" value. then you call a function and pass this value as parameter.

Similar Threads

  1. Replies: 8
    Last Post: 24th February 2011, 13:22
  2. receive signal from a thread of an external program
    By bigbill in forum Qt Programming
    Replies: 5
    Last Post: 1st December 2010, 22:41
  3. Can I define "anonnymous" slots somehow?
    By agnus in forum Newbie
    Replies: 1
    Last Post: 18th November 2009, 22:36
  4. how to define the callback function in QThread?
    By cxl2253 in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2007, 10:59
  5. Replies: 1
    Last Post: 4th November 2006, 06:53

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.