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. Audio duration for file manager

Audio duration for file manager

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 2.3k Views 1 Watching
  • 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.
  • N Nemooo

    @jsulm
    Ok! Yes, emitted signal - > slot called - > still doesn't work

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #10

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

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    N 1 Reply Last reply
    0
    • jsulmJ jsulm

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

      N Offline
      N Offline
      Nemooo
      wrote on last edited by
      #11

      @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!

      jsulmJ 1 Reply Last reply
      0
      • N Nemooo

        @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!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #12

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply
        3
        • jsulmJ jsulm

          @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));
              });
          
          N Offline
          N Offline
          Nemooo
          wrote on last edited by
          #13

          @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

          aha_1980A jsulmJ 2 Replies Last reply
          0
          • N Nemooo

            @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

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #14

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

            Qt has to stay free or it will die.

            1 Reply Last reply
            1
            • N Nemooo

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #15

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              N 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                N Offline
                N Offline
                Nemooo
                wrote on last edited by
                #16

                @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?

                jsulmJ 1 Reply Last reply
                0
                • N Nemooo

                  @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?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #17

                  @Nemooo "Or you first fill in all rows into your model and then use QMediaPlayer." - what about this suggestion?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  N 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Nemooo "Or you first fill in all rows into your model and then use QMediaPlayer." - what about this suggestion?

                    N Offline
                    N Offline
                    Nemooo
                    wrote on last edited by
                    #18

                    @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

                    jsulmJ 1 Reply Last reply
                    0
                    • N Nemooo

                      @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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      N 1 Reply Last reply
                      0
                      • jsulmJ jsulm

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

                        N Offline
                        N Offline
                        Nemooo
                        wrote on last edited by
                        #20

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