QML and QFileSystemModel how to get the current path+file as a String to use as a source for Mediaplayer
-
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 .- How can i get the filepath from the treeview ? So i can set it as a source for the media player ?
- 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 = } } } -
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 .- How can i get the filepath from the treeview ? So i can set it as a source for the media player ?
- 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 = } } }@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.htmlThe 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
playlistproperty of theMediaPlayerto somePlayListtype object you must create and by adding all the supported files in the directory to it.