pvdk
27th June 2008, 16:00
Hello people,
I've successfully subclassed QAbstractItemModel to fit my needs. It acts as a frontend for the ls command. Ls feeds output in the following form to my model:
./parentfolder:
subfolder1/
subfolder2/
./parentfolder/subfolder1:
dir/
dir2/
textfile.txt
./parentfolder/subfolder1/dir:
./parentfolder/subfolder1/dir2:
./parentfolder/subfolder2:
textfile1.txt
textfile2.txt
textfile3.txt
And displays it like in the attached image.
The only thing I can't get fixed is the multiple occurence of parents. I've been struggeling with it the whole day so I hope someone with a fresh mind can see what's wrong with my code:
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
QList<TreeItem*> parents;
QList<int> indentations;
parents << parent;
indentations << 0;
for( int r=0; r<lines.count(); r++ ) {
QString lineData = lines[r].trimmed();
if (!lineData.isEmpty()) {
qDebug() << QString(lineData) << "\n";
// Line is a dir
if (lineData.endsWith(":")) {
// Get rid of the colon
lineData.chop(1);
// Check if the string is a dot from the beginning of the ls output
if (lineData == ".") {
// Get rid of the string
lineData.clear();
}
// Check if the line starts with a dot and get rid of it before we replace the backslashes
if (lineData.startsWith("./")) {
lineData.replace(QString("./"), QString("/"));
}
qDebug() << lineData;
QStringList list = lineData.split("/", QString::SkipEmptyParts);
qDebug() << list << "start appending parent/child shit \n ";
// Reset the parent FIXME: horrible method
parents << parents.first();
for(int p = 0; p < list.size(); p++) {
QList<QVariant> column;
column << list[p];
parents.last()->appendChild(new TreeItem(column, parents.last()));
parents << parents.last()->child(parents.last()->childCount()-1);
}
} else if (lineData.endsWith("/")) {
qDebug() << "is a directory conjo!!!" << lineData << "\n";
} else {
QList<QVariant> column;
column << lineData;
parents.last()->appendChild(new TreeItem(column, parents.last()));
qDebug() << "appended file: " << lineData << "\n";
}
}
}
}
So, in short: I need my QAbstractItemModel to treat items with the same name under the same parent as one item, and not as duplicates.
Thanks in advance,
pvdk
I've successfully subclassed QAbstractItemModel to fit my needs. It acts as a frontend for the ls command. Ls feeds output in the following form to my model:
./parentfolder:
subfolder1/
subfolder2/
./parentfolder/subfolder1:
dir/
dir2/
textfile.txt
./parentfolder/subfolder1/dir:
./parentfolder/subfolder1/dir2:
./parentfolder/subfolder2:
textfile1.txt
textfile2.txt
textfile3.txt
And displays it like in the attached image.
The only thing I can't get fixed is the multiple occurence of parents. I've been struggeling with it the whole day so I hope someone with a fresh mind can see what's wrong with my code:
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
QList<TreeItem*> parents;
QList<int> indentations;
parents << parent;
indentations << 0;
for( int r=0; r<lines.count(); r++ ) {
QString lineData = lines[r].trimmed();
if (!lineData.isEmpty()) {
qDebug() << QString(lineData) << "\n";
// Line is a dir
if (lineData.endsWith(":")) {
// Get rid of the colon
lineData.chop(1);
// Check if the string is a dot from the beginning of the ls output
if (lineData == ".") {
// Get rid of the string
lineData.clear();
}
// Check if the line starts with a dot and get rid of it before we replace the backslashes
if (lineData.startsWith("./")) {
lineData.replace(QString("./"), QString("/"));
}
qDebug() << lineData;
QStringList list = lineData.split("/", QString::SkipEmptyParts);
qDebug() << list << "start appending parent/child shit \n ";
// Reset the parent FIXME: horrible method
parents << parents.first();
for(int p = 0; p < list.size(); p++) {
QList<QVariant> column;
column << list[p];
parents.last()->appendChild(new TreeItem(column, parents.last()));
parents << parents.last()->child(parents.last()->childCount()-1);
}
} else if (lineData.endsWith("/")) {
qDebug() << "is a directory conjo!!!" << lineData << "\n";
} else {
QList<QVariant> column;
column << lineData;
parents.last()->appendChild(new TreeItem(column, parents.last()));
qDebug() << "appended file: " << lineData << "\n";
}
}
}
}
So, in short: I need my QAbstractItemModel to treat items with the same name under the same parent as one item, and not as duplicates.
Thanks in advance,
pvdk