Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML and QFileSystemModel how to get the current path+file as a String to use as a source for Mediaplayer
Forum Updated to NodeBB v4.3 + New Features

QML and QFileSystemModel how to get the current path+file as a String to use as a source for Mediaplayer

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 2.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.
  • M Offline
    M Offline
    Markus Ippy
    wrote on last edited by
    #1

    Hello i am trying to use qt multimedia to build a simple mp3 player in a single window . I am using QFileSystemModel in C++ to show the file system in QML == > this works so far
    I can browse through the file system but i can not use the data further .

    1. How can i get the filepath from the treeview ? So i can set it as a source for the media player ?
    2. Is there a way to tell mediaplayer to play all the mp3 files for a directory

    my Main.cpp

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QtQml>
    #include <QFileSystemModel>
    
    
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    app.setOrganizationName("Power-Tune");
    app.setOrganizationDomain("power-tune.org");
    app.setApplicationName("PowerTune");
    
    QQmlApplicationEngine engine;
    //
    QFileSystemModel model;
    model.setRootPath("/");
    engine.rootContext()->setContextProperty("my_model", &model);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
    
    }
    

    my main QML

    import QtQuick 2.8
    import QtQuick.Controls 2.1
    import QtQuick.Layouts 1.1
    import QtQuick.Controls 1.4
    import QtMultimedia 5.8
    
    ApplicationWindow {
    visible: true
    width: 800
    height: 480
    minimumWidth: 800
    minimumHeight: 480
    title: qsTr("PowerTune ")
    color: "black"
    
    Rectangle {
        width: parent.width
        height: parent.height
        property bool playing: false
    
        MediaPlayer {
            id: playMusic
            autoPlay: false
            volume: 0.5
        }
    
        Button {
            id: previous
            text: "previous"
            width: 100
            anchors.right: playpause.left
            // Here i would like to go to the previous *.mp3 file in the current folder
            //onClicked:
       }
        Button {
            id: playpause
            text: "play/pause" //playing ? "Stop music" : "Start music"
            width: 100
            anchors.right: next.left
    
            onClicked:{
                if(playMusic.status == 6) {
                    playMusic.stop()
                } else {
                    playMusic.play()
                }
            }
        }
    
        Button {
            id: next
             text: "next"
            width: 100
            anchors.right: parent.right
            // Here i would like to go to the next *.mp3 file in the current folder
            //onClicked:
      }
    
    
    TreeView {
        id:mp3selector
        width: parent.width/2
        height: parent.height
        visible: true
        TableViewColumn {
            title: "Name"
            role: "fileName"
            width: 300
        }
        model: my_model
        //Here i would like to set the source of the MediaPlayer with the current selection
        //onActivated: playMusic.source = 
    }
    }
    }
    
    DiracsbracketD 1 Reply Last reply
    0
    • M Markus Ippy

      Hello i am trying to use qt multimedia to build a simple mp3 player in a single window . I am using QFileSystemModel in C++ to show the file system in QML == > this works so far
      I can browse through the file system but i can not use the data further .

      1. How can i get the filepath from the treeview ? So i can set it as a source for the media player ?
      2. Is there a way to tell mediaplayer to play all the mp3 files for a directory

      my Main.cpp

      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      #include <QtQml>
      #include <QFileSystemModel>
      
      
      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);
      app.setOrganizationName("Power-Tune");
      app.setOrganizationDomain("power-tune.org");
      app.setApplicationName("PowerTune");
      
      QQmlApplicationEngine engine;
      //
      QFileSystemModel model;
      model.setRootPath("/");
      engine.rootContext()->setContextProperty("my_model", &model);
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      return app.exec();
      
      }
      

      my main QML

      import QtQuick 2.8
      import QtQuick.Controls 2.1
      import QtQuick.Layouts 1.1
      import QtQuick.Controls 1.4
      import QtMultimedia 5.8
      
      ApplicationWindow {
      visible: true
      width: 800
      height: 480
      minimumWidth: 800
      minimumHeight: 480
      title: qsTr("PowerTune ")
      color: "black"
      
      Rectangle {
          width: parent.width
          height: parent.height
          property bool playing: false
      
          MediaPlayer {
              id: playMusic
              autoPlay: false
              volume: 0.5
          }
      
          Button {
              id: previous
              text: "previous"
              width: 100
              anchors.right: playpause.left
              // Here i would like to go to the previous *.mp3 file in the current folder
              //onClicked:
         }
          Button {
              id: playpause
              text: "play/pause" //playing ? "Stop music" : "Start music"
              width: 100
              anchors.right: next.left
      
              onClicked:{
                  if(playMusic.status == 6) {
                      playMusic.stop()
                  } else {
                      playMusic.play()
                  }
              }
          }
      
          Button {
              id: next
               text: "next"
              width: 100
              anchors.right: parent.right
              // Here i would like to go to the next *.mp3 file in the current folder
              //onClicked:
        }
      
      
      TreeView {
          id:mp3selector
          width: parent.width/2
          height: parent.height
          visible: true
          TableViewColumn {
              title: "Name"
              role: "fileName"
              width: 300
          }
          model: my_model
          //Here i would like to set the source of the MediaPlayer with the current selection
          //onActivated: playMusic.source = 
      }
      }
      }
      
      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      @Markus-Ippy said in QML and QFileSystemModel how to get the current path+file as a String to use as a source for Mediaplayer:

      How can i get the filepath from the treeview ? So i can set it as a source for the media player ?

      The answer can be found in this QtQuick example:
      https://doc.qt.io/qt-5.10/qtquickcontrols-filesystembrowser-example.html

      The solution is create a class derived from QFileSystemModel and add your own methods to interface to QML to get the file info. This seems fairly simple, glancing at the example.

      Is there a way to tell mediaplayer to play all the mp3 files for a directory

      The way seems to be to set the playlist property of the MediaPlayer to some PlayList type object you must create and by adding all the supported files in the directory to it.

      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