Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the current file address just opened in a tableView ?
Qt 6.12 LTS is out! See what's new in the release blog

How to get the current file address just opened in a tableView ?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.8k 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.
  • I imene

    Hello;
    How to get the current file address just opened in a tableView ?

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

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

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    I 1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #3

      This depends on your code since a QTableView doesn't know anything about the file that the data it is showing came from (or if it even originated from a file); it displays the data of a model.

      1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

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

        I Offline
        I Offline
        imene
        wrote on last edited by imene
        #4

        @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();
        }
        
        
        JonBJ 1 Reply Last reply
        0
        • I imene

          @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();
          }
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @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 a QTableView. 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) and on_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 the filename 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?

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imene
            wrote on last edited by imene
            #6

            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();
            }
            
            JonBJ 1 Reply Last reply
            0
            • I imene

              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();
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @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);
                  ...
              }
              
              1 Reply Last reply
              0
              • I Offline
                I Offline
                imene
                wrote on last edited by
                #8

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

                JonBJ 1 Reply Last reply
                0
                • I imene

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

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

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

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    imene
                    wrote on last edited by
                    #10

                    @JonB in the mainwindow.h isn't it ?
                    184d56bf-7115-49e3-a20e-11577e10a317-image.png

                    JonBJ 1 Reply Last reply
                    0
                    • I imene

                      @JonB in the mainwindow.h isn't it ?
                      184d56bf-7115-49e3-a20e-11577e10a317-image.png

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

                      @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? ;-)

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        imene
                        wrote on last edited by imene
                        #12

                        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.

                        JonBJ 1 Reply Last reply
                        2
                        • I imene

                          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.

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

                          @imene
                          Good :) The important thing is to understand how we use that member variable to share the filepath picked for opening with that used for saving.

                          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
                          • Unsolved