Results 1 to 9 of 9

Thread: signals and slots linked api

  1. #1
    Join Date
    Jun 2007
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default signals and slots linked api

    I have a derived class of QWidget, MyClass which uses another api. That api has various methods including a counter or timer which returns a continuously updated float value.

    I'm trying to connect those floats to a QWidget gui object such as QLCDnumber, QLineEdit, or QLabel to display the updated values.

    Do I need a signal to connect to a gui widget slot? I'm having trouble finding a signal which would emit values from the object which returns floats from the linked api. I tried using the display method for a Qt gui object but those do not update continuously.

    Am I going in the right direction with this? Should I make MyClass derive from a different parent class? thanks.

  2. #2
    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: signals and slots linked api

    You should declare your own signal and emit it in your code.

  3. #3
    Join Date
    Jun 2007
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: signals and slots linked api

    Thank You. I gave this a shot, but get "no such signal error".

    Qt Code:
    1. class myClass : public QWidget
    2.  
    3. public:
    4. QLabel *myLabel;
    5. double timeVal;
    6. int runapi();
    7.  
    8. public slots:
    9. void setNum (double num);
    10. signals:
    11. void mySignal(double newnum);
    12. private:
    13. API *myapi;
    14. -----
    15. myClass::myClass()
    16. {
    17. myapi = new API;
    18. mylabel = new QLabel;
    19. connect (mylabel, SIGNAL ( mySignal(double) ),
    20. mylabel, SLOT ( setNum(double) ) );
    21. }
    22. int myClass::runapi()
    23. {
    24. timeVal->myapi->someMethod();
    25. mylabel->setNum(timeVal);
    26.  
    27. }
    28. void myClass::setNum (double num)
    29. {
    30. emit mySignal(num);
    31. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: signals and slots linked api

    You forgot the Q_OBJECT macro.

  5. #5
    Join Date
    Jun 2007
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: signals and slots linked api

    I have the macro, but omitted it in pseudo-code ex., sorry.
    Exact error is: Object::connect: No such signal QLabel::mySignal(double).
    Is there something else I'm missing to create my own signal to connect to a QT slot?

    Qt Code:
    1. //myclass.h
    2.  
    3. #ifndef MYCLASS_H
    4. #define MYCLASS_H
    5. #include <QWidget>
    6. #include "API.hpp"
    7. class QLabel;
    8.  
    9. class myClass : public QWidget
    10. Q_OBJECT
    11. public:
    12. QLabel *mylabel;
    13. double timeVal;
    14. int runapi();
    15. public slots:
    16. void setNum (double num);
    17. signals:
    18. void mySignal(double newnum);
    19. private:
    20. API *myapi;
    21. -----
    22. //myclass.cpp
    23.  
    24. #include "myclass.h"
    25.  
    26. myClass::myClass()
    27. {
    28. myapi = new API;
    29. mylabel = new QLabel;
    30. connect (mylabel, SIGNAL ( mySignal(double) ),
    31. mylabel, SLOT ( setNum(double) ) );.
    32. }
    33. int myClass::runapi()
    34. {
    35. timeVal->myapi->someMethod(); //returns double
    36. mylabel->setNum(timeVal);
    37. }
    38. void myClass::setNum (double num)
    39. {
    40. emit mySignal(num);
    41. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2007
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: signals and slots linked api

    I noticed one problem which was "this" or myclass is where mySignal was declared and I'm trying to connect it to mylabel or QLabel. But back to original problem which is cannot seem to get QLCDNumber, QLabel, or QLineEdit to display running update in realtime from linked api method which returns double h:m:s:ms, now that I have mySignal working.

    Qt Code:
    1. connect (this, SIGNAL ( mySignal(double) ), mylabel, SLOT ( setNum(double) ) );.
    To copy to clipboard, switch view to plain text mode 
    Last edited by jhearon; 1st November 2008 at 22:15.

  7. #7
    Join Date
    Jun 2007
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: signals and slots linked api

    I discovered I need to repaint() to update the widget. In my code (not shown) the call to setNum is within a while loop, thus the widget gets continuously updated; but it is very slow at the moment and need to figure out what is causing the slow down. Will end this thread here. Thanks.

    Qt Code:
    1. void myClass::setNum (double num)
    2. {
    3. emit mySignal(num);
    4. mylabel->repaint();
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: signals and slots linked api

    I think you need to redesign your class. While loops in the gui thread are a bad thing to do. If you are a commercial Qt user, I suggest taking a look at the latest Qt Quarterly issue (#27).

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: signals and slots linked api

    Quote Originally Posted by jhearon View Post
    I discovered I need to repaint() to update the widget. In my code (not shown) the call to setNum is within a while loop
    Rather than calling repaint() here, you should allow event loop to process events in that while loop.

    See QCoreApplication::processEvents() and consider dropping that while loop in favor of a timer.

Similar Threads

  1. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 11:31
  2. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 02:45
  3. Signals and Slots Across Threads
    By themusicalguy in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 12:16
  4. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 14:57
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 09:12

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.