Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating QTableView dynamically / Creating new playlists
Qt 6.11 is out! See what's new in the release blog

Creating QTableView dynamically / Creating new playlists

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 4 Posters 20.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    So I have an already created (not my code) QTableView which is used for a media playlist, it is already up and running. The problem is that the app originally was designed to have only playlist, this requirement have changed and now they want to make it possible for the user to create as many playlists as they want to.

    I have been tasked to make this possible, the requirement is that this is done with a dropdown at the top to choose a playlist. I have googled for this but few results came up and none was what I need.
    I want to know if I can do this working with the already existing QTableView/Playlist without having to re-write the whole thing.

    Is this possible? If it is, how can I do it?

    I am also thinking about grouping the tableview rows, sort of like QTreeView, can't find a way to do it.

    Worth noting that I am a beginner and this is my first time coming in contact with the model/view approach of doing things, I was comfortably using only QTableWidget

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How are these playlist created ?
      Where are they stored ?

      The most simple here is to list these playlist in your combo box and once a user selects its, reload your QTableWidget content based on it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        using a QTableView + std. item model is not that different from using
        the QTableWidget. The QTableWidget just has its model internally and with the view,
        you have it externally.

        so as @SGaist says, how is the single playlist handled now ?
        You need to be able to have a list of playlists or something of that sorts.

        H 1 Reply Last reply
        1
        • H hbatalha

          So I have an already created (not my code) QTableView which is used for a media playlist, it is already up and running. The problem is that the app originally was designed to have only playlist, this requirement have changed and now they want to make it possible for the user to create as many playlists as they want to.

          I have been tasked to make this possible, the requirement is that this is done with a dropdown at the top to choose a playlist. I have googled for this but few results came up and none was what I need.
          I want to know if I can do this working with the already existing QTableView/Playlist without having to re-write the whole thing.

          Is this possible? If it is, how can I do it?

          I am also thinking about grouping the tableview rows, sort of like QTreeView, can't find a way to do it.

          Worth noting that I am a beginner and this is my first time coming in contact with the model/view approach of doing things, I was comfortably using only QTableWidget

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @hbatalha
          Since it sounds like you already have a QTableView, I assume it's using your own model (what is its model type?). I don't see any point or need to change to a QTableWidget and copy data. When the user selects in the combo box, just change your model to the new playlist and do a begin/endResetModel(), or simlar.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            How are these playlist created ?
            Where are they stored ?

            The most simple here is to list these playlist in your combo box and once a user selects its, reload your QTableWidget content based on it.

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @SGaist @mrjj

            How are these playlist created ?
            Where are they stored ?

            I am not sure how to answer these questions. As I said I am trying to figure out the code since this is my first contact with model/view programming.

            @SGaist @JonB

            reload your QTableWidget content based on it.

            No, I stated QTableWidget just to tell my background regarding Qt. I will have to keep using QTableView.

            1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              using a QTableView + std. item model is not that different from using
              the QTableWidget. The QTableWidget just has its model internally and with the view,
              you have it externally.

              so as @SGaist says, how is the single playlist handled now ?
              You need to be able to have a list of playlists or something of that sorts.

              H Offline
              H Offline
              hbatalha
              wrote on last edited by
              #6

              @mrjj said in Creating QTableView dynamically / Creating new playlists:

              You need to be able to have a list of playlists or something of that sorts.

              I am experimenting having a list of QTableView*

              @JonB said in Creating QTableView dynamically / Creating new playlists:

              When the user selects in the combo box, just change your model to the new playlist and do a begin/endResetModel()

              Any example?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                H 1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by
                  #8

                  @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?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @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.

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #10

                      @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
                      
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        H 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          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.

                          H Offline
                          H Offline
                          hbatalha
                          wrote on last edited by
                          #12

                          @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?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            H 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              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.

                              H Offline
                              H Offline
                              hbatalha
                              wrote on last edited by
                              #14

                              @SGaist said in Creating QTableView dynamically / Creating new playlists:

                              Since you will have several playlist

                              So I will have a QVector<Mlt::Playlist*>?

                              JonBJ 1 Reply Last reply
                              0
                              • H hbatalha

                                @SGaist said in Creating QTableView dynamically / Creating new playlists:

                                Since you will have several playlist

                                So I will have a QVector<Mlt::Playlist*>?

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #15

                                @hbatalha
                                Yes, QVector<Mlt::Playlist*> or QList<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!

                                H 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @hbatalha
                                  Yes, QVector<Mlt::Playlist*> or QList<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!

                                  H Offline
                                  H Offline
                                  hbatalha
                                  wrote on last edited by hbatalha
                                  #16

                                  @JonB

                                  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()));
                                  }
                                  
                                  H 1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @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.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • H hbatalha

                                      @JonB

                                      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()));
                                      }
                                      
                                      H Offline
                                      H Offline
                                      hbatalha
                                      wrote on last edited by
                                      #18

                                      @hbatalha said in Creating QTableView dynamically / Creating new playlists:

                                      void PlaylistDock::on_comboBox_currentIndexChanged(int)
                                      {
                                      m_model.setPlaylist(*m_playlists.at(ui->comboBox->currentIndex()));
                                      }

                                      @SGaist

                                      You are creating a new empty playlist

                                      I mean when I switch playlist.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • H hbatalha

                                        @hbatalha said in Creating QTableView dynamically / Creating new playlists:

                                        void PlaylistDock::on_comboBox_currentIndexChanged(int)
                                        {
                                        m_model.setPlaylist(*m_playlists.at(ui->comboBox->currentIndex()));
                                        }

                                        @SGaist

                                        You are creating a new empty playlist

                                        I mean when I switch playlist.

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by JonB
                                        #19

                                        @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.

                                        H 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @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.

                                          H Offline
                                          H Offline
                                          hbatalha
                                          wrote on last edited by
                                          #20

                                          @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

                                          JonBJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved