Qt错误涉及清洁项目未修复的MOC文件

问题描述

什么是代码

我正在尝试使用学习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]或MEMBER memberName [(READ getFunction | WRITE setFunction)])

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

如果未指定READ访问器功能,则需要MEMBER变量关联。这使得给定的成员变量可读写,而无需创建READ和WRITE访问器函数。如果需要控制变量访问,除了MEMBER变量关联(但不能同时使用)之外,仍然可以使用READ或WRITE访问器函数。

因此,不仅要阅读书籍。另请参阅文档。在您的示例中,对其进行修复:

Q_PROPERTY( QString ui_welcomeMessage READ welcomeMessage CONSTANT )

const QString& welcomeMessage() const;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...