I'm trying to connect a C++ signal to a QML slot. The exchanged data is a constant reference to an object of my own class. Thereby this class is registered to the Qt meta system, before the QML file is loaded.
In code it looks like this:
The header of the custom meta type:
class QSong : public QObject
{
private:
Q_OBJECT
Q_PROPERTY( QString artist READ artist CONSTANT )
Q_PROPERTY( QString album READ album CONSTANT )
Q_PROPERTY( QString title READ title CONSTANT)
QString m_artist; ///< Data field containing the song's artist
QString m_album; ///< Data field containing the song's album
QString m_title; ///< Data field containing the song's title
public:
/* Constructors, copy assignment operator and getters */
};
Q_DECLARE_METATYPE(QSong)
The meta type registration (Before loading the qml file):
qmlRegisterType<QSong>("webradio.types", 1, 0, "Song");
The C++ signal:
void currentSongChanged( const QSong& song );
The QML slot:
Connections {
target: Global.MPD; // This is a tested to be working singleton
onCurrentSongChanged: {
print(song);
print(song.artist);
}
}
As far as I understood the procedure, this should be sufficient for transferring the data. Still whenever the signal is triggered with a valid QSong object, the output is the following:
qml: QVariant(QSong)
qml: undefined
I know similar questions have been asked before, however the solutions stated there have not worked for me.
For instance if I try assigning the slot parameter to a local property as suggested in this thread, the output states
Cannot assign QSong to QSong*
I'm using Qt 5.7.1 with the GCC at version 6.2.1.