Qt - 如何从派生 class 发出信号?

Qt - how to emit a signal from derived class?

在我的自定义派生中 class 我正在尝试添加并发出信号 gotChange(QString change):

#ifndef NETTEXTEDITOR_H
#define NETTEXTEDITOR_H

#include <QTextEdit>

class NetTextEditor : public QTextEdit {
    using QTextEdit::QTextEdit;
public:
    void keyPressEvent(QKeyEvent *e) override;

signals:
    void gotChange(QString change);
};

#endif // NETTEXTEDITOR_H

声明没问题,但是当我尝试发出它时,出现下一个错误:

/home/mrsydar/Projects/Qt/DOCSHARE/nettexteditor.cpp:5: error:
‘gotChange’ was not declared in this scope
../DOCSHARE/nettexteditor.cpp: In function ‘void doEmit(QString)’:
../DOCSHARE/nettexteditor.cpp:5:10: error: ‘gotChange’ was not
declared in this scope
5 | emit gotChange(em);
| ^~~~~~~~~

我认为这是因为推导,但我是 Qt 的新手,不知道任何替代方案或如何解决这个问题。

#include "nettexteditor.h"
#include <QKeyEvent>

void NetTextEditor::keyPressEvent(QKeyEvent *e) {
    int key = e->key();
     if((key >= 65 && key <= 90) || (key >= 48 && key <= 57)){
         QString delta;
         int position = this->textCursor().position();

         delta = "c " + QString::number(position) + " " + (char) key;

         emit gotChange(delta);   //if i comment this emit out, everything compiles without errors
     }
}

我做错了什么?

如果要创建信号或槽,则必须在专用部分中使用宏 Q_OBJECT。然后你可以 运行 "运行 qmake" (或者简单地删除构建文件夹)并编译应用程序