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. [SOLVED] Highlight current item
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Highlight current item

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 4.9k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

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

      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.

      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
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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"));
        

        }
        @

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

          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

          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
          0
          • N Offline
            N Offline
            NicuPopescu
            wrote on last edited by
            #5

            @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));
            @

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NicuPopescu
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

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

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

                    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

                    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
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

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

                      Used this and works... Thank you

                      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