Results 1 to 6 of 6

Thread: Simple QTimer Question

  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Simple QTimer Question

    Hi,
    Im tryng to user QTimer in a regular class (not inherited form any QT feature),
    I want to jump to OnTimer() when timeout() is occur.
    and im looking forward the connect command...

    For example:
    class Display
    {
    Init()
    {
    QTimer *timer = new QTimer();
    timer->setInterval( 50 );
    timer->start();
    connect(???);
    }
    OnTimer()
    {
    //commands..
    }
    }

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple QTimer Question

    QTimer::timeout() is a signal, thus you need a slot for it to connect to.
    If your class does not (somehow) derive from QObject you don't and can't have any.
    So you can not connect to OnTimeout(). Solution: Do make it a QObject.

  3. #3
    Join Date
    Oct 2009
    Posts
    25
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Simple QTimer Question

    OK, so i understand right - this code i just wrote need to do it

    mytimer.h
    ************
    class MYTimer : public QObject
    {
    public:
    MYTimer(...);

    private slots:
    void OnTimer();
    private:
    QTimer *mytimer;

    };

    mytimer.cpp
    #include "mytimer.h"

    MYTimer::MYTimer(...)
    {
    mytimer = new QTimer();
    mytimer->setInterval( 50 );
    mytimer->start();
    connect(mytimer,SIGNAL(timeout()),SLOT(OnTimer())) ;
    }
    void MYTimer::OnTimer()
    {
    client->iterate();
    }

    and from my other class it looks like this
    MYTimer *timer= new MYTimer(.....);

    anyway it says that "Object::connect: No such slot QObject::OnTimer() in mytimer.cpp"

  4. #4
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    16
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple QTimer Question

    You missed the Q_OBJECT macro:
    See in the docs: Q_OBJECT

    should be in private section of your .h file

  5. The following user says thank you to Corinzio for this useful post:

    shiranraviv (8th November 2009)

  6. #5
    Join Date
    Oct 2009
    Posts
    25
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Simple QTimer Question

    Oh, you right... im sorry ... stupid mistake
    but that just cause even a bigger problem -

    "error: collect2: ld returned 1 exit status"
    with this line before it:
    "undefined reference to `vtable for MYTimer'"

    any idea what is going on in here?

    thanks alot

  7. #6
    Join Date
    Oct 2009
    Posts
    25
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Thumbs up Re: Simple QTimer Question

    Thanks alot!!!
    Its working!!!


    ohhhhhh

Similar Threads

  1. Simple QTimer help
    By philips in forum Qt Programming
    Replies: 14
    Last Post: 6th July 2015, 08:39
  2. Simple namespace question
    By JPNaude in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2009, 09:31
  3. simple MVC question!
    By landonmkelsey in forum Qt Programming
    Replies: 14
    Last Post: 19th August 2008, 12:25
  4. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

Tags for this Thread

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.