Qt / C ++问题与继承和未声明的信号

时间:2019-05-01 14:36:03

标签: c++ qt

我在弄弄qt,并创建了一个自定义标签对象,该对象将继承到另一个类。我在发出未声明的信号时遇到了这个问题,但是我不确定为什么,因为我在头文件中声明了该信号。问题出在子类cpp文件中。

error: undeclared identifier 'Tile_Mouse_Action'
    emit Tile_Mouse_Action(QString("Clicked"), "test")
         ^
    dsf

compile output: ../Signals/tilewidget.cpp:13:10: error: use of undeclared identifier 'Tile_Mouse_Action'
    emit Tile_Mouse_Action(QString("Clicked"), "test")
         ^
../Signals/tilewidget.cpp:12:35: warning: unused parameter 'ev' [-Wunused-parameter]
void mousePressEvent(QMouseEvent *ev) {
                                  ^
1 warning and 1 error generated.
make: *** [tilewidget.o] Error 1
10:16:32: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Signals (kit: Desktop Qt 5.12.3 clang 64bit)
When executing step "Make"

即使我尝试注释掉发射行,也会遇到其他问题,这使我相信我的代码还有其他问题。感谢您的任何帮助,谢谢。

error: symbol(s) not found for architecture x86_64
error: linker command failed with exit code 1 (use -v to see invocation) 

Undefined symbols for architecture x86_64:
  "vtable for TileWidget", referenced from:
      TileWidget::TileWidget(QWidget*) in tilewidget.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Signals.app/Contents/MacOS/Signals] Error 1
10:26:28: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Signals (kit: Desktop Qt 5.12.3 clang 64bit)
When executing step "Make"

基类头文件

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = nullptr);
    void mouseMoveEvent(QMouseEvent *ev);
    void mousePressEvent(QMouseEvent *ev);
    void mouseDoubleClickEvent(QMouseEvent *ev);

    int x, y;
    char action[20];
    QString mouseAction;

signals:
    void Mouse_Pos();
    void Mouse_Press();
    void Mouse_DoubleClick();
    void Mouse_Action(QString, std::string);

public slots:
};

基类cpp文件

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{

}

void MyLabel::mouseMoveEvent(QMouseEvent *ev) {...

void MyLabel::mousePressEvent(QMouseEvent *ev) {...

void MyLabel::mouseDoubleClickEvent(QMouseEvent *ev) {...

子类头文件

class TileWidget : public MyLabel
{
    Q_OBJECT

public:
    TileWidget(QWidget *parent = nullptr);
    void mousePressEvent(QMouseEvent *ev);


signals:
    void Tile_Mouse_Action(QString, std::string); 

private slots:

};

子类cpp文件

TileWidget::TileWidget(QWidget *parent) :
    MyLabel(parent)
{

}

void mousePressEvent(QMouseEvent *ev) {
    emit Tile_Mouse_Action(QString("Clicked"), "test"); // issue here
};

1 个答案:

答案 0 :(得分:0)

我将在不给出答案的情况下尝试提供指导。

请尝试确保您的函数位于同一名称空间中。查看子类中的构造函数和mousePressEvent()函数。您需要告诉编译器它们是同一类的一部分。

在Qt中编写自己的类时,我经常遇到这个问题。