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 ?

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
  • 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 Offline
    I Offline
    imene
    wrote on last edited by
    #1

    Hi everyone;
    mModel = new QStandardItemModel(this);
    m_ui->Table_CSV->setModel(mModel);
    This code works with tableView, i'm working on this interface, So wish is best using QTable widget or QTable view ? to get the Id and Payload from table and send them into canFrame(m_canDevice->writeFrame(frameconnect);)

    53daaab6-83f1-45a3-b2bb-8d4e6d4d9609-image.png

    JonBJ 1 Reply Last reply
    0
    • I imene

      Hi everyone;
      mModel = new QStandardItemModel(this);
      m_ui->Table_CSV->setModel(mModel);
      This code works with tableView, i'm working on this interface, So wish is best using QTable widget or QTable view ? to get the Id and Payload from table and send them into canFrame(m_canDevice->writeFrame(frameconnect);)

      53daaab6-83f1-45a3-b2bb-8d4e6d4d9609-image.png

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

      @imene
      Either one, it's up to you. If you use your own QStandardItemModel you will use QTableView. QTableWidget has its own internal model, if you used that you would store your data there. It makes no difference, they can both be used for your "canFrame" stuff.

      I have no idea why your code was not good for QTableWidget, that is derived from QTableView and is just as able to store/show your data. But if you have it working with QTableView + QStandardItemModel you might as well continue with that.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #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 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."

          JonBJ mrjjM 2 Replies Last reply
          0
          • I imene

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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 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 ?

                mrjjM 1 Reply Last reply
                0
                • I imene

                  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 ?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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 last edited by
                    #9

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

                    JonBJ 1 Reply Last reply
                    0
                    • I imene

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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 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?

                        JonBJ 1 Reply Last reply
                        0
                        • I imene

                          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?

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

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

                                JonBJ 1 Reply Last reply
                                0
                                • I imene

                                  @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

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on 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

                                  • Login

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