[SOLVED] Simplest QApplication inheritance: segmentation fault on Linux only



  • Hello,

    I came across a strange behaviour. I inherit from QApplication in order to be able to intercept an application-wide esc-key.
    Here I create the QApplication:

    @myQApplicationClass* myApp=new myQApplicationClass(qApp_argc,qApp_argv);@

    and this is my 'myQApplicationClass':

    @myQApplicationClass::myQApplicationClass(int argc ,char** argv) : QApplication(argc,argv)
    {
    }

    myQApplicationClass::~myQApplicationClass()
    {
    }

    bool myQApplicationClass::notify(QObject* object,QEvent* event)
    {
    if(event->type()==QEvent::KeyPress)
    {
    QKeyEvent* keyEvent=static_cast<QKeyEvent*>(event);
    int key=keyEvent->key();
    if (key==Qt::Key_Escape)
    // do something
    }
    return(QApplication::notify(object,event));
    };
    @

    The first time I call a 'show' (e.g. on the splash screen or any other) the application crashes, but only on Linux (32 and 64bits). Windows works fine. When removing the 'notify' function, I get the same result. Only if I use myApp like follows, will it not crash on Linux:

    @QApplication* myApp=new QApplication(qApp_argc,qApp_argv); // i.e. regular way to call it@

    (with above line, the problems on Linux are resolved, but I don't have a global key press handler anymore).
    Am I doing something wrong? Missing something? Or did I discover a bug?

    Thanks


  • Moderators

    constructor signature is wrong, it should be:
    @
    myQApplicationClass(int & argc ,char** argv)
    @
    (pass argc as a reference instead of a copy)

    And btw. you can simply avoid subclassing QApplication at all and install an eventFilter on it and filter for the Esc-KeyEvent.
    @
    qApp->installEventFilter(object);
    @
    The event filter is called right after QApplication::notify() but still before any event gets delivered to a widget.



  • A big big thank you Raven-worx! It's the '&' that I forgot that was the culprit!


Log in to reply
 

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