Calling function from reimplemented QApplication / notify

  • Hi

    I am having issues calling a function from my main class from the reimplemented QApplication / notifier.
    I am getting:
    @main.cpp:(.text._ZN13MyApplication6notifyEP7QObjectP6QEvent[MyApplication::notify(QObject*, QEvent*)]+0x94): undefined reference to `TouchEventForTCO(int, int, int)'
    collect2: ld returned 1 exit status@

    code snippet:
    @class MyApplication : public QApplication
    MyApplication(int &argc, char **argv ) : QApplication(argc, argv)

    virtual bool notify ( QObject *receiver, QEvent *event )
      switch( event->type() )
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::MouseMove:
        case QEvent::MouseButtonDblClick:
          { QMouseEvent *TEvent = static_cast<QMouseEvent *>(event);
            QPoint cursorPos = TEvent->globalPos();
            TouchEventForTCO(event->type(), cursorPos.rx(), cursorPos.ry());
            qDebug("Touch/Mouse event type: %d at %d, %d", event->type(), cursorPos.rx(), cursorPos.ry());


    Any idea what I am doing wrong?

  • Moderators

    Your linker does not see TouchEventForTCO in the libraries/ objects it was given. You probably should include the library where that method comes from.

  • Yeah, that's what I know as well so far.
    TouchEventForTCO is in class QTGUI_MainWindow and the code snippet posted before in in the qtgui_mainwindow.h where QTGUI_MainWindow is defined. The function itself is in qtgui_mainwindow.cpp and I added an extern to the qtgui_mainwindow.h.

    I just dont know what is wrong or incomplete ?!?

  • Moderators

    Again: the error you are getting does not come from the compiler: so all the C++ includes are fine. The linker does not see the (already compiled) object code, which means something is wrong in your build setup. Either a library not included, or qtgui_mainwindow.o is not present.

  • Hmmm...
    If I only comment out the call
    @TouchEventForTCO(event->type(), cursorPos.rx(), cursorPos.ry());@

    everything builds and runs perfect.
    qtgui_mainwindow.o is present.

  • run qmake?

  • qmake didn't help.

  • Anybody else an idea what I have set wrong to have the linker giving this error?

  • Solved it. Changed call to:
    @QTGUI_MainWindow::TouchEventForTCO(event->type(), cursorPos.rx(), cursorPos.ry()); @
    and made TouchEventForTCO() static and public.

    I don't know if this is the "correct" way .. however, it works :)
    Thanks everybody for their help.

  • Too happy too soon ;)

    I can now call TouchEventForTCO() and also read the arguments, copy them ... whatever. However, as soon as I call another function from inside TouchEventForTCO() I get back to the same problem and I get:
    @..\src\qtgui_mainwindow.cpp: In static member function 'static void QTGUI_MainWindow::TouchEventForTCO(int, int, int)':
    ..\src\qtgui_mainwindow.cpp:1851: error: cannot call member function 'void QTGUI_MainWindow::PrepareForTCO(QByteArray*)' without object
    I somehow cant believe that I have to make every function public that need to be called down the line.
    How do I solve this the correct way in C++?

  • You are really not having Qt-Trouble, bu C++ trouble...

    Check C++ - tutorials, what static means pls :-/ In short: Static-Functions are called without an object. So you cant call non-static-functions from within a static function, because non-static function need an object.

    "static functions" in object oriented world is comparable to "global functions" in functional world, but static functions belong to a classes namespace.

    Best Regards.

  • You're right. It's a C++ issue, not Qt.
    So, making TouchEventForTCO() static and public is not the correct way, I assume. If not, how do I call QTGUI_MainWindow::TouchEventForTCO from the MyApplication::notify in the other class then (see code snippets above)?

  • I cant really tell you with that little code-examples... regarding your very first post it seems that you have not implemented your constructor. Another C++-thing...

Log in to reply

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