Reputation: 27
I want to make a song list by open the QfileDialog to add the file .
Now I can play the song of list , When I click the item of QlistWidget.
But the item name is its path .
I want to show the file name on QlistWidget .
And when I click the item of QlistWidget , it has to transfer the path to openaudio()
method .
Here is part of code :
expand='Image Files(*.mp3 *.wav)'
tips=open the file''
path = QtGui.QFileDialog.getOpenFileName(self, tips,QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.MusicLocation), expand)
if path:
mlist.append(path)
index=mlist.index(path)
self.ui.listWidget.insertItem(index,mlist[index])
Here is code of openaudio() :
def openaudio(self,path):
self.connect(self.ui.listWidget,QtCore.SIGNAL('currentTextChanged(QString)'),self.ui.label_4,QtCore.SLOT('setText(QString)'))
index=self.ui.listWidget.currentRow()
path=mlist[index]
self.mediaObject.setCurrentSource(phonon.Phonon.MediaSource(path))
self.mediaObject.play()
By the way ,how can I open multiple files at once ?
Upvotes: 0
Views: 1534
Reputation: 831
One way would be subclassing QListWidgetItem
:
class MyItem(QListWidgetItem):
def __init__(self, path, parent=None):
self.path = path
import os
filename = os.path.basename(self.path)
super().__init__(filename)
And then connecting your QListWidget
to your openaudio(path)
method:
self.ui.listWidget.itemClicked.connect(lambda n: openaudio(n.path))
Apart from that specific question your code seems to have some other issues. Using an additional list (mlist) is not needed in this particular case:
path = QtGui.QFileDialog.getOpenFileName(self, tips, QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.MusicLocation), expand)
if path:
self.ui.listWidget.addItem(MyItem(path))
and
def openaudio(self, path):
# Do not do this here! Connections should usually be made in the init() method of the container!
# self.connect(self.ui.listWidget,QtCore.SIGNAL('currentTextChanged(QString)'),self.ui.label_4,QtCore.SLOT('setText(QString)'))
# Also take a look at the 'new' PyQt signals & slots style:
# self.ui.listWidget.currentTextChanged.connect(self.ui.label_4.setText)
self.mediaObject.setCurrentSource(phonon.Phonon.MediaSource(path))
self.mediaObject.play()
Upvotes: 2