Results 1 to 8 of 8

Thread: Connect SLOTS in QApplication problem

  1. #1
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Exclamation Connect SLOTS in QApplication problem

    Hello,

    I am trying to inherit QApplication in my code but unfortulately i am unable to connect with SLOTS,

    I have commented the Q_OBJECT macro coz when i uncomment that macro it creates vtable problem to me.

    Any idea about this problem please help.

    .h file
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtCore/QCoreApplication>
    3.  
    4. #include <QObject>
    5. #include <QEvent>
    6. #include <QTimer>
    7. #include <QDebug>
    8.  
    9. class InactivityWatcher : public QApplication
    10. {
    11. // Q_OBJECT
    12.  
    13. public:
    14. explicit InactivityWatcher(int &argc, char *argv[]);
    15.  
    16. public:
    17. QTimer m_timer;
    18.  
    19. protected slots:
    20. void appIdleForFiveSecs();
    21.  
    22. virtual bool notify(QObject *receiver, QEvent *event);
    23. virtual bool event (QEvent * e);
    24.  
    25. };
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. InactivityWatcher::InactivityWatcher(int &argc, char *argv[]):
    2. QApplication(argc, argv)
    3. {
    4. // 3600000 => 30 minutes
    5.  
    6. m_timer.setInterval(10000);
    7. connect(&m_timer, SIGNAL(timeout()), this, SLOT(appIdleForFiveSecs()), Qt::UniqueConnection);
    8.  
    9. m_timer.start();
    10. }
    11.  
    12. void InactivityWatcher::appIdleForFiveSecs()
    13. {
    14. qDebug() << "Inactive ";
    15. m_timer.stop();
    16. }
    17.  
    18. bool InactivityWatcher::notify(QObject *receiver, QEvent *event)
    19. {
    20. if (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress) {
    21. qDebug() << "Active";
    22. m_timer.stop(); // reset timer
    23. m_timer.start();
    24. }
    25. return QApplication::notify(receiver, event);
    26. }
    27.  
    28. bool InactivityWatcher::event(QEvent *e)
    29. {
    30. return QApplication::event(e);
    31. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Connect SLOTS in QApplication problem

    signals & slots will work only if Q_OBJECT macro is included. So you need to uncomment Q_OBJECT to make your connection work.

  3. #3
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connect SLOTS in QApplication problem

    Thanks but when i uncomment Q_OBJECT macro i have got vtable reference errors.

    Qt Code:
    1. ./build\objects\main.o:main.cpp:(.text+0x122a): undefined reference to `vtable for InactivityWatcher'
    2. ./build\objects\main.o:main.cpp:(.text+0x1add): undefined reference to `vtable for InactivityWatcher'
    3. ./build\objects\inactivitywatcher.o:inactivitywatcher.cpp:(.text+0x2c): undefined reference to `vtable for InactivityWatcher'
    4. collect2: ld returned 1 exit status
    5. mingw32-make.exe[1]: *** [build\product\TouchDox.exe] Error 1
    6. mingw32-make.exe: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    please tell me if you have any idea about this problem?

  4. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Connect SLOTS in QApplication problem

    Try "clean project", then run qmake to rebuild and run.
    If your are in doubt if your moc files are deleted properly, manually delete projectName.pro.user file, debug & release folders and run qmake.

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

    karankumar1609 (9th May 2013)

  6. #5
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connect SLOTS in QApplication problem

    Thanks it works... :P
    i feel like dumb

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,715
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Connect SLOTS in QApplication problem

    virtual bool notify(QObject *receiver, QEvent *event);
    virtual bool event (QEvent * e);
    You don't intend for these to be slots, do you? Why are they included in the slots declaration of your class?

  8. #7
    Join Date
    Apr 2013
    Location
    milan
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect SLOTS in QApplication problem

    I've had the same problem today I've solved implementing explicitly a destructor... an empty one does the trick too!

    Then the 'vtable' it was defined again... whatever it is!

    I have really the G++ errors they simply means nothing...

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect SLOTS in QApplication problem

    The vtable error is typical artifact when a Q_OBJECT marker is added to a header that did not contain it at the time the header was added to the project.
    All headers with that marker need to be processed by MOC, when a header is added to the .pro file then it is checked for whether this is necessary.
    If the header at this time does not contain the marker yet, no MOC rule is generated. Adding the marker afterwards thus leaves the file only temporarily processed.

    Re-running qmake (either manually or though QtCreator's build menu) will always fix that.

    Generally it is most easily avoided by always putting the Q_OBJECT marker into classes directly or indirectly derived from QObject.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    rawfool (10th May 2013)

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 13:15
  2. How to use connect() in IDE's(slots)
    By wenn32 in forum Newbie
    Replies: 7
    Last Post: 6th July 2010, 17:21
  3. Replies: 16
    Last Post: 16th February 2010, 14:17
  4. Connect to...without slots...
    By Peppy in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 14:47
  5. Where to ::connect() QApplication::aboutToClose()
    By Jason Hamilton in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2008, 02:40

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.