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 ?
-
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);) -
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);)@imene
Either one, it's up to you. If you use your ownQStandardItemModel
you will useQTableView
.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 fromQTableView
and is just as able to store/show your data. But if you have it working withQTableView
+QStandardItemModel
you might as well continue with that. -
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 :)
-
@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." -
@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." -
@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."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.htmlWhere 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.
-
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 ?
@imene
HiQModelIndexList 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
-
@imene
Qt treats as the view just for viewing the model. Updates are done on the model, so go look inQStandardItemModel
for suitable methods. There you findappendRow()
orinsertRow()
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. -
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?
-
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?
@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. -
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(); }
-
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(); }
@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()
androwsInserted
/Removed()
. -
@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
-
@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