我有这个模型,其中包含歌曲的 URL
#ifndef PLAYLISTITEM_H
#define PLAYLISTITEM_H
#include<QUrl>
#include <QObject>
class PlaylistItem : public QObject
{
Q_OBJECT
Q_PROPERTY(QUrl source READ source CONSTANT)
public:
explicit PlaylistItem(QUrl& source, QObject *parent = nullptr);
QUrl source() const;
signals:
public slots:
private:
QUrl m_source;
};
#endif // PLAYLISTITEM_H
我有一个 SongManager 类,它将数据设置为此模型,并且我已在 main.cpp 中设置属性,如下所示
SongManager manager;
ctxt->setContextProperty("manager", &manager);
当我像这样访问 QML 中的模型时
Audio {
id: music_player;
autoPlay: false;
playlist: manager.getSongListByAlbumName("someAlbumName", true)
}
我收到此错误,
Unable to assign QJSValue to ::QDeclarativePlaylist*
为什么,我收到此错误,如果我想用自己的模型覆盖 PlayListItem 等 QML 元素,我该怎么办?
最佳答案
I have this model which holds the URL of the songs
下面的代码不是该术语的任何 QML 定义的模型。
Audio
预计 Playlist
元素,它并没有提及任何模型,即使您有一个模型。只因为一个Playlist
可以用作模型并不一定意味着任何模型都可以用作播放列表。但即使这是可能的,肯定也需要一个带有 source
的模型。角色,而不是具有 source
的对象属性。
什么manager.getSongListByAlbumName()
返回在这一点上还是一个谜,所以只有通灵者才能回答这个问题。
假设manager.getSongListByAlbumName()
返回某种可以从 QML 迭代的列表,您可以使用它来填充 Playlist
通过使用相应的方法。我认为可以直接传递 QList<QUrl>
至Playlist.addItems(sources)
并且让它工作,因为这个特定的容器类型会自动转换为 QML。
关于c++ - 如何将模型传递到 qml 中的播放列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611292/