ListWidget help



  • Hey, I am doing Media Player and I stucked at few things... Since I am pretty new at programming, I would need a bit of help...

    So if you look at the image below...

    1. problem: Songs in listWidget... I used function @ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove);@ to move them around, but a problem is when I move a song to the other position (lets say that I move song "Main Theme" on the top of the widget) and press play, it will start with the song "Fallen Friend"... It plays a songs in order they were added in listWidget and not in the order that is set on listWidget...

    2. problem: Highlight a song that is currently playing... Probably somehow connected with 1. problem, as it won't even recognize which song on the list is playing

    3. problem: Add song... I can add whole folder as a playlist, but don't really know how to add only one song...

    Link to image -> http://imageshack.us/photo/my-images/268/17co.png/

    !http://imageshack.us/photo/my-images/268/17co.png/(Image)!

    I hope you can help me :)

    Regards


  • Moderators

    Hi,
    Few Questions regarding your queries,

    For problem 1:
    When you drag and drop an item to new position and press play button how do you fetch the item which is to be played ?

    For problem 2:
    Do you mean when the current song finishes you want to highlight the next song ?
    If so, then just take the index of the current song's row as,
    @int index = ui->listWidget->currentIndex().row();
    index++;
    @
    and then increment index by 1 and set using
    @ui->listWidget->setCurrentRow(index);@

    For problem 3:
    To add a single song is to add a single item to your listwidget. You can add it like this,
    @QListWidgetItem *itm = new QListWidgetItem(ui->listWidget);

    itm->setText("Song Name");
    

    @
    Thus the new song will be added to end of the list.



  • Hey, thanks for your response...

    1. When I add playlist to a listWidget, it puts it there in certain order and when I press "Play" it plays it in that order... When I change that order (with dragging songs around), it still plays in the same order... So what I think is, player is not associated with widgetList... It plays in same order songs were placed into list and does not change order if order is changed in listWidget...

    2. I want to highlight the song that is currently playing...

    3. WIll it also add a song to a player??? I have figured out how to add song to a playlist, but once again, player won't recognize song on a list...

    Here is source of my app...

    cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtMultimedia/QMediaPlayer>
    #include <QtMultimedia/QMediaPlaylist>
    #include <QStringList>
    #include <QString>
    #include <QFileInfo>
    #include <QList>
    #include <QDir>
    #include <QFileDialog>
    #include <QDateTime>
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    player = new QMediaPlayer(this);
    playlist = new QMediaPlaylist(player);
    //player->setMedia(QUrl::fromLocalFile("C:/Users/Marin/Desktop/Narodni/Made in the USA.mp3"));
    player->setPlaylist(playlist);
    player->setVolume(35);

    //connect(ui->Play, SIGNAL(clicked(&#41;), player, SLOT(play()));
    connect(ui->Pause, SIGNAL(clicked()), player, SLOT(pause()));
    connect(ui->Stop, SIGNAL(clicked()), player, SLOT(stop()));
    connect(ui->Volume, SIGNAL(sliderMoved(int)), player, SLOT(setVolume(int)));
    connect(ui->Previous, SIGNAL(clicked()), playlist, SLOT(previous()));
    connect(ui->Shuffle, SIGNAL(clicked()), playlist, SLOT(shuffle()));
    connect(ui->Next, SIGNAL(clicked()), playlist, SLOT(next()));
    //connect(ui->Clear, SIGNAL(clicked()), ui->listWidget, SLOT(clear()));
    
    //Timer
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeLabel()));
    timer->start(1000);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_Play_clicked() //Play button
    {
    ui->Play->setEnabled(false);
    ui->Pause->setEnabled(true);
    ui->Stop->setEnabled(true);

    }

    void MainWindow::on_Stop_clicked() //Stop button
    {
    ui->Stop->setEnabled(false);
    ui->Pause->setEnabled(false);
    ui->Play->setEnabled(true);
    }

    void MainWindow::on_Pause_clicked() //Pause button
    {
    ui->Play->setEnabled(true);
    ui->Stop->setEnabled(true);
    ui->Pause->setEnabled(false);
    }

    void MainWindow::on_Playlist_clicked() //Playlist button
    {
    QString directory = QFileDialog::getExistingDirectory(this,tr("Select directory for files to import"));
    if(directory.isEmpty())
    return;
    QDir dir(directory);
    QStringList files = dir.entryList(QStringList() << "*.mp3",QDir::Files);
    QList<QMediaContent> content;
    for(const QString& f:files)
    {
    content.push_back(QUrl::fromLocalFile(dir.path()+"/" + f));
    QFileInfo fi(f);
    ui->listWidget->addItem(fi.fileName());
    }
    ui->listWidget->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    playlist->addMedia(content);
    }

    void MainWindow::on_addSong_clicked()
    {

    }

    void MainWindow::timeLabel()
    {

    ui->timeLabel->setText(QTime::currentTime().toString("h:mm:ss"));
    

    }
    @

    h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMediaPlaylist>
    #include <QMediaPlayer>
    #include <QMainWindow>
    #include <QTimer>
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void MainWindow::stop();
    void MainWindow::start();
    void MainWindow::next();
    void MainWindow::previous();
    void MainWindow::shuffle();

    private slots:
    void on_Play_clicked();
    void on_Stop_clicked();
    void on_Pause_clicked();
    void on_Playlist_clicked();
    void timeLabel();
    void on_addSong_clicked();

    private:
    Ui::MainWindow *ui;
    QMediaPlaylist *playlist;
    QMediaPlayer *player;
    QTimer *timer;
    };

    #endif // MAINWINDOW_H
    @


  • Moderators

    bq. 1. When I add playlist to a listWidget, it puts it there in certain order and when I press “Play” it plays it in that order… When I change that order (with dragging songs around), it still plays in the same order… So what I think is, player is not associated with widgetList… It plays in same order songs were placed into list and does not change order if order is changed in listWidget…

    Yes the playlist is not associated with listwidget. You need to load the playlist again with the new positions as per listwidget.

    bq. 2. I want to highlight the song that is currently playing…

    Just set the new item as selected or set the current row as said previously.

    bq. 3. WIll it also add a song to a player??? I have figured out how to add song to a playlist, but once again, player won’t recognize song on a list…

    I guess it will not be added to player as the Listwidget is not associated to the player. As you add new song to the listwidget add it to the playlist.

    See "insertMedia":http://qt-project.org/doc/qt-5.0/qtmultimedia/qmediaplaylist.html#insertMedia



  • Solved


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.