Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
6.7.2, 6.9
-
None
Description
If you add a nested struct in a class that you expose to QML and add a private: section to the struct then Qt wont expose any of the below signals to QML.
Take this simple class:
#pragma once #include <QtCore/QObject> #include <QtQml/QQmlEngine> class TestObject : public QObject { Q_OBJECT QML_ELEMENT Q_PROPERTY(QString testString READ testString WRITE setTestString NOTIFY testStringChanged) public: struct StructThatBreaksEverything { private: // <--- This line breaks QML signal registration }; TestObject(QObject *parent = nullptr); QString testString() const; void setTestString(QString testString); Q_SIGNAL void testStringChanged(); private: QString m_testString{u"hello world"_qs}; };
That above class fails to expose the testStringChanged signal to QML because it includes a nested struct with a private: statement.
If you try to attach to the above testStringChanged signal from QML then then the application will crash if you use a onTestStringChanged handler. Connecting to the signal works fine from c++ which makes me think this is more of a QML issue rather than a straight up moc issue but I may be wrong.
It makes no difference if the struct contains actual public or private members. I ran into this issue with a more much more substantial class that happened to contain a struct with private members and it took longer than I would have liked to work out the issue.
As an extra note, if the struct is replaced with a class the above appears to work.
See the attached project for a basic qt QML project that demonstrates the above issue with some extra comments for clarity.