Results 1 to 14 of 14

Thread: Slot

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Slot

    Hi I coded this connection, but I need to do this: float type = setBox(int)); How? thanks
    Qt Code:
    1. connect(bgroup[i], SIGNAL(clicked(int)), this, SLOT(setBox(int)));
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot

    Slots are just normal functions.

    Delare your slot as

    float setBox(int );

    and then use as normal function whereever you want.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    sorry, I just declare it so. I need to do that connect and togheter assing its returned value to var type..........
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot

    I need to do that connect and togheter assing its returned value to var type..........
    Can you explain this ??

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    that connect is right; it's works; but it return a float that I need to assign to a a variable;
    I can't call setBox() is somewhere: it must return a value only when its signal occurs....
    Regards

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot

    In that case your slot setBox(int) should emit a signal with your return value as a parameter and this signal will be connected to a slot in your bgroup[i].

    I hope you understand what i mean.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    is there a way simpler? SLOT must be declared with a return value (eg void). I thought there's a sintax to assign its returned value to a variabile...
    Last edited by mickey; 3rd June 2006 at 16:09.
    Regards

  8. #8
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slot

    Quote Originally Posted by mickey
    is there a way simpler? SLOT must be declared with a return value (eg void). I thought there's a sintax to assign its returned value to a variabile...
    Why? you must not declare return value ... on slot ...is possible to grab all data from GUI... if you set..
    http://doc.trolltech.com/4.1/signalsandslots.html

    Qt Code:
    1. **.header
    2. --------------------- to call on emit method ....
    3. /* emits */
    4. signals:
    5. void status( const QString &);
    6. void ConnectorSuccess();
    7. void SendLine();
    8. void ErrorCloseAll();
    9. void SuccessQuit();
    10. /* normal slot */
    11. private slots:
    12. void disconnected();
    13. void connected();
    14. void ReadLiner();
    15. void PutSendLine();
    16. ---------------------
    17.  
    18.  
    19. linecmd = 34,56; /* save on class int .or float ... .. */
    20. linesend = 8; /* save on class int .or float ... .. */
    21. emit SendLine(); /* switsch all case.... or grab int or float direct from GUI */
    22.  
    23.  
    24. void Smtp::PutSendLine()
    25. {
    26. int current = linesend;
    27. grab data ... linecmd
    28. qDebug() <<"### Go and Send line " << linesend;
    29. switch(current) {
    30. case 1:
    31. response = SendLineAndGrab("ehlo localhost");
    32. if (response.size() > 0) {
    33. ErrorMSG.append(response);
    34. qDebug() << "1---- " << response;
    35. linesend = 2;
    36. emit SendLine();
    37. } else {
    38. qDebug() << "Connection loost";
    39. }
    40. response ="";
    41. break;
    42. case 2:
    43. response = SendLineAndGrab("AUTH LOGIN");
    44. if (response.size() > 0) {
    45. ErrorMSG.append(response);
    46. qDebug() << "2---- " << response;
    47. linesend = 3;
    48. emit SendLine();
    49. } else {
    50. qDebug() << "Connection loost";
    51. }
    52. response ="";
    53. break;
    54. case 3:
    55. .......................
    56. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    I'm still confused; can anyone learn me why QT permits to declare a SLOT in that way? (this compile). thanks
    Qt Code:
    1. public slots:
    2. float setBox(int);
    To copy to clipboard, switch view to plain text mode 
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot

    It is allowed because slots are normal members functions... What allows connection is some extra meta-data (character strings with Qt meta object system) that is generated by moc and handled by signal/slots system. When a given signal is caught by event handlers all the connected slots (or signals as well BTW) are called one by one thus their return value can't be caught by the user... Emitting another signal is definitely the simplest way!
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    Sorry, I don' t still understand how use a signal...this is what i need do.....
    Qt Code:
    1. float setValBox(int i) {
    2. float ii;
    3. ..........
    4. return ii;
    5. }
    6.  
    7. float type;
    8. connect(bgroup[i], SIGNAL(clicked(int)), this, SLOT(type=setValBox(int)));
    To copy to clipboard, switch view to plain text mode 
    How use signal; for what? thanks
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot

    signals are declared in headers that way :
    Qt Code:
    1. class X : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void mySignal( /*Place there the parameters as for a normal function*/ );
    7. }
    To copy to clipboard, switch view to plain text mode 
    signals are basically private function (or protected I'm not sure...) whose implementation is made by moc. You can call them just as normal functions but it's better to use the "emit" keyword before them to make it clear that they are signals... BTW "emit" is just an empty macro AFAIK

    This code may look pretty ugly because I don't know exactly what you wanna do and how your classes interact...
    Qt Code:
    1. class X : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. ...
    6.  
    7. public slots:
    8. void setValBox(int val);
    9.  
    10. private slots:
    11. void setValBox(float return);
    12.  
    13. signals:
    14. void valBoxSet(float return);
    15.  
    16. private:
    17. float returnValue;
    18. }
    19.  
    20. ...
    21.  
    22. X::X()
    23. {
    24. connect(bgroup[i], SIGNAL( clicked(int) ),
    25. this , SLOT ( setValBox(int) );
    26.  
    27. connect(this, SIGNAL( valBoxSet(float) ),
    28. this, SLOT ( setValBox(float) );
    29. }
    30.  
    31. void X::setValBox(int)
    32. {
    33. float ii;
    34. ...
    35. emit setValBox(ii);
    36. }
    37.  
    38. void X::setValBox(float f)
    39. {
    40. returnValue = f;
    41. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Slot

    Hi, but for my aims this can be OK. The problem was that I don't want declare returnValue as class member, but only a local variabile........I hpe you understand my problem...
    Quote Originally Posted by fullmetalcoder
    signals are declared in headers that way :]
    Qt Code:
    1. class X : public QObject
    2. {
    3. Q_OBJECT
    4. public slots:
    5. void setValBox(int val);
    6. //private slots:
    7. // void setValBox(float return);
    8. //signals:
    9. //void valBoxSet(float return);
    10. private:
    11. float returnValue;
    12. }
    13.  
    14. X::X() {
    15. connect(bgroup[i], SIGNAL( clicked(int) ),
    16. this , SLOT ( setValBox(int) );
    17. }
    18.  
    19. void X::setValBox(int val) {
    20. if (val == 0) returnValue = 1.0; //val has to be 0 or 1
    21. else returnValue = 0.0;
    22. }
    To copy to clipboard, switch view to plain text mode 
    but what's diference between those:
    Qt Code:
    1. void setValBox(float);
    2. void setValBox(float return);
    To copy to clipboard, switch view to plain text mode 
    Regards

  14. #14
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    2
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Slot

    A slot can't have a return value because you can attach multiple slots (=event-handlers) to the same signal (=event). Which one would Qt have to return? Besides, slots can run at a later time in the event queue, when your original function already ended.

    It's recommended to find a different design that doesn't need return values. In your example, I'd rather use an interface:

    First you have a base class that defines the interface all subclasses should confirm to.
    Qt Code:
    1. class BoxHandler {
    2. virtual float setBox(int) = 0;
    3. }
    To copy to clipboard, switch view to plain text mode 

    Then you can define multiple versions of your box function
    Qt Code:
    1. class BoxImplementation1 : public BoxHander {
    2. virtual float setBox(int);
    3. }
    4.  
    5. class BoxImplementation2 : public BoxHander {
    6. virtual float setBox(int);
    7. }
    To copy to clipboard, switch view to plain text mode 

    And then code each method:
    Qt Code:
    1. float BoxImplementation1::setBox(int x) { ... }
    2. float BoxImplementation2::setBox(int x) { ... }
    To copy to clipboard, switch view to plain text mode 

    And the original class can use any reference, as long as it implements the BoxHandler interface:
    Qt Code:
    1. void demo( BoxHandler *handler )
    2. {
    3. float y = handler->setBox(x);
    4. }
    To copy to clipboard, switch view to plain text mode 
    The C++ runtime will automatically know whether it calls the first or second function:

    Qt Code:
    1. void caller()
    2. {
    3. BoxImplemtation2 handler;
    4. demo( handler );
    5. }
    To copy to clipboard, switch view to plain text mode 

    Off course, the BoxImplementation classes may also be part of your normal classes:

    Qt Code:
    1. class MyApplication : public QApplication, public BoxHandler {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    Making the call something like:
    Qt Code:
    1. void caller()
    2. {
    3. demo( myApplicationObject );
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by vdboor; 4th June 2006 at 13:22.
    Working on KMess, a MSN Messenger client for Linux/KDE.

Similar Threads

  1. Replies: 2
    Last Post: 4th May 2006, 20:17
  2. Invoke a slot by its name in QString...
    By sunil.thaha in forum Qt Programming
    Replies: 4
    Last Post: 6th April 2006, 10:28
  3. SLOT and QPushButton
    By mickey in forum Qt Programming
    Replies: 15
    Last Post: 15th February 2006, 07:46
  4. SLOT or functions
    By mickey in forum Newbie
    Replies: 1
    Last Post: 8th February 2006, 19:45
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 19:52

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.