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. How to save a treeview after editing it in a .txt file in Qt
Forum Updated to NodeBB v4.3 + New Features

How to save a treeview after editing it in a .txt file in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 958 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.
  • M Offline
    M Offline
    Minikornio
    wrote on last edited by Minikornio
    #1

    Re: How to save a treeview as .csv file in Qt?

    EDIT:
    I am trying to understand the @fbengo example, but I am not able to run it and I do not understand it.

    Anyone could advice me to use any useful function, or give me an example? Because I am not sure how to iterate into it, or how to send the data to the text file . Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • M Minikornio

      Re: How to save a treeview as .csv file in Qt?

      EDIT:
      I am trying to understand the @fbengo example, but I am not able to run it and I do not understand it.

      Anyone could advice me to use any useful function, or give me an example? Because I am not sure how to iterate into it, or how to send the data to the text file . Thanks in advance!

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

      @Minikornio
      Since that was 3 years ago and @fbengo "no longer exists", just explain what it is that does not work in the code you have written now?

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @Minikornio
        Since that was 3 years ago and @fbengo "no longer exists", just explain what it is that does not work in the code you have written now?

        M Offline
        M Offline
        Minikornio
        wrote on last edited by Minikornio
        #3

        @JonB

        When I run his example, and then push the save button, the program crashes. The .txt file was created, but it's empty.

        I'm still trying to understand why, but would be great to have a little of help.

        JonBJ jsulmJ 2 Replies Last reply
        0
        • M Minikornio

          @JonB

          When I run his example, and then push the save button, the program crashes. The .txt file was created, but it's empty.

          I'm still trying to understand why, but would be great to have a little of help.

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

          @Minikornio
          So that way to tackle that is to put qDebug() statements into your code (or use a debugger). Put them in your loops, check all your QModelIndexes for validity, etc., to see just which line it reaches just before it crashes.

           QModelIndex columnIndex = index.sibling(index.row(), c);
           stream << index.data().toString();
          

          What's the point of assigning columnIndex here if you're not going to use it? index does not change in this loop so whatever it outputs won't change during the loop, doesn't look right. Not saying that's your crash, but needs looking at....

          1 Reply Last reply
          2
          • M Minikornio

            @JonB

            When I run his example, and then push the save button, the program crashes. The .txt file was created, but it's empty.

            I'm still trying to understand why, but would be great to have a little of help.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Minikornio First thing to do in such case: use debugger and execute step-by-step to see where it crashes...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • M Offline
              M Offline
              Minikornio
              wrote on last edited by Minikornio
              #6

              Sorry guys, I am very beginner in these kind of things, and I am just trying to use codes that I find to see if they work, and then trying to understand them if they do what I need.

              https://stackoverflow.com/questions/34992581/how-to-save-a-treeview-as-csv-file-in-qt

              void MainWindow::on_pushButton_clicked()
              {
                  QString filename="aatest1.txt";
                  QFile file( filename );
                  if (file.open(QFile::WriteOnly))
                  {
                      QTextStream stream(&file);
                      for (int i = 0; i < TreeModel::rowCount(); i++)
                          {
                              for (int j = 0; j < TreeModel::columnCount(); j++)
                              {
                                  QModelIndex index = TreeModel::index(i, j);
                                  stream << TreeModel::data(index).toString();
                              }
                          }
                      file.close();
                  }
              }
              

              This one is a little more understandable for me, but it throw me error saying ''call to non-static member function without an object argument"

              I can't convert those functions in static ones in .cpp and .h because it throws me other errors (as far as I know)

              The functions that I'm calling there are the following:
              .
              .
              .
              .
              ROWCOUNT

              int TreeModel::rowCount(const QModelIndex &parent) const
              {
                  TreeItem *parentItem;
                  if (parent.column() > 0)
                      return 0;
              
                  if (!parent.isValid())
                      parentItem = m_rootSetting.get();
                  else
                      parentItem = static_cast<TreeItem*>(parent.internalPointer());
              
                  return parentItem->childCount();
              }
              

              .
              .
              COLUMN COUNT

              int TreeModel::columnCount(const QModelIndex &parent) const
              {
                  if (parent.isValid())
                      return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
                  else
                      return m_rootSetting->columnCount();
              }
              

              .
              .
              PARENT

              QModelIndex TreeModel::parent(const QModelIndex &index) const
              {
                  if (!index.isValid())
                      return QModelIndex();
              
                  TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
                  TreeItem *parentItem = childItem->parentItem();
              
                  if (parentItem == m_rootSetting.get())
                      return QModelIndex();
              
                  return createIndex(parentItem->row(), 0, parentItem);
              }
              

              .
              .
              INDEX

              QModelIndex TreeModel::index(int row, int column,
                                           const QModelIndex &parent) const
              {
                  if (!hasIndex(row, column, parent))
                      return QModelIndex();
              
                  TreeItem *parentItem;
              
                  if (!parent.isValid())
                      parentItem = m_rootSetting.get();
                  else
                      parentItem = static_cast<TreeItem*>(parent.internalPointer());
              
                  TreeItem *childItem = parentItem->child(row);
                  if (childItem)
                      return createIndex(row, column, childItem);
                  else
                      return QModelIndex();
              }
              

              .
              .
              DATA

              QVariant TreeModel::data(const QModelIndex &index, int role) const
              {
                  if (!index.isValid())
                      return QVariant();
              
                  if (role != Qt::DisplayRole)
                      return QVariant();
              
                  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
              
                  return item->data(index.column());
              }
              
              JonBJ 1 Reply Last reply
              0
              • M Minikornio

                Sorry guys, I am very beginner in these kind of things, and I am just trying to use codes that I find to see if they work, and then trying to understand them if they do what I need.

                https://stackoverflow.com/questions/34992581/how-to-save-a-treeview-as-csv-file-in-qt

                void MainWindow::on_pushButton_clicked()
                {
                    QString filename="aatest1.txt";
                    QFile file( filename );
                    if (file.open(QFile::WriteOnly))
                    {
                        QTextStream stream(&file);
                        for (int i = 0; i < TreeModel::rowCount(); i++)
                            {
                                for (int j = 0; j < TreeModel::columnCount(); j++)
                                {
                                    QModelIndex index = TreeModel::index(i, j);
                                    stream << TreeModel::data(index).toString();
                                }
                            }
                        file.close();
                    }
                }
                

                This one is a little more understandable for me, but it throw me error saying ''call to non-static member function without an object argument"

                I can't convert those functions in static ones in .cpp and .h because it throws me other errors (as far as I know)

                The functions that I'm calling there are the following:
                .
                .
                .
                .
                ROWCOUNT

                int TreeModel::rowCount(const QModelIndex &parent) const
                {
                    TreeItem *parentItem;
                    if (parent.column() > 0)
                        return 0;
                
                    if (!parent.isValid())
                        parentItem = m_rootSetting.get();
                    else
                        parentItem = static_cast<TreeItem*>(parent.internalPointer());
                
                    return parentItem->childCount();
                }
                

                .
                .
                COLUMN COUNT

                int TreeModel::columnCount(const QModelIndex &parent) const
                {
                    if (parent.isValid())
                        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
                    else
                        return m_rootSetting->columnCount();
                }
                

                .
                .
                PARENT

                QModelIndex TreeModel::parent(const QModelIndex &index) const
                {
                    if (!index.isValid())
                        return QModelIndex();
                
                    TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
                    TreeItem *parentItem = childItem->parentItem();
                
                    if (parentItem == m_rootSetting.get())
                        return QModelIndex();
                
                    return createIndex(parentItem->row(), 0, parentItem);
                }
                

                .
                .
                INDEX

                QModelIndex TreeModel::index(int row, int column,
                                             const QModelIndex &parent) const
                {
                    if (!hasIndex(row, column, parent))
                        return QModelIndex();
                
                    TreeItem *parentItem;
                
                    if (!parent.isValid())
                        parentItem = m_rootSetting.get();
                    else
                        parentItem = static_cast<TreeItem*>(parent.internalPointer());
                
                    TreeItem *childItem = parentItem->child(row);
                    if (childItem)
                        return createIndex(row, column, childItem);
                    else
                        return QModelIndex();
                }
                

                .
                .
                DATA

                QVariant TreeModel::data(const QModelIndex &index, int role) const
                {
                    if (!index.isValid())
                        return QVariant();
                
                    if (role != Qt::DisplayRole)
                        return QVariant();
                
                    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
                
                    return item->data(index.column());
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Minikornio said in How to save a treeview after editing it in a .txt file in Qt:

                but it throw me error saying ''call to non-static member function without an object argument"

                When the compiler gives you such an error, does it not state which line number it occurs on? You need that to locate the error.

                At a guess: You seem to have changed all of your code to use TreeModel::rowCount() etc. TreeModel is a class/type, hence the warnings. You need an instance to call those methods.

                Even if you are a beginner, you need to understand the complete difference between classes/types vs instances before you will get very far.

                1 Reply Last reply
                1

                • Login

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