Q_PROPERTY in a class failed when moc is running.
-
Hello All,
OS: Window 10
QT: 6.9.1
MSVC 2022
Build tool: qmake==== Class ==
#include <QApplication>
#include <QObject>
#include <QString>class MyClass : public QObject {
Q_OBJECT Q_PROPERTY(QString myText READ getMyText WRITE setMyText NOTIFY myTextChanged)
public:
MyClass(QObject *parent = nullptr);QString getMyText() const; void setMyText(const QString &text);
signals:
void myTextChanged(const QString &newText);private:
QString m_myText;
};======== Implementation ==
#include <QApplication>
#include "myclass.h"MyClass::MyClass(QObject *parent ) : QObject(parent)
{}
QString MyClass::getMyText() const
{
return m_myText;
}void MyClass::setMyText(const QString &text)
{
if (m_myText != text) {
m_myText = text;
emit myTextChanged(m_myText);
}
}===== Main ===
#include "myclass.h"
int main() {
MyClass obj;
obj.setMyText("Hello");
qDebug() << obj.getMyText(); // Output: "Hello"QObject::connect(&obj, &MyClass::myTextChanged, [&](const QString &newText) { qDebug() << "Text changed to:" << newText; }); obj.setMyText("World"); // Output: "Text changed to: World" return 0;
}
================= Moc error =======
cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -permissive- -Zc:_cplusplus -Zc:externConstexpr -std:c++17 -O2 -MD -std:c++17 -utf-8 -W3 -w34100 -w34189 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DC_PLUS_PLUS -DWin32 -DWIN32 -D_MYWINDOW -D_CRT_SECURE_NO_WARNINGS -DCPP -DQTV5 -DQTV5 -DQT5MACRO -DQt_5x -DQt_5X -DQT_5X -DQT_5x -DC_PLUS_PLUS -DCPLUSPLUS -D_CRT_SECURE_NO_WARNINGS -DSTATIC_LIB -DSTATIC_QT -DNDEBUG -DQT_NO_DEBUG -DQT_PRINTSUPPORT_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_CORE_LIB -I. -I. -IE:\DEV\msqlib\v6-22/bTableWidget -IE:\DEV\msqlib\v6-22/qlib -IE:\DEV\msqlib\v6-22/include -IE:\DEV\msqlib\v6-22 -I....\Qt\v6\6.9.1\msvc2022_64\include -I....\Qt\v6\6.9.1\msvc2022_64\include\QtPrintSupport -I....\Qt\v6\6.9.1\msvc2022_64\include\QtWidgets -I....\Qt\v6\6.9.1\msvc2022_64\include\QtGui -I....\Qt\v6\6.9.1\msvc2022_64\include\QtNetwork -I....\Qt\v6\6.9.1\msvc2022_64\include\QtSql -I....\Qt\v6\6.9.1\msvc2022_64\include\QtCore -IMOC -I/include -I....\Qt\v6\6.9.1\msvc2022_64\mkspecs\win32-msvc -FoOBJ\moc_myclass.obj MOC\moc_myclass.cpp
moc_myclass.cpp
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(262): error C2065: 'result': undeclared identifier
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(200): note: see reference to function template instantiation 'auto QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo::<lambda_930f3e387543f60f462d274f690f2e0a>::operator ()<T>(const T &) const' being compiled
with
[
T=QtMocHelpers::PropertyData<QString>
]
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(200): note: see reference to function template instantiation 'auto QtMocHelpers::detail::UintDataStorage<std::integer_sequence<int,0>,QtMocHelpers::PropertyData<QString>>::forEach::<lambda_f952f8e5d1a1fa6d836f8a8f79762b0c>::operator ()<QtMocHelpers::detail::UintDataEntry<0,QtMocHelpers::PropertyData<QString>>>(const QtMocHelpers::detail::UintDataEntry<0,QtMocHelpers::PropertyData<QString>> &) const' being compiled
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(252): note: see reference to function template instantiation 'void QtMocHelpers::detail::UintDataStorage<std::integer_sequence<int,0>,QtMocHelpers::PropertyData<QString>>::forEach<QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo::<lambda_930f3e387543f60f462d274f690f2e0a>>(F &&) const' being compiled
with
[
F=QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo::<lambda_930f3e387543f60f462d274f690f2e0a>
]
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(255): note: see reference to function template instantiation 'void QtMocHelpers::detail::UintDataStorage<std::integer_sequence<int,0>,QtMocHelpers::PropertyData<QString>>::forEach<QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo::<lambda_930f3e387543f60f462d274f690f2e0a>>(F &&) const' being compiled
with
[
F=QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo::<lambda_930f3e387543f60f462d274f690f2e0a>
]
....\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(550): note: see reference to function template instantiation 'void QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo<Unique,QtMocHelpers::MetaObjectContents<29,10,38,4>>(Result &,size_t,uint &) const' being compiled
with
[
Unique=anonymous-namespace'::qt_meta_tag_ZN7MyClassE_t, Result=QtMocHelpers::MetaObjectContents<29,10,38,4> ] ..\..\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(550): note: see reference to function template instantiation 'void QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo<Unique,QtMocHelpers::MetaObjectContents<29,10,38,4>>(Result &,size_t,uint &) const' being compiled with [ Unique=
anonymous-namespace'::qt_meta_tag_ZN7MyClassE_t,
Result=QtMocHelpers::MetaObjectContents<29,10,38,4>
]
MOC\moc_myclass.cpp(61): note: see reference to function template instantiation 'QtMocHelpers::MetaObjectContents<29,10,38,4> QtMocHelpers::metaObjectData<MyClass,anonymous-namespace'::qt_meta_tag_ZN7MyClassE_t,QtMocHelpers::StringRefStorage<char [8],char [14],char [1],char [8],char [7]>,QtMocHelpers::UintData<QtMocHelpers::SignalData<void (const QString &)>>,QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>,QtMocHelpers::UintData<>,QtMocHelpers::UintData<>,QtMocHelpers::detail::UintDataBlock<0,0>>(uint,const Strings &,const Methods &,const Properties &,const Enums &,const Constructors &,const ClassInfo &)' being compiled with [ Strings=QtMocHelpers::StringRefStorage<char [8],char [14],char [1],char [8],char [7]>, Methods=QtMocHelpers::UintData<QtMocHelpers::SignalData<void (const QString &)>>, Properties=QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>, Enums=QtMocHelpers::UintData<>, Constructors=QtMocHelpers::UintData<>, ClassInfo=QtMocHelpers::detail::UintDataBlock<0,0> ] ..\..\Qt\v6\6.9.1\msvc2022_64\include\QtCore/qtmochelpers.h(550): note: while evaluating constexpr function 'QtMocHelpers::UintData<QtMocHelpers::PropertyData<QString>>::copyTo' MOC\moc_myclass.cpp(60): note: while evaluating constexpr function 'QtMocHelpers::metaObjectData' E:\DEV\temp\MOC\../myclass.h(8): note: while evaluating constexpr function 'MyClass::qt_create_metaobjectdata' E:\DEV\temp\MOC\../myclass.h(8): note: see reference to variable template 'const QtMocHelpers::MetaObjectContents<29,10,38,4> MyClass::qt_staticMetaObjectContent<
anonymous namespace'::qt_meta_tag_ZN7MyClassE_t>' being compiled
MOC\moc_myclass.cpp(65): note: see reference to variable template 'const auto MyClass::qt_staticMetaObjectStaticContent<`anonymous namespace'::qt_meta_tag_ZN7MyClassE_t>' being compiled=======
I will appreciate any help. -
Hello and welcome,
Could you please edit your post and format your code using the </> code formatting tags? It’s hard to read otherwise. Please also post the actual error. Possible that I’ve overlooked it, but I couldn’t find it. -
Hi,
One thing I could see is that setMyText is not placed under a slot directive.
-
@SGaist Thank you for your suggestion. I made the changes, and it did not change the behavior of moc.
-
I would test it but since you did not format your code properly I won't fiddle out the mess above by myself...
-
Hello All,
It was my environment which is creating the miss behavior of moc. My building tools were pointing to the wrong version of VSVC (MSVC 2019). I corrected this, and all worked fine.
"My Culpa"
Sincerely.
-