qtreeview structure in a tex file



  • i use qtreeview to create the file system .
    is there any to get the view in a text file .
    like this

    home
        | Desktop
        | Downloads
        |   |Picture
        |   |Audio
        |   |   |a.mp3
        |   |   |m.mp3
        | Picture
    

    like this.
    any way to get like this in a text??


  • Lifetime Qt Champion

    Hi,

    You'll have to iterate through it one child item at a time and build the text yourself.



  • my friend help me to archive this.here is the code.
    it takes a while to make the text .

    void getDirText(const QString &path)
    {
        // Check whether hidden file or folder collected or not
        bool isHidden = 0;
    
        // Store the directories
        QStringList dirs;
        // Store the files
        QStringList files;
    
        // Set the filltering modes
        QDir::Filters filters = QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot | QDir::NoSymLinks;
    
        // Get hidden file or folder if true
        if (isHidden)
            filters |= QDir::Hidden;
    
    
        QDirIterator it(path, filters);
    
        while (it.hasNext()) {
            it.next();
    
            if(it.fileInfo().isDir()) {
                // Skip the given path
                if (it.fileInfo().completeBaseName() == QFileInfo(path).completeBaseName())
                    continue;
    
                dirs.append(it.fileInfo().fileName());
            } else {
                files.append(it.fileInfo().fileName());
            }
        }
    
        // Make a natural sort for both file and dir lists
        QCollator col;
        col.setNumericMode(true);
    
        // Sort for directories
        std::sort(dirs.begin(), dirs.end(), col);
    
        // Sort for files
        std::sort(files.begin(), files.end(), col);
    
        // Store the sorted data to a string for saving it to file
        QString output;
    
        foreach (QString s, dirs) {
            output.append(s + "\n");
        }
    
        foreach (QString s, files) {
            output.append(s + "\n");
        }
    
        // Save path for the collected file folder list
        QString filePath = path + "/" + QFileInfo(path).baseName() + ".txt";
    
        QFile file(filePath);
        file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
        QTextStream text(&file);
        text << output;
        file.close();
    }
    
    

    thanks



  • QString treeToText(const QAbstractItemModel* model, const QModelIndex& parent=QModelIndex(), const QString& head = QString()){
    QString result;
    for(int i=0, maxRow = model->rowCount(parent);i<maxRow;++i){
    const QModelIndex currIdx = model->index(i,0,parent);
    result += head + currIdx.data().toString() + '\n';
    if(model->hasChildren(currIdx))
    result += treeToText(model,currIdx,head + "    |");
    }
    result.chop(1);
    return result;
    }
    

    then you can just QTextStream(textFile) << treeToText(myModel);



  • @VRonin said in qtreeview structure in a tex file:

    treeToText

    i am getting this error
    0_1534136680891_w.png

    and did not understand how to use it


  • Moderators

    @saber Well, the error message says what is wrong. Convert QVariant to string.
    Can you please stop posting screen-shots? It makes hard to copy your code and make changes...

    result = head + currIdx.data().toString() + '\n';
    


  • @jsulm ok .it solved the error.
    but can't understand out how to use that if only i have a folder path?


  • Moderators

    @saber said in qtreeview structure in a tex file:

    but can't understand out how to use that if only i have a folder path?

    How to use what?



  • @jsulm use the function that @VRonin gives



  • @saber said in qtreeview structure in a tex file:

    i use qtreeview to create the file system

    If you have the view you have the model

    @VRonin said in qtreeview structure in a tex file:

    then you can just QTextStream(textFile) << treeToText(myModel);


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.