0
我的目標是當打開按鈕被點擊時,它會顯示目錄中的所有文件。當您單擊任何目錄時,它將在另一個框架中顯示該目錄的內容。現在我將如何將列表中的每個項目連接到一個可以獲取目錄內容的函數。這裏是我的代碼:如何將函數與listWidget中的每個項目相關聯?
from PyQt4 import QtCore, QtGui
import os
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(20, 20, 361, 201))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.listWidget = QtGui.QListWidget(self.frame)
self.listWidget.setGeometry(QtCore.QRect(0, 0, 91, 201))
self.listWidget.setObjectName("listWidget")
self.widget = QtGui.QWidget(self.frame)
self.widget.setGeometry(QtCore.QRect(110, 20, 120, 80))
self.widget.setObjectName("widget")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.add)
#QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("accepted()"), self.test)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def add(self):
item=[]
item = os.listdir('.')
for value in item:
self.listWidget.addItem(value)
def test(self):
print 'hello'
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
這沒有奏效。 :( – Jack 2012-02-14 21:12:35
哦,對,連接線是'QtCore.QObject.connect(self.listWidget,QtCore.SIGNAL(「itemDoubleClicked(QListWidgetItem *)」),self.test)',注意信號規範中的星號和測試方法應該定義爲'def test(self,item)' – Gianluca 2012-02-14 21:32:12
Ya我剛纔知道了。謝謝你 – Jack 2012-02-14 21:33:29