PDA

View Full Version : Undefined reference to QWidget::event(QKeyEvent *) in custom classes



Toniy
9th July 2015, 14:26
Greetings, everyone.

Here is my problem: I use Qt-4.8.5 (I need that version for work, another versions aren't compatible with my goals). In process of learning QEvents i added some code (functions that override virtual event handling functions and includings of event headers) into some of my classes inherited from QWidget, but deleted that added code later. Since that time when i trying to make new class that inherits QWidget (even in absolutely new projects and in classes that already exist in old projects), compiler says something like that:


(.rodata._ZTV10MainWindow[vtable for MainWindow]+0xc8):-1: error: undefined reference to `QWidget::event(QKeyEvent*)'
:-1: error: collect2: ld returned 1 exit status

even if there is no usage of any kind of events in project (I experimented with QKeyEvents too).

Here is an example of simpliest project that has been created during attempts to find out true source of problem (if additional information is necessary then I ready to provide it, but at the moment I can't even imagine what kind information is necessary in addition to that one):

PRO-file (generated by Qt Creator):


QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled1
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

#include <QHBoxLayout>
#include <QLabel>

class MainWindow : public QWidget
{
Q_OBJECT
//protected:
// virtual void event(QKeyEvent *)
// {
// }
public:
explicit MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H

By the way, if function


virtual void event(QKeyEvent *)

in mainwindow.h is uncommented then everithyng compiles normally and works fine.

mainwindow.cpp:


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
}


main.cpp:


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Cleaning projects and rebuilding them haven't helped to solve this problem so as manual removing of folder with builded project (I use Qt-creator, but with only qmake and make commands problem still appears)
But standard Qt classes, inherited from QWidget (QDialog, QMainWindow etc) and QWidget itself work normally (I mean, if project has not custom classes inherited from QWidget then everything works fine). For finding true source of problem I tried to compile it with Qt-4.8.6, and this mistake did not appear (but I can't use 4.8.6 permanently)

It is curious, but there were no compile errors for another QWidget::event() functions and any functions of QWidget or other class.

Additional information:

Platform: Linux Red Hat Enterprise Edition 64-bit
Compiler version: gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) 64-bit
Qt and QMake version: Qt-4.8.5
Qt Creator version: 2.7.2

The question is: what can cause this problem ( and what could I break, because everithyng used to work fine before:) ) and what should I try to get rid of it assuming that I have no ability to reinstall Qt and Qt-creator (it is not my own computer)?

Thanks in advance for any help.

anda_skoa
9th July 2015, 14:59
There is no


event(QKeyEvent*)

method.
There is


event(QEvent*)

though.

Cheers,
_

Toniy
9th July 2015, 15:09
There is no


event(QKeyEvent*)

method.
There is


event(QEvent*)

though.

Cheers,
_

I checked that in Qt includes. Strange, but I'm watching on it:

File qwidget.h:


#ifndef QWIDGET_H
#define QWIDGET_H
...
class Q_GUI_EXPORT QWidget : public QObject, public QPaintDevice
{
...

protected:
// Event handlers
...
virtual void event(QKeyEvent *);
...


What is it then? I mean, it used to be found by compiler before. No changes in Qt original files were made (at least not planned and not manually).
In any case, why does it try to find it if that stuff is not there or why can't it find it from custom class?

Cheers
--

anda_skoa
9th July 2015, 16:47
Nope, no such method:



#> grep QKeyEvent /usr/include/x86_64-linux-gnu/qt5/QtWidgets/qwidget.h

class QKeyEvent;
virtual void keyPressEvent(QKeyEvent *);
virtual void keyReleaseEvent(QKeyEvent *);



#> grep QKeyEvent /usr/include/qt4/QtGui/qwidget.h

class QKeyEvent;
virtual void keyPressEvent(QKeyEvent *);
virtual void keyReleaseEvent(QKeyEvent *);


Cheers,
_

Toniy
9th July 2015, 17:20
Strange... But how are these differences between yours and mine possible? :)
Anyway, thanks: situation is more clear now...

And how should I act in this situation? Would it be wise to comment these lines in my header or patching third parties' files is bad practicing? If that is so, what can be done?
However, why does compiler not cry about it for native classes like QWidget? And how could these functions appear into file?

jefftee
9th July 2015, 20:20
No virtual void event(QKeyEvent *) either in my mac Qt 5.5.0 installation either:



imac01:~/Qt/5.5.0$ grep QKeyEvent /Users/jefft/Qt/5.5.0/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qwidget.h
class QKeyEvent;
virtual void keyPressEvent(QKeyEvent *);
virtual void keyReleaseEvent(QKeyEvent *);

Seems something is wonky with your Qt installation. If you can't remove/install the Qt packages that installed into /usr, can you perhaps install 4.8.5 into your home directory and see if it matches what you have installed into /usr?

Toniy
13th July 2015, 12:34
All right. I have collected some statistics on different Qt versions and there is no any of these functions. How they appeared and more important how they used to work before -- here is the question.
I'll reply here when I find source sample that was used for Qt installation on my computer and check it.

Thanks for the quickest responses.

Toniy
14th July 2015, 12:17
I didn't find sources but deleted that function and replaced it with ones recommended in that thread (in file "qwidget.h") and problem has been solved.
Seems like I accidentally corrupted that file myself.

So, maybe that thread will be useful for someone and he/she will solve similar problem quicker :)

d_stranz
14th July 2015, 15:56
Seems like I accidentally corrupted that file myself.


If I were you, I would re-install Qt instead of relying on your memory and hand-editing a really core Qt header file. Who knows what else you've "accidentally corrupted" that might come back to bite you later?

You are fortunate that you seem to be using a binary distribution of Qt, and the linker caught your error.