涉及清理项目未修复的 moc 文件的 Qt 错误

Qt error involving moc files not fixed by cleaning project

代码是什么

我正在尝试通过学习 Qt 5 这本书来学习 Qt c++。这是后续代码。给出错误的特定部分用于编写预设的欢迎消息。 Qml代码使用头文件中的Q_PROPERTY宏来访问这个函数。 这是实际头文件和宏中的函数定义,以防有用:

Q_PROPERTY( QString ui_welcomeMessage MEMBER welcomeMessage CONSTANT )

const QString& welcomeMessage() const;

错误非静态成员的使用无效

错误分为两部分,但它们似乎相互关联。第一个似乎表明我没有正确使用 const QString& 作为我的成员函数的输出:

/home/sina/Documents/code/Qt/Learn Qt 5/client_management/client_management-lib/build/linux
/gcc/x64/debug/.moc/moc_master-controller.cpp:83: 

error: invalid use of non-static member function ‘const QString& 
cm::controllers::MasterController::welcomeMessage() const’

../../client_management/client_management-lib/build/linux/gcc/x64/debug/.moc/moc_master-
controller.cpp:83:56: error: invalid use of non-static member function ‘const QString& 
cm::controllers::MasterController::welcomeMessage() const’

   83 |         case 0: *reinterpret_cast< QString*>(_v) = _t->welcomeMessage; break;
      |                                                        ^~~~~~~~~~~~~~

对于 moc 文件中的错误,大多数建议只清理并删除开头带有 moc 的所有文件。在那之后不起作用,我尝试删除自动生成的所有内容。

错误 #moc 文件中的错误

错误的第二部分指的是同一个文件。这是添加到 moc 文件顶部的几行:

#include <memory>
#include "../../../../../../controllers/master-controller.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'master-controller.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.15.1. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

正如我们在 https://doc.qt.io/qt-5/properties.html 中看到的 Q_PROPERTY必须有

(READ getFunction [WRITE setFunction] or MEMBER memberName [(READ getFunction | WRITE setFunction)])

您正在尝试使用此代码的两个部分。还有:

A MEMBER variable association is required if no READ accessor function is specified. This makes the given member variable readable and writable without the need of creating READ and WRITE accessor functions. It's still possible to use READ or WRITE accessor functions in addition to MEMBER variable association (but not both), if you need to control the variable access.

所以不光是读书。另请阅读文档。在您的示例中,只需修复它:

Q_PROPERTY( QString ui_welcomeMessage READ welcomeMessage CONSTANT )

const QString& welcomeMessage() const;