Solved 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 thishome | Desktop | Downloads | |Picture | |Audio | | |a.mp3 | | |m.mp3 | Picture
like this.
any way to get like this in a text?? -
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
and did not understand how to use it
-
@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? -
@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?
-
-
@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);
-
I used the function of @VRonin but i am not understand what kind of model i need to pass to the function. Note that i have a model with which root path is root("/"). I tried to pass that model but it hanged my whole laptop. Please tell the right procedure to select the right QAbstractItemModel...
Thanks -
Would you guys please help me to find out...
please.