Существует несколько решений в зависимости от того, что вы хотите реализовать:
- Если вы собираетесь загружать список только в начале, вам следует использовать только
QList <QUrl>
:
// ...
QList<QUrl> sources;
QString folder = "E:/"
QDirIterator it(folder, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()){
sources << QUrl::fromLocalFile(it.next());
}
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("sources", QVariant::fromValue(sources));
// ...
// ...
Audio {
id: player;
playlist: Playlist {
id: playlist
Component.onCompleted: playlist.addItems(sources)
}
}
// ...
- Если вы хотите добавлять и удалять элементы во время выполнения программы, вы можете использовать модель:
class SourceModel: public QStandardItemModel{
Q_OBJECT
public:
enum SourceRoles{
SourceRole = Qt::UserRole + 1000
};
SourceModel(QObject *parent=nullptr):
QStandardItemModel(parent)
{
QHash<int, QByteArray> roles;
roles[SourceRole] = "source";
setItemRoleNames(roles);
connect(this, &QAbstractItemModel::rowsInserted, this, &SourceModel::onRowsInserted);
connect(this, &QAbstractItemModel::rowsRemoved, this, &SourceModel::onRowsRemoved);
connect(this, &QAbstractItemModel::modelReset, this, &SourceModel::reset);
}
void addSource(const QUrl & url){
QStandardItem *item = new QStandardItem();
item->setData(url, SourceRole);
appendRow(item);
}
Q_SIGNALS:
void sourcesInserted(int first, QList<QUrl> sources);
void sourcesRemoved(int first, int last);
void reset();
private:
void onRowsInserted(const QModelIndex &parent, int first, int last){
QList<QUrl> sources;
for (int i=first; i <= last; ++i) {
QModelIndex ix = this->index(i, 0, parent);
QVariant v = data(ix, SourceRole);
sources << v.toUrl();
}
Q_EMIT sourcesInserted(first, sources);
}
void onRowsRemoved(const QModelIndex & /*parent*/, int first, int last){
Q_EMIT sourcesRemoved(first, last);
}
};
// ...
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("source_model", &source_model);
// ...
QString folder = "E:/"
QDirIterator it(folder, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()){
source_model.addSource(QUrl::fromLocalFile(it.next()));
}
// ...
// ...
Connections{
target: source_model
onSourcesInserted: playlist.insertItems(first, sources)
onSourcesRemoved: playlist.removeItems(first, last);
onReset: playlist.clear()
}
Audio {
id: player;
playlist: Playlist {
id: playlist
}
}
// ...
- Другой вариант - использовать Q_PROPERTY:
class SourceManager: public QObject{
Q_OBJECT
Q_PROPERTY(QList<QUrl> sources READ sources WRITE setSources NOTIFY sourcesChanged)
public:
using QObject::QObject;
QList<QUrl> sources() const{
return m_sources;
}
void setSources(QList<QUrl> sources){
if (m_sources == sources)
return;
m_sources = sources;
emit sourcesChanged();
}
Q_SIGNALS:
void sourcesChanged();
private:
QList<QUrl> m_sources;
};
// ...
SourceManager source_manager;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("source_manager", &source_manager);
// ...
QList<QUrl> sources;
QString folder = "E:/"
QDirIterator it(folder, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()){
sources << QUrl::fromLocalFile(it.next());
}
source_manager.setSources(sources);
// ...
// ...
Connections{
target: source_manager
onSourcesChanged: {
playlist.clear();
playlist.addItems(source_manager.sources)
}
}
Audio {
id: player;
playlist: Playlist {
id: playlist
}
}
// ...