fix: crash when close program

playlist model no longer take the ownership of the playlist
This commit is contained in:
Gary Wang 2020-04-07 20:38:08 +08:00
parent 86994811fe
commit 9f940e5046
3 changed files with 15 additions and 18 deletions

View File

@ -1,4 +1,4 @@
_**This is a not ready for use, toy project**_ _**This is a not ready to use, toy project**_
## Note ## Note

View File

@ -48,7 +48,6 @@
** **
****************************************************************************/ ****************************************************************************/
#include "playlistmodel.h" #include "playlistmodel.h"
#include <QFileInfo> #include <QFileInfo>
@ -110,28 +109,28 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const
QMediaPlaylist *PlaylistModel::playlist() const QMediaPlaylist *PlaylistModel::playlist() const
{ {
return m_playlist.data(); return m_playlist;
} }
void PlaylistModel::setPlaylist(QMediaPlaylist *playlist) void PlaylistModel::setPlaylist(QMediaPlaylist *playlist)
{ {
if (m_playlist) { if (m_playlist) {
disconnect(m_playlist.data(), &QMediaPlaylist::mediaAboutToBeInserted, this, &PlaylistModel::beginInsertItems); disconnect(m_playlist, &QMediaPlaylist::mediaAboutToBeInserted, this, &PlaylistModel::beginInsertItems);
disconnect(m_playlist.data(), &QMediaPlaylist::mediaInserted, this, &PlaylistModel::endInsertItems); disconnect(m_playlist, &QMediaPlaylist::mediaInserted, this, &PlaylistModel::endInsertItems);
disconnect(m_playlist.data(), &QMediaPlaylist::mediaAboutToBeRemoved, this, &PlaylistModel::beginRemoveItems); disconnect(m_playlist, &QMediaPlaylist::mediaAboutToBeRemoved, this, &PlaylistModel::beginRemoveItems);
disconnect(m_playlist.data(), &QMediaPlaylist::mediaRemoved, this, &PlaylistModel::endRemoveItems); disconnect(m_playlist, &QMediaPlaylist::mediaRemoved, this, &PlaylistModel::endRemoveItems);
disconnect(m_playlist.data(), &QMediaPlaylist::mediaChanged, this, &PlaylistModel::changeItems); disconnect(m_playlist, &QMediaPlaylist::mediaChanged, this, &PlaylistModel::changeItems);
} }
beginResetModel(); beginResetModel();
m_playlist.reset(playlist); m_playlist = playlist;
if (m_playlist) { if (m_playlist) {
connect(m_playlist.data(), &QMediaPlaylist::mediaAboutToBeInserted, this, &PlaylistModel::beginInsertItems); connect(m_playlist, &QMediaPlaylist::mediaAboutToBeInserted, this, &PlaylistModel::beginInsertItems);
connect(m_playlist.data(), &QMediaPlaylist::mediaInserted, this, &PlaylistModel::endInsertItems); connect(m_playlist, &QMediaPlaylist::mediaInserted, this, &PlaylistModel::endInsertItems);
connect(m_playlist.data(), &QMediaPlaylist::mediaAboutToBeRemoved, this, &PlaylistModel::beginRemoveItems); connect(m_playlist, &QMediaPlaylist::mediaAboutToBeRemoved, this, &PlaylistModel::beginRemoveItems);
connect(m_playlist.data(), &QMediaPlaylist::mediaRemoved, this, &PlaylistModel::endRemoveItems); connect(m_playlist, &QMediaPlaylist::mediaRemoved, this, &PlaylistModel::endRemoveItems);
connect(m_playlist.data(), &QMediaPlaylist::mediaChanged, this, &PlaylistModel::changeItems); connect(m_playlist, &QMediaPlaylist::mediaChanged, this, &PlaylistModel::changeItems);
} }
endResetModel(); endResetModel();

View File

@ -48,12 +48,10 @@
** **
****************************************************************************/ ****************************************************************************/
#ifndef PLAYLISTMODEL_H #ifndef PLAYLISTMODEL_H
#define PLAYLISTMODEL_H #define PLAYLISTMODEL_H
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QScopedPointer>
class QMediaPlaylist; class QMediaPlaylist;
@ -69,7 +67,7 @@ public:
}; };
explicit PlaylistModel(QObject *parent = nullptr); explicit PlaylistModel(QObject *parent = nullptr);
~PlaylistModel(); ~PlaylistModel() override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
@ -92,7 +90,7 @@ private slots:
void changeItems(int start, int end); void changeItems(int start, int end);
private: private:
QScopedPointer<QMediaPlaylist> m_playlist; QMediaPlaylist * m_playlist = nullptr;
QMap<QModelIndex, QVariant> m_data; QMap<QModelIndex, QVariant> m_data;
}; };