如何获取文件名并在Qlistwidget上显示?

时间:2015-07-26 17:08:02

标签: python pyqt

我想通过打开QfileDialog来创建一个歌曲列表来添加文件 现在我可以播放列表的歌曲,当我点击QlistWidget的项目时 但项目名称是其路径 我想在QlistWidget上显示文件名 当我点击QlistWidget的项目时,它必须将路径转移到<!doctype html> <html> <head> <meta charset="utf-8"> <title>Movie Night Reviews</title> <link href="styles/main.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <header id="top"> <h1>Movie Night Reviews</h1> <nav id="mainnav"> <ul> <li><a href="index.html" class="thispage">Home</a> </li> <li><a href="trailers.html">Trailers</a> </li> <li class="review"><a href="reviews.html">Reviews</a> <ul id="dropdown1"> <li><a href="civil_war.html">Civil War</a> </li> <li><a href="furious_7.html">Furious 7</a> </li> <li><a href="fantastic_four.html">Fantastic Four</a> </li> <li><a href="jurassic_world.html">Jurassic World</a> </li> <li><a href="agents_of_shield.html">Agents of Shield</a> </li> </ul> </li> <li><a href="about_us.html">About Us</a> </li> <li><a href="contact_us.html">Contact Us</a> </li> </ul> </nav> </header> <div id="slideshow"> <div id="comslider_in_point_720546"></div> <script type="text/javascript"> var oCOMScript720546 = document.createElement('script'); oCOMScript720546.src = "comslider720546/comsliderd.js?timestamp=1437916662"; oCOMScript720546.type = 'text/javascript'; document.getElementsByTagName("head").item(0).appendChild(oCOMScript720546); </script> </div> <article id="main"> <h2>Top 10 Highest Grossing Movies</h2> <p>The following list is of the highest grossing movies ever made. Director James Cameron currently holds the top two spots after directing both Avartar and Titanic.</p> <ol> <li>Avatar</li> <li>Titanic</li> <li>Juassic World</li> <li>The Avengers</li> <li>Furious 7</li> <li>Avengers: Age of Ultron</li> <li>Harry Potter and the Deathly Hallows – Part 2</li> <li>Frozen</li> <li>Iron Man 3</li> <li>Transformers: Dark of the Moon</li> </ol> <figure class="centered"> <img src="Images/titanic.jpg" width="400" height="266" alt="" /> <figcaption>Titanic, the oldest film in the top 10 still sits in a comfy second place just below Avatar. Both directed by world-renowned director James Cameron.</figcaption> </figure> <h2>Top 10 Highest Grossing Movie Franchises</h2> <p>The following list is of the Top 10 Highest Grossing Movie Franchises of all time. The Marvel Cinematic Universe franchise currently sits at the top nearly a billion higher than Harry Potter and roughly 2.5 billion higher than James Bond which is the closest franchise that is also still running.</p> <ol> <li>Marvel Cinematic Universe</li> <li>Harry Potter</li> <li>James Bond</li> <li>Middle-Earth</li> <li>Star Wars</li> <li>Spider-Man</li> <li>The Fast and the Furious</li> <li>Batman</li> <li>Transformers</li> <li>Pirates of the Caribbean</li> </ol> <figure class="centered"> <img src="Images/antman.jpg" width="400" height="266" alt="" /> <figcaption>Ant-Man, the latest film in the Marvel Cinematic Universe franchise looks set to push them even further ahead than the rest of the Top 10.</figcaption> </figure> </article> <aside id="sidebar"> <h2>Tweets by IMDb</h2> <a class="twitter-timeline" href="https://twitter.com/IMDb" data-widget-id="625318734555693056">Tweets by @IMDb</a> <script> ! function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + "://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } }(document, "script", "twitter-wjs"); </script> </aside> <footer> <p>&copy; Copyright 2013 Bayside Beat</p> </footer> </div> </body> </html>方法。

以下是代码的一部分:

openaudio()

这是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])

顺便说一句,如何一次打开多个文件?

1 个答案:

答案 0 :(得分:1)

一种方法是继承QListWidgetItem

class MyItem(QListWidgetItem):
    def __init__(self, path, parent=None):
        self.path = path

        import os
        filename = os.path.basename(self.path)
        super().__init__(filename)

然后将QListWidgetopenaudio(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()