Error using QApplication::notify
-
wrote on 4 Mar 2014, 08:46 last edited by
test.h
@class TestApp : public QWidget, public QApplication
{
public:
TestApp(QWidget *parent = 0);
bool notify(QObject *receiver, QEvent *event);
};@test.cpp
@bool TestApp:: notify(QObject *receiver, QEvent *event)
{
try
{if(event->type() == QEvent::KeyPress) { QKeyEvent *ke = (QKeyEvent *) event; int keyValue = ke->key(); qDebug() << "Key Value is" << keyValue; } return QApplication::notify(receiver, event); } catch(std::exception& e) { qDebug() << "Exception thrown:" << e.what(); }
}@
main.cpp
@
int main(int argc, char *argv[])
{
QApplication testQapp(argc, argv);TestApp objTestApp; objTestApp.show(); return testQapp.exec();
}@
I am trying to use function notify to receive the key events , but i am getting error
error: cannot call member function 'virtual bool QApplication::notify(QObject*, QEvent*)' without object
if i use
class TestApp : public QWidgetand getting
error: no matching function for call to 'QApplication::QApplication()'if i use
class TestApp : public QWidget, public QApplication.please suggest me how to call correct way.
-
wrote on 4 Mar 2014, 12:44 last edited by
You cannot derive a class from QObject twice. Both QApplication and QWidget derive from QObject.
Do you want to receive the key events within the widget? Use QWidget's key events like keyPressEvent.
Do you want to receive the key events for the whole applications? Take a look at QCoreApplication::setEventFilter
1/2