PDA

View Full Version : Connect SLOTS in QApplication problem



karankumar1609
8th May 2013, 08:06
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


#include <QtGui/QApplication>
#include <QtCore/QCoreApplication>

#include <QObject>
#include <QEvent>
#include <QTimer>
#include <QDebug>

class InactivityWatcher : public QApplication
{
// Q_OBJECT

public:
explicit InactivityWatcher(int &argc, char *argv[]);

public:
QTimer m_timer;

protected slots:
void appIdleForFiveSecs();

virtual bool notify(QObject *receiver, QEvent *event);
virtual bool event (QEvent * e);

};


.cpp file


InactivityWatcher::InactivityWatcher(int &argc, char *argv[]):
QApplication(argc, argv)
{
// 3600000 => 30 minutes

m_timer.setInterval(10000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(appIdleForFiveSecs()), Qt::UniqueConnection);

m_timer.start();
}

void InactivityWatcher::appIdleForFiveSecs()
{
qDebug() << "Inactive ";
m_timer.stop();
}

bool InactivityWatcher::notify(QObject *receiver, QEvent *event)
{
if (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress) {
qDebug() << "Active";
m_timer.stop(); // reset timer
m_timer.start();
}
return QApplication::notify(receiver, event);
}

bool InactivityWatcher::event(QEvent *e)
{
return QApplication::event(e);
}

rawfool
8th May 2013, 08:11
signals & slots will work only if Q_OBJECT macro is included. So you need to uncomment Q_OBJECT to make your connection work.

karankumar1609
8th May 2013, 08:32
Thanks but when i uncomment Q_OBJECT macro i have got vtable reference errors.



./build\objects\main.o:main.cpp:(.text+0x122a): undefined reference to `vtable for InactivityWatcher'
./build\objects\main.o:main.cpp:(.text+0x1add): undefined reference to `vtable for InactivityWatcher'
./build\objects\inactivitywatcher.o:inactivitywatche r.cpp:(.text+0x2c): undefined reference to `vtable for InactivityWatcher'
collect2: ld returned 1 exit status
mingw32-make.exe[1]: *** [build\product\TouchDox.exe] Error 1
mingw32-make.exe: *** [release] Error 2


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

rawfool
8th May 2013, 08:40
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.

karankumar1609
8th May 2013, 08:50
Thanks it works... :P
i feel like dumb :rolleyes:

d_stranz
8th May 2013, 15:44
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?

fanoI
8th May 2013, 19:31
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...

anda_skoa
9th May 2013, 15:41
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,
_