[SOLVED] Highlight current item



  • Hello, I am doing media player and want to highlight item that is current playing... I have no idea how to do that, tried many things and none of them worked...
    I made it to highlight item, but it always just highlight first item... When next song playing it does not highlight next song.... Can someone tell me how to make it follow current playing song and highlight it???

    I used this @ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 0, 0, 127));@

    !http://img194.imageshack.us/img194/6610/rplh.png(image)!

    Thank you


  • Lifetime Qt Champion

    Hi,

    When the next song start, you have to signal it somewhere and then react by clearing the background of the current item, make the new item current and then set its background.



  • Hey, can you show me on my example, please... I have no idea how to do that...

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    player = new QMediaPlayer(this);
    playlist = new QMediaPlaylist();
    //player->setMedia(QUrl::fromLocalFile("C:/Users/Marin/Desktop/Narodni/Made in the USA.mp3"));
    player->setPlaylist(playlist);
    
    player->setVolume(75);
    
    connect(ui->Play, SIGNAL(clicked()), 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()));
    //playlist->currentIndex(setBackgroundRole(QPalette::Highlight));
    
    
    
    //Timer
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeLabel()));
    timer->start(1000);
    
    ui->listWidget1->setDragDropMode(QAbstractItemView::InternalMove);
    
    ui->comboBox->addItem("0.5x", QVariant(0.5));
        ui->comboBox->addItem("1.0x", QVariant(1.0));
        ui->comboBox->addItem("2.0x", QVariant(2.0));
        ui->comboBox->setCurrentIndex(1);
    
    ui->Play->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    

    }

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

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

    }

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

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

    void MainWindow::on_Playlist_clicked()
    {
    QString directory = QFileDialog::getExistingDirectory(this,tr("Select directory for files to import"),"C:/Users/Marin/Desktop/Narodni");
    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->listWidget1->addItem(fi.fileName());
    }
    ui->listWidget1->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    //ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    playlist->addMedia(content);
    ui->listWidget1->item(0)->setSelected(true);
    // ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 0, 0, 127));
    }

    void MainWindow::on_addSong_clicked()
    {
    //const QFileDialog::Options options = QFlag(fileDialogOptionsWidget->value());
    QString selectedFilter;
    QStringList files = QFileDialog::getOpenFileNames(
    this, tr("QFileDialog::getOpenFileNames()"),
    openFilesPath,
    tr("MP3 Files (*.mp3)"),
    &selectedFilter
    );

    /*QDir dir(files);
    QList<QMediaContent> content;
    for(const QString& f:files)
    {
        content.push_back(QUrl::fromStringList(dir.filePath()+"/" + f));
        QFileInfo fi(f);
        ui->listWidget1->addItem(fi.fileName());
    
    }
    playlist->addMedia(content);*/
    if (files.count()) {
        openFilesPath = files[0];
        ui->label->setText(QString("[%1]").arg(files.join(", ")));
    
    }
    

    }

    void MainWindow::timeLabel()
    {

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

    }
    @


  • Lifetime Qt Champion

    You can use the QMediaPlayer signal mediaChanged . Connect it to a slot in your MainWindow, then search in your listWidget for a content similar to the new media content path and update the listWidget background



  • @ui->listWidget1->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    //ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    playlist->addMedia(content);
    ui->listWidget1->item(0)->setSelected(true);@

    ui->listWidget1->item(0)->setSelected(true) does always first row selection ! what would you do that?

    this alone should work if you have a 1:1 mapping of playlist to the listwidget:
    @ui->listWidget1->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 0, 0, 127));
    @



  • Hey... It works somehow, but still not as it supposed to do... Highlight works only when I manually use previous/next button or when next song on the list is played... But does not work when I press shuffle... It change song but highlingt does not move to that song, but remains on the last played...

    @
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(updatef()));

    void MainWindow::updatef()
    {
    ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 255, 255));
    ui->listWidget1->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 0, 0, 127));
    }

    @

    Also, how could I make it to be able to click on song and it start playing and get highlighted??? Now I can click but nothing happens...

    Also how to get rid of that blue overflow??? Some blue line is lying on my red highlight...

    !http://img132.imageshack.us/img132/1108/rxst.png(bh)!

    EDIT:
    Also tried with connect(player, SIGNAL(mediaChanged(QMediaContent)), this, SLOT(updatef()));

    but shuffle still does not work...



  • try smth like:

    @ ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 255, 255));
    int index = playlist->currentIndex() != -1? playlist->currentIndex():0;
    ui->listWidget1->setCurrentItem(ui->listWidget1->item(index));
    ui->listWidget1->currentItem()->setBackgroundColor(QColor(255, 0, 0, 127));@

    your picture is not downloadable!



  • Does not make any difference... It's same... Shuffle still does not work... And blue overflow is still there...

    What you you mean with "your picture is not downloadable!"??? You should see my picture posted here on forum... I can see it... Else go to link
    http://img132.imageshack.us/img132/1108/rxst.png

    P.S: Blue highlight goes away when I click some other element and appears only when I click insde the listwidget... But still, its annoying...


  • Lifetime Qt Champion

    The blue line is probably the focus rectangle since the item is selected.

    Like I said, you have to search through your list widget for the item corresponding to the new song playing then do your background stuff



  • @ui->listWidget1->setFocusPolicy(Qt::NoFocus);@

    Used this and works... Thank you


Log in to reply
 

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