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. generating tree using Qtreewidget
Qt 6.11 is out! See what's new in the release blog

generating tree using Qtreewidget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 459 Views
  • 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.
  • K Offline
    K Offline
    kook
    wrote on last edited by
    #1

    i have a text file which contains data as fallows
    Screenshot from 2021-09-27 11-19-49.png

    1st column is message name, 2nd is message id and 3rd is parent id

    i want to create a tree using this data i want to make the messages with parent id -1 as roots
    and the other messages should become the chld based on their parent id (eg: message1_1 is a child of message1 since its parent id is 1)

    i am very new to Qt and i really want some solution and guidelines on how to achieve my task (this may be very fundamental since i am new i am finding it difficult please help)

    here is the code how i am trying to do it i am abe to create roots not sure how to create child item

    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/textfiles/data.txt");
    
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "file not opened";
    
    }
    
     QTextStream stream(&file);
     QString line;
    
     while (stream.readLineInto(&line))
     {
         QStringList list = line.split(QLatin1Char('\t'), QString::SkipEmptyParts);
         qDebug()<<list;
    
         if (list.at(2) == "-1")
         {
             root = new QTreeWidgetItem(ui->treeWidget);
             root->setText(0,list.at(0));
         }
         else
         {
    
    
         }
     }
    
     file.close();
    

    }

    if this is not the right way i am open for suggestion
    thanks in advance.

    eyllanescE 1 Reply Last reply
    0
    • K kook

      i have a text file which contains data as fallows
      Screenshot from 2021-09-27 11-19-49.png

      1st column is message name, 2nd is message id and 3rd is parent id

      i want to create a tree using this data i want to make the messages with parent id -1 as roots
      and the other messages should become the chld based on their parent id (eg: message1_1 is a child of message1 since its parent id is 1)

      i am very new to Qt and i really want some solution and guidelines on how to achieve my task (this may be very fundamental since i am new i am finding it difficult please help)

      here is the code how i am trying to do it i am abe to create roots not sure how to create child item

      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/textfiles/data.txt");
      
      if (!file.open(QIODevice::ReadOnly))
      {
          qDebug() << "file not opened";
      
      }
      
       QTextStream stream(&file);
       QString line;
      
       while (stream.readLineInto(&line))
       {
           QStringList list = line.split(QLatin1Char('\t'), QString::SkipEmptyParts);
           qDebug()<<list;
      
           if (list.at(2) == "-1")
           {
               root = new QTreeWidgetItem(ui->treeWidget);
               root->setText(0,list.at(0));
           }
           else
           {
      
      
           }
       }
      
       file.close();
      

      }

      if this is not the right way i am open for suggestion
      thanks in advance.

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

      @kook You just have to map the items:

      #include <QApplication>
      #include <QFile>
      #include <QTreeWidget>
      
      #include <QDebug>
      
      bool buildTree(const QString & filename, QTreeWidget *treeWidget){
          QFile file(filename);
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
              qWarning() << file.error() << file.errorString();
              return false;
          }
          QMap<int, QTreeWidgetItem*> items;
          QTextStream stream(&file);
          QString line;
          while (stream.readLineInto(&line)){
              QStringList words = line.split(QLatin1Char('\t'), Qt::SkipEmptyParts);
              if(words.length() != 3){
                  qWarning() << "content" << words << ", lenght:" << words.length();
                  continue;
              }
              QTreeWidgetItem *item = nullptr;
              QString text = words.at(0);
              bool ok;
              int id = words.at(1).toInt(&ok);
              if(!ok){
                  qDebug() << "id not is number";
                  continue;
              }
              int parent_id = words.at(2).toInt(&ok);
              if(!ok){
                  qWarning() << "parentId not is number";
              }
              if(parent_id == -1){
                  item = new QTreeWidgetItem(treeWidget);
              }
              else if(items.contains(parent_id)){
                  item = new QTreeWidgetItem(items[parent_id]);
              }
              else{
                  qWarning() << "parentId: " << parent_id << "not found";
                  continue;
              }
              if(item){
                  item->setText(0, text);
                  items[id] = item;
              }
          }
      
          return true;
      }
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTreeWidget w;
      
          buildTree("/home/textfiles/data.txt", &w);
      
          w.expandAll();
          w.show();
          return a.exec();
      }
      

      Screenshot_20210927_013956.png

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

      K 2 Replies Last reply
      2
      • eyllanescE eyllanesc

        @kook You just have to map the items:

        #include <QApplication>
        #include <QFile>
        #include <QTreeWidget>
        
        #include <QDebug>
        
        bool buildTree(const QString & filename, QTreeWidget *treeWidget){
            QFile file(filename);
            if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                qWarning() << file.error() << file.errorString();
                return false;
            }
            QMap<int, QTreeWidgetItem*> items;
            QTextStream stream(&file);
            QString line;
            while (stream.readLineInto(&line)){
                QStringList words = line.split(QLatin1Char('\t'), Qt::SkipEmptyParts);
                if(words.length() != 3){
                    qWarning() << "content" << words << ", lenght:" << words.length();
                    continue;
                }
                QTreeWidgetItem *item = nullptr;
                QString text = words.at(0);
                bool ok;
                int id = words.at(1).toInt(&ok);
                if(!ok){
                    qDebug() << "id not is number";
                    continue;
                }
                int parent_id = words.at(2).toInt(&ok);
                if(!ok){
                    qWarning() << "parentId not is number";
                }
                if(parent_id == -1){
                    item = new QTreeWidgetItem(treeWidget);
                }
                else if(items.contains(parent_id)){
                    item = new QTreeWidgetItem(items[parent_id]);
                }
                else{
                    qWarning() << "parentId: " << parent_id << "not found";
                    continue;
                }
                if(item){
                    item->setText(0, text);
                    items[id] = item;
                }
            }
        
            return true;
        }
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            QTreeWidget w;
        
            buildTree("/home/textfiles/data.txt", &w);
        
            w.expandAll();
            w.show();
            return a.exec();
        }
        

        Screenshot_20210927_013956.png

        K Offline
        K Offline
        kook
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • eyllanescE eyllanesc

          @kook You just have to map the items:

          #include <QApplication>
          #include <QFile>
          #include <QTreeWidget>
          
          #include <QDebug>
          
          bool buildTree(const QString & filename, QTreeWidget *treeWidget){
              QFile file(filename);
              if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                  qWarning() << file.error() << file.errorString();
                  return false;
              }
              QMap<int, QTreeWidgetItem*> items;
              QTextStream stream(&file);
              QString line;
              while (stream.readLineInto(&line)){
                  QStringList words = line.split(QLatin1Char('\t'), Qt::SkipEmptyParts);
                  if(words.length() != 3){
                      qWarning() << "content" << words << ", lenght:" << words.length();
                      continue;
                  }
                  QTreeWidgetItem *item = nullptr;
                  QString text = words.at(0);
                  bool ok;
                  int id = words.at(1).toInt(&ok);
                  if(!ok){
                      qDebug() << "id not is number";
                      continue;
                  }
                  int parent_id = words.at(2).toInt(&ok);
                  if(!ok){
                      qWarning() << "parentId not is number";
                  }
                  if(parent_id == -1){
                      item = new QTreeWidgetItem(treeWidget);
                  }
                  else if(items.contains(parent_id)){
                      item = new QTreeWidgetItem(items[parent_id]);
                  }
                  else{
                      qWarning() << "parentId: " << parent_id << "not found";
                      continue;
                  }
                  if(item){
                      item->setText(0, text);
                      items[id] = item;
                  }
              }
          
              return true;
          }
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QTreeWidget w;
          
              buildTree("/home/textfiles/data.txt", &w);
          
              w.expandAll();
              w.show();
              return a.exec();
          }
          

          Screenshot_20210927_013956.png

          K Offline
          K Offline
          kook
          wrote on last edited by
          #4

          @eyllanesc thank you for the solution

          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