我正在开发一个GUI,它需要在布局中使用qtreeview。现在,我已经编写了一个示例代码,以了解qtreeview的工作,我遇到了一个问题。
我的问题是: 1.它应该只显示给定路径中的文件夹。
2.双倍浏览qtreeview中的文件夹应该给出列视图中文件夹的内容,并将其作为列的内容之一(在这里,我的意思是,如果我在终端中做了一个"ll“,就会得到该文件夹所有者的列表),我也想要这些信息。
这是我的密码:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(1150, 905)
self.gridLayout_2 = QtGui.QGridLayout(Dialog)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.groupBox = QtGui.QGroupBox(Dialog)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.gridLayout = QtGui.QGridLayout(self.groupBox)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.treeView = QtGui.QTreeView(self.groupBox)
self.treeView.setObjectName(_fromUtf8("treeView"))
self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)
self.columnView = QtGui.QColumnView(self.groupBox)
self.columnView.setObjectName(_fromUtf8("columnView"))
self.gridLayout.addWidget(self.columnView, 0, 1, 1, 1)
self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 2)
spacerItem = QtGui.QSpacerItem(1073, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem, 1, 0, 1, 1)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout_2.addWidget(self.pushButton, 1, 1, 1, 1)
self.fileSystemModel = QtGui.QFileSystemModel(self.treeView)
self.fileSystemModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
self.fileSystemModel.setReadOnly(False)
self.root = self.fileSystemModel.setRootPath('/home/hamanda/present_wrkng_python')
self.treeView.setModel(self.fileSystemModel)
self.treeView.setRootIndex(self.root)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.groupBox.setTitle(_translate("Dialog", "List of folders", None))
self.pushButton.setText(_translate("Dialog", "Quit", None))
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_())请帮我做这件事--我对pyqt程序很陌生
发布于 2014-10-14 18:34:18
仅获取目录:
self.filemodel.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot)若要在树中双击,请重置列视图:
ui.treeView.doubleClicked.connect(ui.columnView.setRootIndex)但是,列视图不是要显示详细信息的视图;treeView或listView将是要使用的视图。正如您所看到的,默认情况下,treeView已经给出了一些细节。我不知道如何通过这个QFileSystemModel获得所有者,您可能需要子类。
若要在一边显示文件而另一侧仅显示文件夹,您需要两个模型,一个代理模型,或者一个自定义模型。
https://stackoverflow.com/questions/26335064
复制相似问题