gpt4 book ai didi

c++ - 信号上的 QT 未定义引用编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:15 25 4
gpt4 key购买 nike

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

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

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

主窗口.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

主窗口.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 上工作。

最佳答案

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

主窗口.h:

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

...

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

我还得到了一本关于信号/插槽常见问题的好书,值得一读:My signal / slot connection does not work

关于c++ - 信号上的 QT 未定义引用编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com