如何获取文件名并将其显示在 Qlistwidget 上?
How to get the file name and show it on Qlistwidget?
我想通过打开QfileDialog添加文件来制作歌曲列表。
现在我可以播放列表的歌曲,当我点击QlistWidget的项目时。
但是项目名称是它的路径。
我想在 QlistWidget 上显示文件名。
当我单击 QlistWidget 的项目时,它必须将路径传输到 openaudio()
方法。
这是部分代码:
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])
这里是 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()
顺便问一下,我怎样才能一次打开多个文件?
一种方法是子类化 QListWidgetItem
:
class MyItem(QListWidgetItem):
def __init__(self, path, parent=None):
self.path = path
import os
filename = os.path.basename(self.path)
super().__init__(filename)
然后将您的 QListWidget
连接到您的 openaudio(path)
方法:
self.ui.listWidget.itemClicked.connect(lambda n: openaudio(n.path))
除了那个具体问题之外,您的代码似乎还有其他一些问题。在这种特殊情况下不需要使用附加列表 (mlist):
path = QtGui.QFileDialog.getOpenFileName(self, tips, QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.MusicLocation), expand)
if path:
self.ui.listWidget.addItem(MyItem(path))
和
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()
我想通过打开QfileDialog添加文件来制作歌曲列表。
现在我可以播放列表的歌曲,当我点击QlistWidget的项目时。
但是项目名称是它的路径。
我想在 QlistWidget 上显示文件名。
当我单击 QlistWidget 的项目时,它必须将路径传输到 openaudio()
方法。
这是部分代码:
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])
这里是 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()
顺便问一下,我怎样才能一次打开多个文件?
一种方法是子类化 QListWidgetItem
:
class MyItem(QListWidgetItem):
def __init__(self, path, parent=None):
self.path = path
import os
filename = os.path.basename(self.path)
super().__init__(filename)
然后将您的 QListWidget
连接到您的 openaudio(path)
方法:
self.ui.listWidget.itemClicked.connect(lambda n: openaudio(n.path))
除了那个具体问题之外,您的代码似乎还有其他一些问题。在这种特殊情况下不需要使用附加列表 (mlist):
path = QtGui.QFileDialog.getOpenFileName(self, tips, QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.MusicLocation), expand)
if path:
self.ui.listWidget.addItem(MyItem(path))
和
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()