Error using QApplication::notify



  • 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 QWidget

    and 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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.