Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-129168

Object signals not registed with QML if after nested struct with private stmt

    XMLWordPrintable

Details

    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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            fabiankosmale Fabian Kosmale
            haiku Ollie Dawes
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes