Solved Creating QTableView dynamically / Creating new playlists
-
There's not need to have several QTableView. As was already suggested, you should update the model.
One question that you did not answer: how are you currently handling your playlist ?
You might also be interested in the Qt Media Player example.
-
@SGaist said in Creating QTableView dynamically / Creating new playlists:
As was already suggested, you should update the model.
So I should have a list of models?
@SGaist said in Creating QTableView dynamically / Creating new playlists:
how are you currently handling your playlist ?
I am still not sure how to respond? Should I post the code from PlaylistModel.h?
-
@hbatalha said in Creating QTableView dynamically / Creating new playlists:
@SGaist said in Creating QTableView dynamically / Creating new playlists:
As was already suggested, you should update the model.
So I should have a list of models?
Why ? You can replace the content of a model.
@hbatalha said in Creating QTableView dynamically / Creating new playlists:
@SGaist said in Creating QTableView dynamically / Creating new playlists:
how are you currently handling your playlist ?
I am still not sure how to respond? Should I post the code from PlaylistModel.h?
That can be a good start.
-
@SGaist said in Creating QTableView dynamically / Creating new playlists:
That can be a good start.
#ifndef PLAYLISTMODEL_H #define PLAYLISTMODEL_H #include <QAbstractTableModel> #include <qmimedata.h> #include <QStringList> #include "mltcontroller.h" #include "MltPlaylist.h" #define kDetailedMode "detailed" #define kIconsMode "icons" #define kTiledMode "tiled" class PlaylistModel : public QAbstractTableModel { Q_OBJECT public: enum ViewMode { Invalid, Detailed, Tiled, Icons, }; enum Columns { COLUMN_INDEX = 0, COLUMN_THUMBNAIL, COLUMN_RESOURCE, COLUMN_IN, COLUMN_DURATION, COLUMN_START, COLUMN_DATE, COLUMN_COUNT }; enum Fields { FIELD_INDEX = Qt::UserRole, FIELD_THUMBNAIL, FIELD_RESOURCE, FIELD_IN, FIELD_DURATION, FIELD_START, FIELD_DATE, }; static const int THUMBNAIL_WIDTH = 80; static const int THUMBNAIL_HEIGHT = 45; explicit PlaylistModel(QObject *parent = 0); ~PlaylistModel(); int rowCount(const QModelIndex& parent = QModelIndex()) const; int columnCount(const QModelIndex& parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; Qt::DropActions supportedDropActions() const; bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex()); bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); Qt::ItemFlags flags(const QModelIndex &index) const; QStringList mimeTypes() const; QMimeData *mimeData(const QModelIndexList &indexes) const; bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); QModelIndex incrementIndex(const QModelIndex& index) const; QModelIndex decrementIndex(const QModelIndex& index) const; QModelIndex createIndex(int row, int column) const; void createIfNeeded(); void showThumbnail(int row); void refreshThumbnails(); Mlt::Playlist* playlist() { return m_playlist; } void setPlaylist(Mlt::Playlist& playlist); void setInOut(int row, int in, int out); ViewMode viewMode() const; void setViewMode(ViewMode mode); signals: void created(); void cleared(); void closed(); void modified(); void loaded(); void dropped(const QMimeData *data, int row); void moveClip(int from, int to); void inChanged(int in); void outChanged(int out); public slots: void clear(); void load(); void append(Mlt::Producer&, bool emitModified = true); void insert(Mlt::Producer&, int row); void remove(int row); void update(int row, Mlt::Producer& producer, bool copyFilters = false); void updateThumbnails(int row); void appendBlank(int frames); void insertBlank(int frames, int row); void close(); void move(int from, int to); private: Mlt::Playlist* m_playlist; int m_dropRow; ViewMode m_mode; QList<int> m_rowsRemoved; }; #endif // PLAYLISTMODEL_H
-
From the looks of it, you would only need to call setPlayList that should trigger a reset of your model and you should be good to go as this will trigger the update of your UI.
-
@SGaist said in Creating QTableView dynamically / Creating new playlists:
you would only need to call setPlayList that should trigger a reset of your model and you should be good to go
How will the files added in a playlist be saved when I switched back to a playlist?
-
Since you will have several playlist, you'll likely have them stored using QList or QVector.
There's no deletion happening, you should "exchange" the current playlist with the one selected.
-
@SGaist said in Creating QTableView dynamically / Creating new playlists:
Since you will have several playlist
So I will have a
QVector<Mlt::Playlist*>
? -
@hbatalha
Yes,QVector<Mlt::Playlist*>
orQList<Mlt::Playlist*>
would be fine.If you are feeling adventurous, you might even opt for a
QMap<QString, Mlt::Playlist*>
. If your playlists are simply distinguished by their name in the droplist, saves you having to look it up! -
It doesn't work, when I switched a playlist it reload with a empty playlist.
The code:void PlaylistDock::on_addButton_clicked() { Mlt::Playlist* playlist = new Mlt::Playlist; m_playlists.append(playlist); ui->comboBox->addItem("Playlist " + QString::number(i++)); } void PlaylistDock::on_comboBox_currentIndexChanged(int) { m_model.setPlaylist(*m_playlists.at(ui->comboBox->currentIndex())); }
-
@hbatalha said in Creating QTableView dynamically / Creating new playlists:
void PlaylistDock::on_addButton_clicked()
{
Mlt::Playlist* playlist = new Mlt::Playlist;m_playlists.append(playlist); ui->comboBox->addItem("Playlist " + QString::number(i++));
}
You are creating a new empty playlist so that's normal that your model is empty after you set.
-
@hbatalha said in Creating QTableView dynamically / Creating new playlists:
void PlaylistDock::on_comboBox_currentIndexChanged(int)
{
m_model.setPlaylist(*m_playlists.at(ui->comboBox->currentIndex()));
}You are creating a new empty playlist
I mean when I switch playlist.
-
@hbatalha
As I wrote earlier, you need to use QAbstractItemModel::beginResetModel() when you change models. See e.g. https://stackoverflow.com/questions/14756645/how-to-reset-model-in-qt. -
@JonB said in Creating QTableView dynamically / Creating new playlists:
@hbatalha
As I wrote earlier, you need to use QAbstractItemModel::beginResetModel() when you change models. See e.g. https://stackoverflow.com/questions/14756645/how-to-reset-model-in-qt.The playlist model inherits from
QAbstractTableModel
as shown in the code above -
@hbatalha
Yes, and what does that inherit from...? -
@JonB
IbeginResetModel
is protected. https://doc.qt.io/qt-5/qabstractitemmodel.html#beginResetModel -
@hbatalha said in Creating QTableView dynamically / Creating new playlists:
beginResetModel
is protected. https://doc.qt.io/qt-5/qabstractitemmodel.html#beginResetModelYou wrote earlier
The playlist model inherits from
QAbstractTableModel
as shown in the code aboveSo that means you can call
protected
methods, doesn't it?If you have some aversion to resetting the model, you can probably delete all existing rows and insert all new rows if you really wish.
-
@JonB said in Creating QTableView dynamically / Creating new playlists:
So that means you can call protected methods, doesn't it?
That is what I expected, I am finding strange that the compiler is complaining that it is protected.
-
@hbatalha
If you are trying to gom_model.beginResetModel(); m_model.setPlaylist(*m_playlists.at(ui->comboBox->currentIndex())); m_model.endResetModel();
you cannot do that on
protected
methods. You must call them inPlaylistModel::setPlaylist()
. -
@JonB said in Creating QTableView dynamically / Creating new playlists:
you cannot do that on protected methods. You must call them in PlaylistModel::setPlaylist().
yeah I fixed that.
But still, the playlist will be empty when I switch. I must be missing something.