当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - SubClassing QObject and creating QList

问题描述:

Here is my code: http://pastebin.com/57hXKCYm

Here is my error: main.cpp:32: error: undefined reference to `vtable for SongObject'

When I remove the Q_OBJECT from the SongObject class, the error goes away, and the app launches, but when useing the songList in QML, it says name is undefined, and color is undefined. Here is my QML:

ListView {

width: 100; height: 100

model: allSongObjects

delegate: Rectangle {

height: 25

width: 100

color: {

console.log(model.modelData.name)

return color

}

Text { text: name }

}

}

Here is how I'm passing the songList to QML:

 engine.rootContext()->setContextProperty("allSongObjects", QVariant::fromValue(getAllSongs(db)));

I am a novice to C++, and so I'm sure the issue is going to be very obvious to someone here. I appreciate any help I can get. I just want to know WHY I'm getting hte issues, and how my code SHOULD be, and I'll probably understand why it should be that way when I see the corrected code.

Thanks for any help.

网友答案:

The header file was written incorrectly. The corrected (and working) header is

#ifndef SONG_OBJECT_MAIN
#define SONG_OBJECT_MAIN

#include <QObject>

class SongObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString path READ path CONSTANT)
    Q_PROPERTY(QString title READ title CONSTANT)
    Q_PROPERTY(QString album READ album CONSTANT)
    Q_PROPERTY(QString artist READ artist CONSTANT)
    Q_PROPERTY(QString art READ art CONSTANT)
    QString m_path;
    QString m_title;
    QString m_album;
    QString m_artist;
    QString m_art;


    QString path() { return m_path;}
    QString title() { return m_title;}
    QString album() { return m_album;}
    QString artist() {return m_artist;}
    QString art() {return m_art;}

public:
    explicit SongObject(const QString &path, const QString &title, const QString &album, const QString &artist, const QString &art) :
        QObject(),
        m_path(path),
        m_title(title),
        m_album(album),
        m_artist(artist),
        m_art(art)
    {}

};

#endif // SONG_OBJECT_MAIN
分享给朋友:
您可能感兴趣的文章:
随机阅读: