QTreewidget adding child based on tab space in the beginning
-
Hi Qt community i am new to Qt i started using it a month back i need your help with the Qt tree widget topic
I am able to add root, child and child to the child(grandchild) with the fallowing code decision on parent and child is made based on the number of tab spaces in the beginning of the line
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
root = new QTreeWidgetItem(ui->treeWidget);
ui->treeWidget->addTopLevelItem(root);
subroot = new QTreeWidgetItem();
child = new QTreeWidgetItem();//opening file to read QFile file("/home/download/text2.txt"); if(!file.open(QIODevice::ReadOnly)) { qDebug() << "file not opened"; } QTextStream stream(&file); QString line; do { line = stream.readLine(); int tabcount = line.count('\t'); if (tabcount == 0) { root = new QTreeWidgetItem(ui->treeWidget); root->setText(0,line); } else if (tabcount == 1) { subroot = new QTreeWidgetItem(); subroot->setText(0,line); root->addChild(subroot); } else { child = new QTreeWidgetItem(); child->setText(0,line); subroot->addChild(child); } }while(!line.isNull()); file.close();
}
MainWindow::~MainWindow()
{
delete ui;
}but now i want to make this code as generic means if a line has 'N' number of tab spaces in the beginning it should become the child to the line with 'N-1' tab spaces
please suggest a way how i can implement this Thanks in advance
-
Hi and welcome to devnet,
You need to implement a loop that will get down the three as much as required to add a child. Don't forget to check that each item is valid.
You may want to add some form of file validation to ensure its structure is valid.
-
@123newuser
You can do this iteratively in a loop in your current method, in place of your existingelse if (tabcount == 1) ... else ...
. No need for a new class. Algorithmically something like:QTreeWidgetItem *parent = root; while (tabcount > 0) { QTreeWidgetItem *child = new QTreeWidgetItem(); parent->addChild(child); parent = child; tabcount--; if (tabcount == 0) // created leaf, set text child->setText(0, line); }
-
@123newuser In the case of 2 levels, it has a lower number of possibilities than the higher levels, so these new possibilities must be analyzed. Considering the above I have created the following demo:
#include <QApplication> #include <QDebug> #include <QFile> #include <QTreeWidget> #include <QtGlobal> void fillView(QIODevice *device, QTreeWidget *treeWidget){ if(!device || !treeWidget){ return; } QString line; int level; QString word; QTreeWidgetItem *item = nullptr; QList<QTreeWidgetItem *> rootItems; QTextStream stream(device); while (stream.readLineInto(&line)) { level = line.count("\t"); word = line; if(level > 0){ word = line.mid(level); } if(level == 0){ item = new QTreeWidgetItem(treeWidget); rootItems.clear(); rootItems.append(item); } else if(level == rootItems.length()){ item = new QTreeWidgetItem(rootItems.last()); rootItems.append(item); } else if(level > rootItems.length()){ QTreeWidgetItem *parentItem = rootItems.last(); for(int i=rootItems.length() -1; i < level; ++i){ item = new QTreeWidgetItem(parentItem); parentItem = item; rootItems.append(item); } } else{ item = new QTreeWidgetItem(rootItems.at(level -1)); rootItems = rootItems.mid(0, level); rootItems.append(item); } if(item) item->setText(0, word); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile file("/home/qt/test.txt"); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return EXIT_FAILURE; QTreeWidget view; fillView(&file, &view); view.resize(640, 480); view.expandAll(); view.show(); return a.exec(); }
test.txt
rootA childA childB rootB childA childC childA childD
Output:
-
@eyllanesc thank you i will try this
-
@eyllanesc sir i tried the solution you have provided it works thank you
but i want to understand the approach can you explain please that would be a great help
-
@123newuser My code is clear and simple so I don't understand what I should explain.
-
@eyllanesc i got confused with "mid" but now i read the qt documentation its clear thank you for your time