Solved List View
-
Hi all,
i am developing a small video application, where i am playing a video in full screen. i have a browse button at the top right corner of the application window. when i click on the button i am displaying a rectangle in which, i have a list model which shows the list of video songs to be played. for the list view i have set FolderModelList as model and a button with label as a delegate.
Now what i want is, when i click on the browse button and get the list of songs, which ever video song i select from the list i want it to play. but that is not happening. i want to know how can i make this work.
I tried with passing the current index of the list view to the setIndex function in the given code to play the video song. but every time i am getting the index as 0 and only one song is played even if i select different song from the list.
Can anyone please guide me how to resolve this issue ? Thankshere is my code :
import QtQuick 2.6 import QtQuick.Controls 1.5 import QtQuick.Controls.Styles 1.4 import QtQuick.Layouts 1.1 import QtMultimedia 5.4 import Qt.labs.folderlistmodel 1.0 ApplicationWindow { id: mainWindow visible: true width: 640 height: 480 property int index: -1 MediaPlayer { id: player } Item { id: playerLogic FolderListModel{ id: items folder: "file:///home/ubuntu/Documents/Sample_Examples_Qt_Qml/DummyMusicPlayer/VideoSongs/" showDirs: false showDirsFirst: false nameFilters: ["*.mp4"] } function init(){ console.log("Entered init function") if(player.PlayingState===1) { console.log("playing state is 1") player.pause(); }else if(player.PlayingState===2){ console.log("playing state is 2") player.play(); } else { console.log("calling index function") setIndex(0); } } function setIndex(i){ console.log("entered setindex function") index= i if(index<0 || index> items.count){ console.log("checking if condition") index=-1; player.source=""; }else { console.log("else part") console.log("items count >>", items.count) player.source=items.get(index,"fileURL"); player.play(); console.log("Song started to play ...!") } } } VideoOutput{ id: videoOutputArea source: player fillMode: VideoOutput.PreserveAspectCrop height: parent.height } Button{ id: playpausebutton anchors.bottom: parent.bottom anchors.left: parent.left width: 80 height: 40 text: "PlayMusic" state: "none" style: ButtonStyle{ background: Rectangle{ id: buttonbackground implicitHeight: parent.height implicitWidth: parent.width color: "#00bcd4" opacity: playpausebutton.pressed ? 0.5: 1.0 radius: 5 } } MouseArea { id: buttonmousearea anchors.fill: parent onClicked: { playerLogic.init() /*player.play()*/; } } } MouseArea { anchors.bottom: parent.bottom anchors.bottomMargin: 100 hoverEnabled: true onEntered: { playpausebutton.opacity = 0 } onExited: { playpausebutton.opacity=1 } } Button{ id: fullscreenbutton anchors.top: parent.top anchors.right: parent.right width: 80 height: 40 text: "Browse" state: "none" z:1 style: ButtonStyle{ background: Rectangle{ id: buttonBG implicitHeight: parent.height implicitWidth: parent.width color: "#00bcd4" opacity: playpausebutton.pressed ? 0.5: 1.0 radius: 5 } } MouseArea { anchors.fill: parent onClicked: { listRect.visible=true } } } Rectangle { id: listRect width: parent.width-100 height: parent.height-100 visible: false color :"#155896" anchors.centerIn: parent Component { id: listDelegate Button{ id: labelComponent width: listRect.width height: listRect.height/10 RowLayout{ anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.leftMargin: 10 Rectangle{ width: labelComponent.width/16 height: labelComponent.height-10 color: "red" } Label { id: musicListLabel text: fileName elide: Label.ElideRight color: "#000000" font.pixelSize: 18 font.family:"Myriad Pro" } } MouseArea{ anchors.fill: parent onClicked: { console.log("current index >>"+ musicListView.currentIndex) listRect.visible= false // playerLogic.setIndex(musicListView.currentIndex) } } } } ListView { id:musicListView anchors.fill: parent model: items delegate: listDelegate } } }
-
Hi,
The method below may be helpful :
change property int index: -1 to property int indexTemp: -1
change console.log("current index >>"+ musicListView.currentIndex) to
console.log("current index >>"+ index)you will find the stdout is :
current index >> 0
current index >> 1
current index >>2 -
@Naveen_D
Having a quick view at your code, I just addedplayerLogic.init();
to the MouseArea regarding "labelComponent", and it works. But maybe you need something else. This is just a quick hint.That's the code:
MouseArea{ anchors.fill: parent onClicked: { console.log("current index >>"+ musicListView.currentIndex) listRect.visible= false playerLogic.init(); // playerLogic.setIndex(musicListView.currentIndex) } }
-
Having a quick view at your code, I just added playerLogic.init();
thanks you for your reply, i have tried this sir, in that case only the first video which is in the list will be played.
-
@liubbc Thank you for your reply. issue is resolved.
-
@Naveen_D
You had better mark the topic to Resolved.