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. Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?
Forum Updated to NodeBB v4.3 + New Features

Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 11 Aug 2022, 10:06 last edited by mrjj 8 Nov 2022, 10:07
    #3

    Hi
    A Table WIDGET is just a TableView with an embedded model.

    A TableView + a standardModel is very close to a TableWidget.

    Using Views are recommended. The Widgets versions are meant as a convenience for
    some use cases.

    (meh jonB types faster :)

    1 Reply Last reply
    2
    • I Offline
      I Offline
      imene
      wrote on 11 Aug 2022, 10:20 last edited by
      #4

      @JonB , @mrjj ;
      I've followed this tuto https://www.youtube.com/watch?v=NsjDQCvlNaY he worked with QTableView + QStandardItemModel what should i change to work with QTableWidget.
      What is the purpose of QStandardItemModel ? should i remove it to work with QTableWidget. ?
      @JonB Where should i store data? in this case "QTableWidget has its own internal model, if you used that you would store your data there."

      J M 2 Replies Last reply 11 Aug 2022, 10:21
      0
      • I imene
        11 Aug 2022, 10:20

        @JonB , @mrjj ;
        I've followed this tuto https://www.youtube.com/watch?v=NsjDQCvlNaY he worked with QTableView + QStandardItemModel what should i change to work with QTableWidget.
        What is the purpose of QStandardItemModel ? should i remove it to work with QTableWidget. ?
        @JonB Where should i store data? in this case "QTableWidget has its own internal model, if you used that you would store your data there."

        J Offline
        J Offline
        JonB
        wrote on 11 Aug 2022, 10:21 last edited by
        #5

        @imene
        You keep asking about changing from (apparently working) QTableView + QStandardItemModel to QTableWidget. Don't bother, no advantage.

        1 Reply Last reply
        1
        • I imene
          11 Aug 2022, 10:20

          @JonB , @mrjj ;
          I've followed this tuto https://www.youtube.com/watch?v=NsjDQCvlNaY he worked with QTableView + QStandardItemModel what should i change to work with QTableWidget.
          What is the purpose of QStandardItemModel ? should i remove it to work with QTableWidget. ?
          @JonB Where should i store data? in this case "QTableWidget has its own internal model, if you used that you would store your data there."

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 11 Aug 2022, 10:55 last edited by
          #6

          Hi

          What is the purpose of QStandardItemModel ? should i remove it to work with QTableWidget. ?

          TableWidget has its own items
          https://doc.qt.io/qt-6/qtablewidgetitem.html

          Where as the views can use QStandardItemModel or a custom model.

          To swap from QTableView + QStandardItemModel to TableWidget, you would simply
          new and insert QTableWidgetItem instead of QStandardItemModelItems.

          But no reason to swap to that if you already are using the View as no gains to do so.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imene
            wrote on 11 Aug 2022, 11:02 last edited by
            #7

            Okay i'm gonna continue with QTableView + QStandardItemModel, but how can i select a row from the table and save it in an other csv file ?

            M 1 Reply Last reply 11 Aug 2022, 11:24
            0
            • I imene
              11 Aug 2022, 11:02

              Okay i'm gonna continue with QTableView + QStandardItemModel, but how can i select a row from the table and save it in an other csv file ?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 11 Aug 2022, 11:24 last edited by
              #8

              @imene
              Hi

              QModelIndexList selection = yourTableView->selectionModel()->selectedRows();
              
              // Multiple rows can be selected
              for(int i=0; i< selection.count(); i++)
              {
                  QModelIndex index = selection.at(i);
                // use index to look up data in the model 
              }
              

              https://doc.qt.io/qt-6/qstandarditemmodel.html#itemFromIndex

              1 Reply Last reply
              2
              • I Offline
                I Offline
                imene
                wrote on 11 Aug 2022, 15:07 last edited by
                #9

                @mrjj Great!
                In this case how can i add or remove a row from TableView ?

                J 1 Reply Last reply 11 Aug 2022, 15:36
                0
                • I imene
                  11 Aug 2022, 15:07

                  @mrjj Great!
                  In this case how can i add or remove a row from TableView ?

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Aug 2022, 15:36 last edited by JonB 8 Nov 2022, 15:41
                  #10

                  @imene
                  Qt treats as the view just for viewing the model. Updates are done on the model, so go look in QStandardItemModel for suitable methods. There you find appendRow() or insertRow() etc. Use these to alter your model, then the view will reflect your updates automatically. https://stackoverflow.com/questions/16961560/qstandarditemmodel-delete-a-row seems to show these being used.

                  1 Reply Last reply
                  2
                  • I Offline
                    I Offline
                    imene
                    wrote on 15 Aug 2022, 11:35 last edited by
                    #11

                    Thanks @JonB;
                    I did it this way:

                    void MainWindow::on_Add_Row_Button_clicked()
                    {
                                 mModel->insertRow(mModel->rowCount());
                    }
                    
                    

                    Note :This one adds new ligne in the end of the table, how can i add in a specific line?

                    void MainWindow::on_Delete_Row_Button_clicked()
                    {
                        
                       
                            while (!indexes.isEmpty())
                            {
                                mModel->removeRows(indexes.last().row(), 1);
                                indexes.removeLast();
                            }
                    }
                    

                    Note: This one removes selected rows.

                    An other question : Can i save qtableview content in a specific file qtcreator every time i change a row content?

                    J 1 Reply Last reply 15 Aug 2022, 11:41
                    0
                    • I imene
                      15 Aug 2022, 11:35

                      Thanks @JonB;
                      I did it this way:

                      void MainWindow::on_Add_Row_Button_clicked()
                      {
                                   mModel->insertRow(mModel->rowCount());
                      }
                      
                      

                      Note :This one adds new ligne in the end of the table, how can i add in a specific line?

                      void MainWindow::on_Delete_Row_Button_clicked()
                      {
                          
                         
                              while (!indexes.isEmpty())
                              {
                                  mModel->removeRows(indexes.last().row(), 1);
                                  indexes.removeLast();
                              }
                      }
                      

                      Note: This one removes selected rows.

                      An other question : Can i save qtableview content in a specific file qtcreator every time i change a row content?

                      J Offline
                      J Offline
                      JonB
                      wrote on 15 Aug 2022, 11:41 last edited by
                      #12

                      @imene said in Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?:

                      Note :This one adds new ligne in the end of the table, how can i add in a specific line?

                      Please look at the parameter to insertRow()!

                      Can i save qtableview content in a specific file qtcreator every time i change a row content?

                      Don't know what this means. In Creator, i.e. UI Designer? In any case, nothing to do with a QTableView, if you want to save changed model content you can do so whenever you want.

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        imene
                        wrote on 15 Aug 2022, 12:52 last edited by
                        #13

                        The idea is that i want to detect the changes in the table view to save it dynamicly (every time that we edit a row ) in the current file just oppened:
                        This is the opening files button:

                        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;
                            }
                        
                            //QString Data = m_ui->Table_CSV->model()->data(m_ui->Table_CSV->model()->index(0,2)).toString();
                            //qDebug()<< m_ui->Table_CSV->indexAt(QPoint(2,2)).row() << " and " <<  m_ui->Table_CSV->indexAt(QPoint(2,2)).column();
                            //qDebug()<<"Data="<<Data;
                            file.close();
                        }
                        
                        

                        This is saving button:

                        void MainWindow::on_SaveReport_Button_clicked()
                        {
                            auto filename = QFileDialog::getSaveFileName(this, "Save Report", 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();
                        }
                        
                        
                        void MainWindow::setValueAt(int ix, int jx, const QString &value)
                        {
                            if(!mModel->item(ix, jx))
                            {
                                mModel->setItem(ix, jx, new QStandardItem(value));
                            }
                            else
                            {
                                mModel->item(ix, jx)->setText(value);
                            }
                        }
                        QString MainWindow::getValueAt(int ix, int jx)
                        {
                            if(!mModel->item(ix, jx))
                            {
                                return "";
                            }
                            return mModel->item(ix, jx)->text();
                        }
                        
                        
                        J 1 Reply Last reply 15 Aug 2022, 12:59
                        0
                        • I imene
                          15 Aug 2022, 12:52

                          The idea is that i want to detect the changes in the table view to save it dynamicly (every time that we edit a row ) in the current file just oppened:
                          This is the opening files button:

                          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;
                              }
                          
                              //QString Data = m_ui->Table_CSV->model()->data(m_ui->Table_CSV->model()->index(0,2)).toString();
                              //qDebug()<< m_ui->Table_CSV->indexAt(QPoint(2,2)).row() << " and " <<  m_ui->Table_CSV->indexAt(QPoint(2,2)).column();
                              //qDebug()<<"Data="<<Data;
                              file.close();
                          }
                          
                          

                          This is saving button:

                          void MainWindow::on_SaveReport_Button_clicked()
                          {
                              auto filename = QFileDialog::getSaveFileName(this, "Save Report", 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();
                          }
                          
                          
                          void MainWindow::setValueAt(int ix, int jx, const QString &value)
                          {
                              if(!mModel->item(ix, jx))
                              {
                                  mModel->setItem(ix, jx, new QStandardItem(value));
                              }
                              else
                              {
                                  mModel->item(ix, jx)->setText(value);
                              }
                          }
                          QString MainWindow::getValueAt(int ix, int jx)
                          {
                              if(!mModel->item(ix, jx))
                              {
                                  return "";
                              }
                              return mModel->item(ix, jx)->text();
                          }
                          
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 15 Aug 2022, 12:59 last edited by JonB
                          #14

                          @imene said in Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?:

                          The idea is that i want to detect the changes in the table view to save it dynamicly (every time that we edit a row ) i

                          And what is the question? Again, the view does not report or implement changes. It is always the model. And for that you have a variety of signals, including dataChanged() and rowsInserted/Removed().

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            imene
                            wrote on 15 Aug 2022, 13:19 last edited by imene
                            #15

                            @JonB said in Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?:

                            This is the question How to detect when item is changed in a table view?
                            I find this code it works for me each time i've made a change in an item it retuens in terminal "the cell has changed":

                            MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                m_ui(new Ui::MainWindow),
                                m_busStatusTimer(new QTimer(this))
                            {
                                m_ui->setupUi(this);
                            
                                m_connectDialog = new ConnectDialog;
                            
                                
                                connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged()));
                            
                            
                            
                            
                            }
                            
                            void MainWindow::onDataChanged()
                            {
                                qDebug() << "the cell has changed";
                            }
                            

                            Code source : https://www.qtcentre.org/threads/49032-Detect-when-the-content-of-a-cell-of-QTableView-is-changed

                            J 1 Reply Last reply 15 Aug 2022, 16:33
                            0
                            • I imene
                              15 Aug 2022, 13:19

                              @JonB said in Importing and showing csv file content into QTable widget doesn't work for me ! But with QTable view works what is the diffrence between them ?:

                              This is the question How to detect when item is changed in a table view?
                              I find this code it works for me each time i've made a change in an item it retuens in terminal "the cell has changed":

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  m_ui(new Ui::MainWindow),
                                  m_busStatusTimer(new QTimer(this))
                              {
                                  m_ui->setupUi(this);
                              
                                  m_connectDialog = new ConnectDialog;
                              
                                  
                                  connect(m_ui->Table_CSV->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged()));
                              
                              
                              
                              
                              }
                              
                              void MainWindow::onDataChanged()
                              {
                                  qDebug() << "the cell has changed";
                              }
                              

                              Code source : https://www.qtcentre.org/threads/49032-Detect-when-the-content-of-a-cell-of-QTableView-is-changed

                              J Offline
                              J Offline
                              JonB
                              wrote on 15 Aug 2022, 16:33 last edited by
                              #16

                              @imene
                              Indeed. Which is why I said the model would emit dataChanged() signal, and that's what you should listen for! Anyway, so what you have should be good for you.

                              1 Reply Last reply
                              0

                              12/16

                              15 Aug 2022, 11:41

                              • Login

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