How to add data to QTableView?
-
I created a table and I want to add data to this table but I couldn't figure out how to do it. Can someone help me? I will be grateful.
Here is my code
void MainWindow::on_pushButton_clicked() { QTableView *view = new QTableView; QStandardItemModel *mod = new QStandardItemModel; QStandardItem *it = new QStandardItem(QObject::tr("ID")); mod->setHorizontalHeaderItem(0,it); QStandardItem *it1 = new QStandardItem(QObject::tr("Name")); mod->setHorizontalHeaderItem(1,it1); QStandardItem *it2 = new QStandardItem(QObject::tr("City")); mod->setHorizontalHeaderItem(2,it2); QStandardItem *it3 = new QStandardItem(QObject::tr("Country")); mod->setHorizontalHeaderItem(3,it3); view->setModel(mod); view->show(); } -
You don't add data to a view but to a model and since you're using a QStandardItemModel you can look into it's documentation here: https://doc.qt.io/qt-6/qstandarditemmodel.html#details
You should also read this: https://doc.qt.io/qt-6/model-view-programming.html -
You don't add data to a view but to a model and since you're using a QStandardItemModel you can look into it's documentation here: https://doc.qt.io/qt-6/qstandarditemmodel.html#details
You should also read this: https://doc.qt.io/qt-6/model-view-programming.html@Christian-Ehrlicher I store my data in QStringList. I want to show them in a table. What would you recommend for this?
-
Hi,
Loop on your list and create the items as needed.
Note that Qt has also QStringListModel but it's usually used with a QListView.