Overriding thePressEvent Key in a QTextEdit Subclass

I have the following example in Qt in which I am trying to redefine the keyPressEvent subclass of QTextEdit, but gives me a "plural definition of txt :: keyPressEvent (QKeyEvent *)", and I cannot understand why:

//txt.h #ifndef TXT_H #define TXT_H #include <QTextEdit> class txt : public QTextEdit { Q_OBJECT public: txt(QWidget *parent = 0); signals: void keyPressEvent(QKeyEvent *e); public slots: }; #endif // TXT_H //txt.cpp #include "txt.h" txt::txt(QWidget *parent) : QTextEdit(parent) { } void txt::keyPressEvent(QKeyEvent *e){ //do stuff with the key event } //main.cpp #include <QtGui/QApplication> #include "txt.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); txt w; w.show(); return a.exec(); } 

I tried scrolling the QTextEdit declaration in txt.h and #, including it only in cpp, but this gives me some other errors (maybe because I need a class in the header, so how do I subclass it?)

Anyway, what am I doing wrong?

EDIT: I also tried it with another class (QLabel) and now I get the same error ...

+4
source share
1 answer

The problem is that you declared it as a signal. This should find a job:

 class txt : public QTextEdit { Q_OBJECT public: txt(QWidget *parent = 0); protected: virtual void keyPressEvent(QKeyEvent *e); public slots: }; 
+6
source

Source: https://habr.com/ru/post/1402768/


All Articles