繁体   English   中英

信号上的QT未定义参考编译错误

[英]QT undefined reference compilation error on a signal

我试图将属性标题添加到应用程序的主窗口中。 但是当我尝试编译它时,编译器给我这个错误:

mainwindow.cpp:19: undefined reference to `MainWindow::titleChanged(QString const&)'

我在mingw和msvc2013上都尝试过,但都在同一行失败,并出现此错误。 头文件/源文件:

mainwindow.h:

#ifndef MAINWINDOW
#define MAINWINDOW

#include <QObject>
#include <QString>

class MainWindow : public QObject {
    QOBJECT_H

    Q_PROPERTY(QString title READ getTitle WRITE setTitle NOTIFY titleChanged)

public:
    MainWindow();

    QString getTitle();

public slots:
    void setTitle(const QString& title);

signals:
    void titleChanged(const QString& title);

private:
    QString title_;
};

#endif // MAINWINDOW

mainwindow.cpp:

#include "mainwindow.h"

#include <QString>

MainWindow::MainWindow()
{
}

QString MainWindow::getTitle()
{
    return title_;
}

void MainWindow::setTitle(const QString& title)
{
    if (title_ != title) {
        title_ = title;

        emit titleChanged(title);
    }
}

如果我将以下方法添加到mainwindow.cpp文件的末尾,则该应用程序将编译并运行,但不会发出信号:

void MainWindow::titleChanged(const QString&)
{
}

我试图清理项目的构建文件夹,但无济于事:(。我正在使用QT 5.4并在QT Creator上工作。

有人已经在评论中回答了这个问题。 我只是想强调答案。 我的错误是在头文件中

mainwindow.h:

class MainWindow : public QObject {
   QOBJECT_H  // This should be Q_OBJECT

...

我将QObject.h头文件中用作包含保护的QOBJECT_H宏与QT的moc工具使用的Q_OBJECT宏混淆了。 由于智能感知将为您提供这两种选择,因此很容易混淆它们。

我还指出了有关信号/插槽的常见问题的良好阅读,值得阅读: 我的信号/插槽连接不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM