C++ 信号上的QT未定义引用编译错误
我正在尝试将属性标题添加到应用程序的主窗口中。但当我试图编译它时,编译器会给我以下错误:C++ 信号上的QT未定义引用编译错误,c++,qt,signals,C++,Qt,Signals,我正在尝试将属性标题添加到应用程序的主窗口中。但当我试图编译它时,编译器会给我以下错误: mainwindow.cpp:19: undefined reference to `MainWindow::titleChanged(QString const&)' 我在mingw和msvc2013上试过,但都在同一行失败,出现了这个错误。头文件/源文件: mainwindow.h: #ifndef MAINWINDOW #define MAINWINDOW #include <QOb
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
class MainWindow : public QObject {
QOBJECT_H // This should be Q_OBJECT
...
我试图清理项目的构建文件夹,但没有帮助:(。我正在使用QT 5.4并使用QT Creator。其他人已经在评论中回答了这个问题。我只想突出显示答案。我的错误在头文件中 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
class MainWindow : public QObject {
QOBJECT_H // This should be Q_OBJECT
...
我把QOBJECT.H头文件中用作包含保护的QOBJECT_H宏与QT的moc工具使用的Q_对象宏混淆了。由于intellisense将为您提供这两个选项,因此它们很容易混淆
我还看到了一篇关于值得阅读的信号/插槽常见问题的好文章:
QOBJECT\u H
?尝试Q\u OBJECT
。不要忘记在之后运行qmake
。(QtCreator应该自动执行此操作,但以防万一…)QMainWindow没有任何标题更改的方法。是的,将其从QOBJECT\u H更改为Q\u OBJECT helped:))。非常感谢,我不知道我以前怎么没有注意到。这可能会让你感兴趣: