I am trying to learn Qt c++ with the book learn Qt 5. This is the follow along code. The particular section that is giving the error is for writing a preset welcome message. The Qml code uses the Q_PROPERTY macro in the header file to access this function. This is the function definition in the actual header and the macro in case it is useful:
Q_PROPERTY( QString ui_welcomeMessage MEMBER welcomeMessage CONSTANT )
const QString& welcomeMessage() const;
The error is in two parts but they seem to be related to one another. The first seems to suggest I am not using const QString& properly as the output of my member function:
/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;
| ^~~~~~~~~~~~~~
for errors in moc files most recommend just cleaning and deleting all files with moc at the start. After that did not work I tried deleting everything that is auto generated.
The second part of the error is referring to the same file. This though is a couple lines that were added to the top of the moc file:
#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
As we see in https://doc.qt.io/qt-5/properties.html Q_PROPERTY have to have
(READ getFunction [WRITE setFunction] or MEMBER memberName [(READ getFunction | WRITE setFunction)])
You are trying to use both parts of this code. Also :
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.
So read not only books. Read also documentation. In your example just fix it:
Q_PROPERTY( QString ui_welcomeMessage READ welcomeMessage CONSTANT )
const QString& welcomeMessage() const;