How to get the current file address just opened in a tableView ?
-
@imene said in How to get the current file address just opened in a tableView ?:
How to get the current file address just opened in a tableView ?
Since you can't open a file with qtableview - what's your question?
-
@imene said in How to get the current file address just opened in a tableView ?:
How to get the current file address just opened in a tableView ?
Since you can't open a file with qtableview - what's your question?
@Christian-Ehrlicher can i open a file with QTableWidget, did i have to use QTableWidget ?
@mchinand in case that i import the content of a TableView from .csv file, i didn't want to lose the content when i close the app is that possible (i want it to save changes made in the tableview in the current file) ?
This is my code:void MainWindow::on_Open_Button_clicked() { auto filename = QFileDialog::getOpenFileName(this, "Open", QDir::rootPath(), "CSV File(*.csv)"); if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } QTextStream xin(&file); int ix = 0; while(!xin.atEnd()) { mModel->setRowCount(ix); // qDebug()<<"mModel->setRowCount(ix) "<<ix; auto line = xin.readLine(); qDebug()<<"auto line = xin.readLine(); "<<line; auto values = line.split(","); qDebug()<<"auto values = line.split(""); "<<values ; const int colCount = values.size(); qDebug()<<"const int colCount = values.size(); "<<colCount ; mModel->setColumnCount(colCount); qDebug()<<"mModel->setColumnCount(colCount); "<<colCount ; for(int jx=0; jx<colCount; ++jx) { setValueAt(ix, jx, values.at(jx)); qDebug()<<"setValueAt(ix, jx, values.at(jx)); "<< values.at(jx) ; } ++ix; } file.close(); connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(on_Save_FUNC(filename))); } void MainWindow::on_Save_FUNC(QString filename) { if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); } void MainWindow::on_Save_Button_clicked() { auto filename = QFileDialog::getSaveFileName(this, "Save", QDir::rootPath(), "CSV File(*.csv)"); if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); }
-
@Christian-Ehrlicher can i open a file with QTableWidget, did i have to use QTableWidget ?
@mchinand in case that i import the content of a TableView from .csv file, i didn't want to lose the content when i close the app is that possible (i want it to save changes made in the tableview in the current file) ?
This is my code:void MainWindow::on_Open_Button_clicked() { auto filename = QFileDialog::getOpenFileName(this, "Open", QDir::rootPath(), "CSV File(*.csv)"); if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } QTextStream xin(&file); int ix = 0; while(!xin.atEnd()) { mModel->setRowCount(ix); // qDebug()<<"mModel->setRowCount(ix) "<<ix; auto line = xin.readLine(); qDebug()<<"auto line = xin.readLine(); "<<line; auto values = line.split(","); qDebug()<<"auto values = line.split(""); "<<values ; const int colCount = values.size(); qDebug()<<"const int colCount = values.size(); "<<colCount ; mModel->setColumnCount(colCount); qDebug()<<"mModel->setColumnCount(colCount); "<<colCount ; for(int jx=0; jx<colCount; ++jx) { setValueAt(ix, jx, values.at(jx)); qDebug()<<"setValueAt(ix, jx, values.at(jx)); "<< values.at(jx) ; } ++ix; } file.close(); connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(on_Save_FUNC(filename))); } void MainWindow::on_Save_FUNC(QString filename) { if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); } void MainWindow::on_Save_Button_clicked() { auto filename = QFileDialog::getSaveFileName(this, "Save", QDir::rootPath(), "CSV File(*.csv)"); if(filename.isEmpty()) { return; } QFile file(filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); }
@imene
The code you show is actually OK. You show reading a CSV file into a model (which you have created, but do not show). And you have code which will save that model back to a CSV file.It would not matter if you had chosen to use a
QTableWidget
instead of aQTableView
. Just that it would have provided its own model instead of you creating one, but otherwise similar.You show two similar functions,
on_Save_FUNC(QString filename)
andon_Save_Button_clicked()
. You don't want to duplicate the saving code, but that's a minor point.Meanwhile I guess this saves fine when the user clicks the Save button, and you ask for a filename.
You have
connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(on_Save_FUNC(filename)));
If this worked, it would mean you re-save the file after every editing change the user makes. But it won't work, because you cannot do
SLOT(on_Save_FUNC(filename))
. For that, to save back to the same file as you read, I suggest you put thefilename
of the file which was opened into a member variable so you can re-use that to save to.Further, if you do not want to save every time a change is made you might instead check just before the user quits the app (QCoreApplication::aboutToQuit()) whether there are any unsaved changes and save at that point if there are.
Does that answer your questions?
-
Thanks @JonB yess u did, i'm gonna try (QCoreApplication::aboutToQuit()) ;
What's about creating a global QString that get's File adresse+name each time i make or a New file or a just Opened file?
How could i do it ?
I've added File system path to this function each time i made changes in the tableView, it will be saved into it, is it a good idea i'm gonna used to put it's content in the current file just opened or the new one (The global QString that i want to make it "it get's File adresse+name each time i make or a New file or a just Opened file" )MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { m_ui->setupUi(this); m_connectDialog = new ConnectDialog; connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(on_Save_FUNC_System()));
void MainWindow::on_Save_FUNC_System() //statique save tabview in system file { QFile file("/home/pi/can_V3/FileSystem/TabSystem.csv"); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); }
-
Thanks @JonB yess u did, i'm gonna try (QCoreApplication::aboutToQuit()) ;
What's about creating a global QString that get's File adresse+name each time i make or a New file or a just Opened file?
How could i do it ?
I've added File system path to this function each time i made changes in the tableView, it will be saved into it, is it a good idea i'm gonna used to put it's content in the current file just opened or the new one (The global QString that i want to make it "it get's File adresse+name each time i make or a New file or a just Opened file" )MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { m_ui->setupUi(this); m_connectDialog = new ConnectDialog; connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(on_Save_FUNC_System()));
void MainWindow::on_Save_FUNC_System() //statique save tabview in system file { QFile file("/home/pi/can_V3/FileSystem/TabSystem.csv"); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } QTextStream xout(&file); const int rowCount = mModel->rowCount(); const int colCount = mModel->columnCount(); for(int ix=0; ix<rowCount; ++ix) { xout << getValueAt(ix, 0); for(int jx=1; jx < colCount; ++jx) { xout <<", "<< getValueAt(ix, jx); } xout <<"\n"; } file.flush(); file.close(); }
@imene said in How to get the current file address just opened in a tableView ?:
What's about creating a global QString that get's File adresse+name each time i make or a New file or a just Opened file?
Indeed!
You don't want that hard-coded path you have just put in.
Think about it: you (say you) want to save back to the same file that was opened. So you when opening the file you want to keep that name in a member variable so it is available in the save method. Now you know member variables because you already seem to have a
mModel
one. You need something like the following changes:private: QString filepathOpened; void MainWindow::on_Open_Button_clicked() { filepathOpened = QFileDialog::getOpenFileName(this, "Open", QDir::rootPath(), "CSV File(*.csv)"); ... } void MainWindow::on_Save_FUNC() { if(filepathOpened.isEmpty()) { return; } QFile file(filepathOpened); ... }
-
@JonB said in How to get the current file address just opened in a tableView ?:
QFile file(filepathOpened);
it doesn't work filepathOpened is not a global variable, did i have to work with it as a pointer ?
@imene
It does work. Do I have to tell you where to put:private: QString filepathOpened;
This is basic C++, and I already mentioned you seem to have a member variable
mModel
you are using so you really should know, you cannot use the language if you don't understand classes and member variables.... -
@imene
OK, better than I thought, well done, I take it back :)You named it
currentFilePath
there. You then say "filepathOpened is not a global variable,". I take it you got these names consistent?In which case, what exactly does "it doesn't work" mean? Member variable saves the file path the user opens in
on_Open_Button_clicked()
,on_Save_FUNC()
uses that as the path to save back to. What could possibly go wrong, you tell me? ;-) -
Sorry @JonB
This was my fault i put this under void MainWindow::on_Save_FUNC():QFile file("/home/pi/"+currentFilePath+".csv");
insted of this:
QFile file(currentFilePath);
it works now and it saves the tabelView content in the file i just opened thanks a lot.
-
Sorry @JonB
This was my fault i put this under void MainWindow::on_Save_FUNC():QFile file("/home/pi/"+currentFilePath+".csv");
insted of this:
QFile file(currentFilePath);
it works now and it saves the tabelView content in the file i just opened thanks a lot.