Results 1 to 10 of 10

Thread: Getting a timer's thing to work....

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Getting a timer's thing to work....

    Hello, this is a terminal application i'm trying to make:
    Qt Code:
    1. #include <QtCore/QTimer>
    2. #include <QCoreApplication>
    3. #include <QtCore/QObject>
    4. #include <QtCore/QDebug>
    5.  
    6. class checker : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. checker(QObject * o = 0) : QObject(o) {}
    11.  
    12. public slots:
    13. void check() {
    14. qDebug() << "i am going through the check() function right now!\n";
    15. }
    16. };
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QCoreApplication a(argc, argv);
    21. QTimer * timer = new QTimer;
    22. QObject::connect(timer, SIGNAL(timeout()),new checker(timer), SLOT(checker::check()));
    23. timer->start(1000);
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    I get this error:
    Qt Code:
    1. error: undefined reference to `vtable for checker'
    To copy to clipboard, switch view to plain text mode 

    Can anybody explain me why?
    Thanks a lot!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting a timer's thing to work....

    Try to implement check function in source code, not in header

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

    hakermania (3rd June 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting a timer's thing to work....

    meta object code for class is not generated, you'll need to call moc on this source yourself:
    Qt Code:
    1. moc file.cpp >> moc_file.cpp
    To copy to clipboard, switch view to plain text mode 
    and include this file in your source:
    Qt Code:
    1. int main{...}
    2.  
    3. #include "moc_file.cpp"
    To copy to clipboard, switch view to plain text mode 

    edit:
    SLOT statement is wrong, you should not include class name, only method name:
    Qt Code:
    1. QObject::connect(timer, SIGNAL(timeout()),new checker(timer), SLOT(check()));
    2. // not checker::check(), because it will lead to checker::checker::check() in the end :)
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 3rd June 2011 at 11:51.

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

    hakermania (3rd June 2011)

  6. #4
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting a timer's thing to work....

    Thanks for your suggestions!
    stampede, when should I run the 'moc' command? Before compiling, right?
    But still, I get flood of error messages:
    Qt Code:
    1. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
    2. In file included from main.cpp:5:0:
    3. moc_main.cpp:42:19: error: ‘checker’ has not been declared
    4. moc_main.cpp:51:20: error: ‘checker’ has not been declared
    5. moc_main.cpp:51:42: error: non-member function ‘const QMetaObject* metaObject()’ cannot have cv-qualifier
    6. /usr/include/qt4/QtCore/qobject.h: In function ‘const QMetaObject* metaObject()’:
    7. /usr/include/qt4/QtCore/qobject.h:296:33: error: ‘QScopedPointer<QObjectData> QObject::d_ptr’ is protected
    8. moc_main.cpp:53:21: error: within this context
    9. /usr/include/qt4/QtCore/qobject.h:296:33: error: invalid use of non-static data member ‘QObject::d_ptr’
    10. moc_main.cpp:53:21: error: from this location
    11. /usr/include/qt4/QtCore/qobject.h:296:33: error: ‘QScopedPointer<QObjectData> QObject::d_ptr’ is protected
    12. moc_main.cpp:53:50: error: within this context
    13. /usr/include/qt4/QtCore/qobject.h:296:33: error: invalid use of non-static data member ‘QObject::d_ptr’
    14. moc_main.cpp:53:50: error: from this location
    15. moc_main.cpp: At global scope:
    16. moc_main.cpp:56:7: error: ‘checker’ has not been declared
    17. moc_main.cpp: In function ‘void* qt_metacast(const char*)’:
    18. moc_main.cpp:60:47: error: expected type-specifier before ‘checker’
    19. moc_main.cpp:60:47: error: expected ‘>’ before ‘checker’
    20. moc_main.cpp:60:47: error: expected ‘(’ before ‘checker’
    21. moc_main.cpp:60:47: error: ‘checker’ was not declared in this scope
    22. moc_main.cpp:60:55: error: expected primary-expression before ‘>’ token
    23. moc_main.cpp:60:57: error: invalid use of ‘this’ in non-member function
    24. moc_main.cpp:60:63: error: expected ‘)’ before ‘;’ token
    25. moc_main.cpp:61:40: error: cannot call member function ‘virtual void* QObject::qt_metacast(const char*)’ without object
    26. moc_main.cpp: At global scope:
    27. moc_main.cpp:64:5: error: ‘checker’ has not been declared
    28. moc_main.cpp: In function ‘int qt_metacall(QMetaObject::Call, int, void**)’:
    29. moc_main.cpp:66:43: error: cannot call member function ‘virtual int QObject::qt_metacall(QMetaObject::Call, int, void**)’ without object
    30. moc_main.cpp:71:23: error: ‘check’ was not declared in this scope
    31. moc_main.cpp: In function ‘void* qt_metacast(const char*)’:
    32. moc_main.cpp:62:1: warning: control reaches end of non-void function
    33. moc_main.cpp: In function ‘const QMetaObject* metaObject()’:
    34. moc_main.cpp:54:1: warning: control reaches end of non-void function
    35. make: *** [main.o] Error 1
    To copy to clipboard, switch view to plain text mode 
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting a timer's thing to work....

    Yes, before compilation, redirect the moc output to file, and include this file in your source. I've just tried with your source code, it worked ok.
    If you want to make it done automatically, create a header, add it to HEADERS list in .pro file and put the class definition there.
    edit:
    remember to include "moc_main.cpp" AFTER the class declaration, if you want to do it this way

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting a timer's thing to work....

    Or you could just put #include "foo.moc" at the end of your source file and run QMake, then it'll do it all for you automatically, including running MOC. foo is the name of your source file.

  9. #7
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Getting a timer's thing to work....

    I think the main problem you have is that the slot function check() is implemented as an "inline" rather than a separate method. This is OK for regular methods, but not slots, due to the way the MOC compiler works. Try something like this:
    Qt Code:
    1. class checker : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. checker(QObject * o = 0) : QObject(o) {}
    6.  
    7. public slots:
    8. void check();
    9. };
    10.  
    11. void checker::check() {
    12. qDebug() << "i am going through the check() function right now!\n";
    13. }
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QCoreApplication a(argc, argv);
    18. QTimer * timer = new QTimer;
    19. QObject::connect(timer, SIGNAL(timeout()),new checker(timer), SLOT(checker::check()));
    20. timer->start(1000);
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    (And, of course, fix your connect().)

  10. #8
    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: Getting a timer's thing to work....

    Quote Originally Posted by DanH View Post
    I think the main problem you have is that the slot function check() is implemented as an "inline" rather than a separate method.
    Hmm.... really?
    This is OK for regular methods, but not slots, due to the way the MOC compiler works.
    Would you care to explain?

    Qt Code:
    1. #include <QtCore>
    2.  
    3.  
    4. class Dummy : public QObject {
    5. Q_OBJECT
    6. public:
    7. Dummy() : QObject() {
    8. connect(this, SIGNAL(sig()), this, SLOT(print()));
    9. }
    10. void emitASignal() { emit sig(); }
    11. public slots:
    12. inline void print() {
    13. qDebug() << Q_FUNC_INFO;
    14. }
    15.  
    16. signals:
    17. void sig();
    18.  
    19. };
    20.  
    21. #include "main.moc"
    22.  
    23. int main(int argc, char **argv){
    24. Dummy d;
    25. d.emitASignal();
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    $ ./test
    void Dummy::print()
    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. #9
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Getting a timer's thing to work....

    What happens if you move the connect() call into main?

  12. #10
    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: Getting a timer's thing to work....

    It works, of course.

    Qt Code:
    1. #include <QtCore>
    2.  
    3.  
    4. class Dummy : public QObject {
    5. Q_OBJECT
    6. public:
    7. Dummy() : QObject() {
    8. // connect(this, SIGNAL(sig()), this, SLOT(print()));
    9. }
    10. void emitASignal() { emit sig(); }
    11. public slots:
    12. inline void print() {
    13. qDebug() << Q_FUNC_INFO;
    14. }
    15.  
    16. signals:
    17. void sig();
    18.  
    19. };
    20.  
    21. #include "main.moc"
    22.  
    23. int main(int argc, char **argv){
    24. Dummy d;
    25. QObject::connect(&d, SIGNAL(sig()), &d, SLOT(print()));
    26. d.emitASignal();
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 

    $ ./test
    void Dummy::print()
    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.


Similar Threads

  1. a QProcess thing
    By jajdoo in forum Newbie
    Replies: 4
    Last Post: 23rd March 2011, 15:25
  2. Move that thing!
    By Archa4 in forum Newbie
    Replies: 8
    Last Post: 23rd February 2011, 14:22
  3. KDChart. What do you thing about?
    By Nadia in forum Qt-based Software
    Replies: 1
    Last Post: 15th February 2011, 01:55
  4. timer problem(timer does not work)
    By masuk in forum Newbie
    Replies: 6
    Last Post: 14th February 2011, 05:00
  5. There is QTimeEdit another thing in Qt4
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2006, 16:06

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.