How to save a treeview after editing it in a .txt file in Qt
-
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!
-
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!
-
@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?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.
-
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.
@Minikornio
So that way to tackle that is to putqDebug()
statements into your code (or use a debugger). Put them in your loops, check all yourQModelIndex
es 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.... -
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.
@Minikornio First thing to do in such case: use debugger and execute step-by-step to see where it crashes...
-
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:
.
.
.
.
ROWCOUNTint 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 COUNTint TreeModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return static_cast<TreeItem*>(parent.internalPointer())->columnCount(); else return m_rootSetting->columnCount(); }
.
.
PARENTQModelIndex 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); }
.
.
INDEXQModelIndex 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(); }
.
.
DATAQVariant 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()); }
-
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:
.
.
.
.
ROWCOUNTint 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 COUNTint TreeModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return static_cast<TreeItem*>(parent.internalPointer())->columnCount(); else return m_rootSetting->columnCount(); }
.
.
PARENTQModelIndex 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); }
.
.
INDEXQModelIndex 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(); }
.
.
DATAQVariant 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()); }
@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.