[Solved] Use QTreeWidget to build a filter tree for log file
-
Hi,
I'm really new to qt, especially to QTreeWidget. The final conceptual question is below (after the tree example)Suppose I have a ready huge log file, each line is comma-separated and consists of several headers and then the log msg itself.
My purpose is to build a filter tree to filter each log line by headers.Here are a few log-lines for example:
LOG_FILE , Header0001 , Header0002 , Header0003 , Header0004 , MSG1
LOG_FILE , Header0001 , Header0002 , Header0003 , Header0014 , MSG2
LOG_FILE , Header0001 , Header0002 , Header0003 , Header0024 , MSG3
LOG_FILE , Header0001 , Header0002 , Header0003 , Header0034 , MSG4LOG_FILE , Header0001 , Header0002 , Header0013 , Header0004 , MSG5
LOG_FILE , Header0001 , Header0002 , Header0013 , Header0014 , MSG6
LOG_FILE , Header0001 , Header0002 , Header0013 , Header0024 , MSG7
LOG_FILE , Header0001 , Header0002 , Header0013 , Header0034 , MSG8So I need to build a filter tree (checkable) like the following:
-LOG_FILE-Header0001 Header0002 -Header0003 -Header0004 -Header0014 -Header0024 -Header0034 -Header0013 -Header0004 -Header0014 -Header0024 -Header0034
What I can't handle is:
If I read line by line, each log line, I split it into QStringList, so I have all the headers in a list and the MSG as the last QString.
I can't find a way to determine if this series of QStrings exists in the tree yet.
If exists - no need to add anything.
If doesn't exists - I want to find the deepest last string's QTreeWidgetItem* that is common to the new series, so that I could then attach the rest of headers from that point.If I could have had the access to the list of items at depth-level i - then I could check if it consists the i-th header, go up in the hierarchy and check that the ancestors are really the previous headers, run it from the last header in the list, and thus detect the last uncommon string. But I don't know if there exists such func to get the list of all items at depth-level i... and it seems also not such efficient.
Please help :( It's part of my project before army and no idea how to handle this.
I'm sure there must be a simple way to determine if there is a route of a series in the tree and detect the place it becomes uncommon.
Any ideas?Thanks a lot.
-
Got it!
Thanks to the post here:
http://www.qtcentre.org/threads/31578-List-all-elements-of-a-QTreeWidget-from-top-to-bottom
and a little change of it:void MainWindow::visitTree(QStringList &list, QTreeWidgetItem *item, int currLevel, int finalLevel)
{
if(currLevel == (finalLevel - 1))
{
for(int i = 0, cCount = item->childCount(); i < cCount; i++)
{
list << item->child(i)->text(0);
}
return;
}
for(int i = 0; i < item->childCount(); i++)
{
visitTree(list, item->child(i), currLevel+1, finalLevel);
}
}void MainWindow::getListOfItemsAtLevelN(QTreeWidget *tree, QStringList& list, int finalLevel)
{
for(int i=0;i<tree->topLevelItemCount();++i)
{
visitTree(list, tree->topLevelItem(i), 0, finalLevel);
}
}call it:
QStringList list;
getListOfItemsAtLevelN(tree, list, 2);
for(int i=0; i<list.count(); i++)
{
ui->lw->addItem(list.at(i));
}P.S: it doesn't work if you pass 0, but for that we can simply use the topItemBLABLABLAs functions.
Thanks anyway!
-
You can mark the post [SOLVED].
Regards.
-
where? :/
-
Editing the title like this: [SOLVED] Use QTreeWidget to build a filter tree for log file.