Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreewidget adding child based on tab space in the beginning
Forum Updated to NodeBB v4.3 + New Features

QTreewidget adding child based on tab space in the beginning

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 123newuser1 Offline
    123newuser1 Offline
    123newuser
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      123newuser1 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        123newuser1 Offline
        123newuser1 Offline
        123newuser
        wrote on last edited by
        #3

        @SGaist can you show how to implement that should i create a separate class for adding a child or i can do in this only

        JonBJ 1 Reply Last reply
        0
        • 123newuser1 123newuser

          @SGaist can you show how to implement that should i create a separate class for adding a child or i can do in this only

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @123newuser
          You can do this iteratively in a loop in your current method, in place of your existing else 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);
          }
          
          123newuser1 1 Reply Last reply
          0
          • JonBJ JonB

            @123newuser
            You can do this iteratively in a loop in your current method, in place of your existing else 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);
            }
            
            123newuser1 Offline
            123newuser1 Offline
            123newuser
            wrote on last edited by
            #5

            @JonB i tried with the method you mentioned i still dint get please help me with this

            eyllanescE 1 Reply Last reply
            0
            • 123newuser1 123newuser

              @JonB i tried with the method you mentioned i still dint get please help me with this

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by eyllanesc
              #6

              @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:

              Screenshot_20210916_025519.png

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              123newuser1 2 Replies Last reply
              2
              • eyllanescE eyllanesc

                @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:

                Screenshot_20210916_025519.png

                123newuser1 Offline
                123newuser1 Offline
                123newuser
                wrote on last edited by
                #7

                @eyllanesc thank you i will try this

                1 Reply Last reply
                0
                • eyllanescE eyllanesc

                  @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:

                  Screenshot_20210916_025519.png

                  123newuser1 Offline
                  123newuser1 Offline
                  123newuser
                  wrote on last edited by
                  #8

                  @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

                  eyllanescE 1 Reply Last reply
                  0
                  • 123newuser1 123newuser

                    @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

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #9

                    @123newuser My code is clear and simple so I don't understand what I should explain.

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    123newuser1 1 Reply Last reply
                    1
                    • eyllanescE eyllanesc

                      @123newuser My code is clear and simple so I don't understand what I should explain.

                      123newuser1 Offline
                      123newuser1 Offline
                      123newuser
                      wrote on last edited by
                      #10

                      @eyllanesc i got confused with "mid" but now i read the qt documentation its clear thank you for your time

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved