I got the answer . MainWindow.cpp is like that
ui->listWidget->setFlow(QListView::LeftToRight);
ui->listWidget->setMinimumSize(1050,800);
ui->listWidget->setGridSize(QSize(340, 320));
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->setViewMode(QListView::ListMode);
ui->listWidget->setWrapping(true);
QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
for(const QFileInfo & finfo: directory.entryInfoList()){
QMediaPlayer *mediaPlayer = new QMediaPlayer();
mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
videoItem = new QGraphicsVideoItem;
videoItem->setSize(QSize(300,240));
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *graphicsView = new QGraphicsView(scene);
mediaPlayer->setVideoOutput(videoItem);
QPushButton *m_playButton = new QPushButton();
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
switch (mediaPlayer->state()) {
case QMediaPlayer::PlayingState:
mediaPlayer->pause();
break;
default:
mediaPlayer->play();
break;
}
});
connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) {
switch(state) {
case QMediaPlayer::PlayingState:
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
break;
default:
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
}
});
QSlider *m_positionSlider = new QSlider(Qt::Horizontal,this);
m_positionSlider->setRange(0,mediaPlayer->duration() / 1000);
connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){
m_positionSlider->setValue(position);
});
connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){
m_positionSlider->setRange(0,duration);
});
connect(m_positionSlider ,&QAbstractSlider::sliderMoved, [mediaPlayer ,this] (int position){
mediaPlayer->setPosition(position);
});
auto item = new QListWidgetItem("", ui->listWidget);
auto widget = new QWidget;
auto label = new QLabel(finfo.fileName());
auto vb = new QVBoxLayout;
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(m_playButton);
controlLayout->addWidget(m_positionSlider);
vb->addWidget(label);
vb->addWidget(graphicsView);
vb->addLayout(controlLayout);
widget->setLayout(vb);
widget->setMinimumSize(320, 320);
ui->listWidget->setItemWidget(item,widget);
player.append(mediaPlayer);
scene->addItem(videoItem);
}