2015-02-07 79 views
1

我想将属性标题添加到我的应用程序的主窗口中。但是,当我尝试编译,编译器给了我这个错误:QT未定义的参考信号编译错误

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&) 
{ 
} 

我试图清理项目的bu ild文件夹,它没有帮助:(。我正在使用QT 5.4并在QT Creator上工作。

+3

'QOBJECT_H'?改为尝试'Q_OBJECT'。 – Mat 2015-02-07 18:58:57

+1

之后不要忘记运行'qmake'。 (QtCreator应该自动执行此操作,但以防万一...) – Zeta 2015-02-07 19:00:08

+0

QMainWindow没有任何titleChanged方法。 – 2015-02-07 19:01:32

回答

0

此问题已在其他人的评论中回答。我只想强调答案。在我的情况下,错误是在头文件

mainwindow.h:

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

... 

我迷糊中QObject.h头文件作为与Q_OBJECT宏的包括警卫通过Qt的MOC工具中使用的QOBJECT_H宏。由于intellisense会为您提供两种选择,因此很容易混淆。

我还新指出,有关的常见问题与信号/插槽值得阅读一个良好的阅读:My signal/slot connection does not work