Audio duration for file manager
-
@Nemooo Just to clarify: you don't know when exactly the signal will be emitted. This is done by QMediaPlayer, not by you. So, you do not know in which order the signals will be emitted. It can work like you're doing. You should pass an additional parameter to the slot to tell it which line/index to update.
-
@Nemooo Just to clarify: you don't know when exactly the signal will be emitted. This is done by QMediaPlayer, not by you. So, you do not know in which order the signals will be emitted. It can work like you're doing. You should pass an additional parameter to the slot to tell it which line/index to update.
@jsulm said in Audio duration for file manager:
@Nemooo Just to clarify: you don't know when exactly the signal will be emitted. This is done by QMediaPlayer, not by you. So, you do not know in which order the signals will be emitted. It can work like you're doing. You should pass an additional parameter to the slot to tell it which line/index to update.
Thank you! Could you please show me the example how to do it. I am new in Qt so I don't know how to do it.
Thank you! -
@jsulm said in Audio duration for file manager:
@Nemooo Just to clarify: you don't know when exactly the signal will be emitted. This is done by QMediaPlayer, not by you. So, you do not know in which order the signals will be emitted. It can work like you're doing. You should pass an additional parameter to the slot to tell it which line/index to update.
Thank you! Could you please show me the example how to do it. I am new in Qt so I don't know how to do it.
Thank you!@Nemooo Something like (not really Qt specific):
auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { dura = static_cast<double>(dur)/60000.0; qDebug() << "duration = " << dura; filemodel->setData(index, QString().setNum(dura)); });
-
@Nemooo Something like (not really Qt specific):
auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { dura = static_cast<double>(dur)/60000.0; qDebug() << "duration = " << dura; filemodel->setData(index, QString().setNum(dura)); });
@jsulm said in Audio duration for file manager:
@Nemooo Something like (not really Qt specific):
auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { dura = static_cast<double>(dur)/60000.0; qDebug() << "duration = " << dura; filemodel->setData(index, QString().setNum(dura)); });
Now I understand what you mean but it still doesn't work
-
@jsulm said in Audio duration for file manager:
@Nemooo Something like (not really Qt specific):
auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { dura = static_cast<double>(dur)/60000.0; qDebug() << "duration = " << dura; filemodel->setData(index, QString().setNum(dura)); });
Now I understand what you mean but it still doesn't work
@Nemooo said in Audio duration for file manager:
Now I understand what you mean but it still doesn't work
Then please debug the code and find out where the problem is.
-
@jsulm said in Audio duration for file manager:
@Nemooo Something like (not really Qt specific):
auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { dura = static_cast<double>(dur)/60000.0; qDebug() << "duration = " << dura; filemodel->setData(index, QString().setNum(dura)); });
Now I understand what you mean but it still doesn't work
-
@Nemooo I think the problem is that index changes as you're inserting new rows. You need to find a way to make sure you're using the correct index. Or you first fill in all rows into your model and then use QMediaPlayer.
@jsulm said in Audio duration for file manager:
@Nemooo I think the problem is that index changes as you're inserting new rows. You need to find a way to make sure you're using the correct index. Or you first fill in all rows into your model and then use QMediaPlayer.
Yes, I feel, that mistake is somewhere in wrong index but I cannot catch how.
I did in a different ways. No chance.May be you know how can I get information about duration in mp3 file in different ways?
-
@jsulm said in Audio duration for file manager:
@Nemooo I think the problem is that index changes as you're inserting new rows. You need to find a way to make sure you're using the correct index. Or you first fill in all rows into your model and then use QMediaPlayer.
Yes, I feel, that mistake is somewhere in wrong index but I cannot catch how.
I did in a different ways. No chance.May be you know how can I get information about duration in mp3 file in different ways?
-
@Nemooo "Or you first fill in all rows into your model and then use QMediaPlayer." - what about this suggestion?
@jsulm said in Audio duration for file manager:
@Nemooo "Or you first fill in all rows into your model and then use QMediaPlayer." - what about this suggestion?
Tried to do this way
void MainWindow::CreateTable(const QString & p) { filemodel->removeRows( 0, filemodel->rowCount() ); filemodelcurrentdir = QDir(p).path(); files = QDir(p).entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); filemodel->insertRow(0); if (fi.isDir()) { filemodel->setData(filemodel->index(0, 0), name); filemodel->setData(filemodel->index(0, 0), dirIcon, Qt::DecorationRole); } else { filemodel->setData(filemodel->index(0, 0), name); filemodel->setData(filemodel->index(0, 0), fileIcon, Qt::DecorationRole); filemodel->setData(filemodel->index(0, 1), size_human(fi.size())); } } foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); if (!fi.isDir()) { QMediaPlayer* player = new QMediaPlayer(); player->setMedia(QUrl::fromLocalFile(filemodelcurrentdir + "/" + name)); auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { filemodel->setData(index, QString().setNum(static_cast<double>(dur)/60000.0)); }); delete player;} } filemodel->setHeaderData(0,Qt::Horizontal,QObject::tr("Name" )); filemodel->setHeaderData(1,Qt::Horizontal,QObject::tr("Size" )); filemodel->setHeaderData(2,Qt::Horizontal,QObject::tr("Duration" )); pTreeView2->setSortingEnabled(true); pTreeView2->setColumnWidth(0, 300); pTreeView2->setColumnWidth(1, 100); pTreeView2->setColumnWidth(2, 100); }
The result is the same - doesn't fill the last column
-
@jsulm said in Audio duration for file manager:
@Nemooo "Or you first fill in all rows into your model and then use QMediaPlayer." - what about this suggestion?
Tried to do this way
void MainWindow::CreateTable(const QString & p) { filemodel->removeRows( 0, filemodel->rowCount() ); filemodelcurrentdir = QDir(p).path(); files = QDir(p).entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); filemodel->insertRow(0); if (fi.isDir()) { filemodel->setData(filemodel->index(0, 0), name); filemodel->setData(filemodel->index(0, 0), dirIcon, Qt::DecorationRole); } else { filemodel->setData(filemodel->index(0, 0), name); filemodel->setData(filemodel->index(0, 0), fileIcon, Qt::DecorationRole); filemodel->setData(filemodel->index(0, 1), size_human(fi.size())); } } foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); if (!fi.isDir()) { QMediaPlayer* player = new QMediaPlayer(); player->setMedia(QUrl::fromLocalFile(filemodelcurrentdir + "/" + name)); auto index = filemodel->index(0, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { filemodel->setData(index, QString().setNum(static_cast<double>(dur)/60000.0)); }); delete player;} } filemodel->setHeaderData(0,Qt::Horizontal,QObject::tr("Name" )); filemodel->setHeaderData(1,Qt::Horizontal,QObject::tr("Size" )); filemodel->setHeaderData(2,Qt::Horizontal,QObject::tr("Duration" )); pTreeView2->setSortingEnabled(true); pTreeView2->setColumnWidth(0, 300); pTreeView2->setColumnWidth(1, 100); pTreeView2->setColumnWidth(2, 100); }
The result is the same - doesn't fill the last column
@Nemooo said in Audio duration for file manager:
The result is the same - doesn't fill the last column
Yes, because you need to take the correct index now instead of 0.
-
@Nemooo said in Audio duration for file manager:
The result is the same - doesn't fill the last column
Yes, because you need to take the correct index now instead of 0.
@jsulm said in Audio duration for file manager:
@Nemooo said in Audio duration for file manager:
The result is the same - doesn't fill the last column
Yes, because you need to take the correct index now instead of 0.
May be this way?
void MainWindow::CreateTable(const QString & p) { filemodel->removeRows( 0, filemodel->rowCount() ); filemodelcurrentdir = QDir(p).path(); files = QDir(p).entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); int in = 0; foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); filemodel->insertRow(in); if (fi.isDir()) { filemodel->setData(filemodel->index(in, 0), name); filemodel->setData(filemodel->index(in, 0), dirIcon, Qt::DecorationRole); } else { filemodel->setData(filemodel->index(in, 0), name); filemodel->setData(filemodel->index(in, 0), fileIcon, Qt::DecorationRole); filemodel->setData(filemodel->index(in, 1), size_human(fi.size())); } ++in; } in = 0; foreach(const QFileInfo &fi, files) { QString name = QString("%0").arg(fi.fileName()); if (!fi.isDir()) { QMediaPlayer* player = new QMediaPlayer(); player->setMedia(QUrl::fromLocalFile(filemodelcurrentdir + "/" + name)); auto index = filemodel->index(in, 2); connect(player, &QMediaPlayer::durationChanged, this, [this, index](qint64 dur) { filemodel->setData(index, QString().setNum(static_cast<double>(dur)/60000.0)); }); delete player;} ++in; } filemodel->setHeaderData(0,Qt::Horizontal,QObject::tr("Name" )); filemodel->setHeaderData(1,Qt::Horizontal,QObject::tr("Size" )); filemodel->setHeaderData(2,Qt::Horizontal,QObject::tr("Duration" )); pTreeView2->setSortingEnabled(true); pTreeView2->setColumnWidth(0, 300); pTreeView2->setColumnWidth(1, 100); pTreeView2->setColumnWidth(2, 100); }