2011-02-28 9 views
5

मैं किसी विशिष्ट पथ की संरचना को देखने के लिए फ़ोल्डर व्यूअर को कार्यान्वित करने का प्रयास कर रहा हूं। और यह फ़ोल्डर दृश्य PyQT में पेड़ विजेट की तरह दिखना चाहिए, मुझे पता है कि फ़ाइल संवाद मदद कर सकता है, लेकिन मुझे इसे अपनी मुख्य विंडो के अंदर रखना होगा।
मैंने QTreeWidget का उपयोग करके इसे कार्यान्वित करने का प्रयास किया और मैंने फ़ोल्डरों के अंदर लूप के लिए एक रिकर्स फ़ंक्शन का उपयोग किया, लेकिन यह बहुत धीमा है। चूंकि इसे बड़ी संख्या में फ़ोल्डरों की मरम्मत करने की आवश्यकता है। क्या यह करने का यह सही तरीका है? या इस समस्या के लिए एक तैयार क्यूटी समाधान है।
नीचे दिए गए आंकड़े की जांच करें।मुख्य विंडो के अंदर pyqt में फ़ोल्डर दृश्य कैसे बनाएं


enter image description here

उत्तर

5

उपयोग मॉडल और विचारों।

"""An example of how to use models and views in PyQt4. 
Model/view documentation can be found at 
http://doc.qt.nokia.com/latest/model-view-programming.html. 
""" 
import sys 

from PyQt4.QtGui import (QApplication, QColumnView, QFileSystemModel, 
         QSplitter, QTreeView) 
from PyQt4.QtCore import QDir, Qt 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    # Splitter to show 2 views in same widget easily. 
    splitter = QSplitter() 
    # The model. 
    model = QFileSystemModel() 
    # You can setRootPath to any path. 
    model.setRootPath(QDir.rootPath()) 
    # List of views. 
    views = [] 
    for ViewType in (QColumnView, QTreeView): 
     # Create the view in the splitter. 
     view = ViewType(splitter) 
     # Set the model of the view. 
     view.setModel(model) 
     # Set the root index of the view as the user's home directory. 
     view.setRootIndex(model.index(QDir.homePath())) 
    # Show the splitter. 
    splitter.show() 
    # Maximize the splitter. 
    splitter.setWindowState(Qt.WindowMaximized) 
    # Start the main loop. 
    sys.exit(app.exec_()) 
4

PyQt5 के लिए मैं इस समारोह किया:

def load_project_structure(startpath, tree): 
    """ 
    Load Project structure tree 
    :param startpath: 
    :param tree: 
    :return: 
    """ 
    import os 
    from PyQt5.QtWidgets import QTreeWidgetItem 
    from PyQt5.QtGui import QIcon 
    for element in os.listdir(startpath): 
     path_info = startpath + "/" + element 
     parent_itm = QTreeWidgetItem(tree, [os.path.basename(element)]) 
     if os.path.isdir(path_info): 
      load_project_structure(path_info, parent_itm) 
      parent_itm.setIcon(0, QIcon('assets/folder.ico')) 
     else: 
      parent_itm.setIcon(0, QIcon('assets/file.ico')) 

तो मैं इसे इस तरह कहते हैं:

load_project_structure("/your/path/here",projectTreeWidget) 

और मैं इस परिणाम है: enter image description here

संबंधित मुद्दे