Results 1 to 8 of 8

Thread: Counter widget

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Counter widget

    Hi,

    I'm trying to make a widget to handle time.

    timer.h
    Qt Code:
    1. #ifndef Timer_H
    2. #define Timer_H
    3.  
    4. #include <QWidget>
    5. class QLabel;
    6.  
    7. class Timer : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Timer();
    13. int getHour();
    14. int getMinute();
    15. int getSecond();
    16.  
    17. int secondValue() const;
    18.  
    19. void show();
    20.  
    21. public slots:
    22. void addSecond();
    23.  
    24. signals:
    25. void secondChanged(int);
    26.  
    27. private:
    28. int hour;
    29. int minute;
    30. int second;
    31.  
    32. QLabel *l;
    33. };
    34.  
    35. #endif
    To copy to clipboard, switch view to plain text mode 
    -----------------------------------------------------

    timer.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "timer.h"
    3.  
    4. Timer::Timer() : QWidget()
    5. {
    6. hour = 0;
    7. minute = 0;
    8. second = 0;
    9.  
    10. l = new QLabel();
    11. }
    12.  
    13. void Timer::addSecond()
    14. {
    15. second++;
    16.  
    17. if(second == 60)
    18. {
    19. minute++;
    20. second = 0;
    21. if(minute == 60)
    22. {
    23. hour++;
    24. minute = 0;
    25. }
    26. }
    27.  
    28. emit secondChanged(second);
    29. }
    30.  
    31. int Timer::getHour()
    32. {
    33. return hour;
    34. }
    35.  
    36. int Timer::getMinute()
    37. {
    38. return minute;
    39. }
    40.  
    41. int Timer::getSecond()
    42. {
    43. return second;
    44. }
    45.  
    46. int Timer::secondValue() const
    47. {
    48. return second;
    49. }
    50.  
    51. void Timer::secondChanged(int secondValue)
    52. {
    53. l->setNum(secondValue);
    54. }
    55.  
    56. void Timer::show()
    57. {
    58. l->show();
    59. }
    To copy to clipboard, switch view to plain text mode 

    I'm getting this error:

    debug\moc_timer.o(.text+0x1e0): In function `ZN5Timer13secondChangedEi':
    C:/Projetos/backlog/debug/moc_timer.cpp:77: multiple definition of `Timer::secon
    dChanged(int)'
    debug\timer.o(.text+0x46a):C:/Projetos/backlog/timer.cpp:52: first defined here
    collect2: ld returned 1 exit status
    mingw32-make[1]: *** [debug\backlog.exe] Error 1
    mingw32-make[1]: Leaving directory `C:/Projetos/backlog'
    mingw32-make: *** [debug] Error 2

    What should I do?!?
    Last edited by eu.x; 27th February 2007 at 18:52. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Counter widget

    You don't implement function body for signals (the meta object compiler does it for you behind the curtains). So the declaration of secondChanged() is enough.

    PS. Please use [ code ] -tags around code blocks to make them more readable.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    eu.x (27th February 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Counter widget

    So how do I change values on other objects? For example, I need to update a QLabel everytime secondChanged signal is called.

    My new code is:

    timer.h
    Qt Code:
    1. #ifndef Timer_H
    2. #define Timer_H
    3.  
    4. #include <QWidget>
    5. class QLabel;
    6.  
    7. class Timer : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Timer();
    13. int getHour();
    14. int getMinute();
    15. int getSecond();
    16.  
    17. int secondValue() const;
    18.  
    19. void show();
    20.  
    21. public slots:
    22. void addSecond();
    23.  
    24. signals:
    25. void secondChanged(int);
    26.  
    27. private:
    28. int hour;
    29. int minute;
    30. int second;
    31.  
    32. QLabel *l;
    33. };
    34.  
    35. #endif
    To copy to clipboard, switch view to plain text mode 

    -----------------------------------------------------------

    timer.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "timer.h"
    3.  
    4. Timer::Timer() : QWidget()
    5. {
    6. hour = 0;
    7. minute = 0;
    8. second = 0;
    9.  
    10. l = new QLabel();
    11. }
    12.  
    13. void Timer::addSecond()
    14. {
    15. second++;
    16. if(second == 60)
    17. {
    18. minute++;
    19. second = 0;
    20. if(minute == 60)
    21. {
    22. hour++;
    23. minute = 0;
    24. }
    25. }
    26. emit secondChanged(second);
    27. }
    28.  
    29. int Timer::getHour()
    30. {
    31. return hour;
    32. }
    33.  
    34. int Timer::getMinute()
    35. {
    36. return minute;
    37. }
    38.  
    39. int Timer::getSecond()
    40. {
    41. return second;
    42. }
    43.  
    44. int Timer::secondValue() const
    45. {
    46. return second;
    47. }
    48.  
    49. void Timer::show()
    50. {
    51. l->show();
    52. }
    To copy to clipboard, switch view to plain text mode 

    -----------------------------------------------

    main.cpp
    Qt Code:
    1. ...
    2. Timer *t = new Timer();
    3. QTimer *qTimer = new QTimer();
    4. QObject::connect(qTimer, SLOT(timeout()), t, SIGNAL(addSecond()));
    5. QObject::connect(t, SLOT(secondChanged(int)), l, SIGNAL(setNum(int)));
    6. qTimer->start(1000);
    7. l->show();
    8. ...
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Counter widget

    I'd suggest using the timerEvent here instead of a standalone QTimer object.

  6. #5
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Counter widget

    Quote Originally Posted by wysota View Post
    I'd suggest using the timerEvent here instead of a standalone QTimer object.
    Do you have some example?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Counter widget

    Well, your widget is so simple it might serve as an example

    Please take a look at QObject::timerEvent() reference. It has all you need along an example use of startTimer() and timerEvent().

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

    Default Re: Counter widget

    Quote Originally Posted by eu.x View Post
    For example, I need to update a QLabel everytime secondChanged signal is called.
    You don't call signals --- you emit them. If you want to react on a signal, you have to connect it to a slot and do what you need in that slot.

    Quote Originally Posted by eu.x View Post
    QObject::connect(qTimer, SLOT(timeout()), t, SIGNAL(addSecond()));
    QObject::connect(t, SLOT(secondChanged(int)), l, SIGNAL(setNum(int)));
    It should be:
    Qt Code:
    1. QObject::connect( qTimer, SIGNAL(timeout()), t, SLOT(addSecond()) );
    2. QObject::connect( t, SIGNAL(secondChanged(int)), l, SLOT(setNum(int)) );
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jacek for this useful post:

    eu.x (27th February 2007)

  10. #8
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Counter widget

    Thank you jacek, I was confusing slots and signals.

Similar Threads

  1. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00
  2. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. Forwarding mouse events to another widget.
    By yogeshm02 in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2006, 13:25
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.