Solved Text from database to QStandardItemModel
-
Hi
Its not really clear what you are trying but if you are trying to ADD
new items, you should new EACH of them.QStandardItemModel model(4, 4); for (int row = 0; row < 4; ++row) { for (int column = 0; column < 4; ++column) { // Create new Item QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column)); // add it model.setItem(row, column, item); } }
Also if from database, why not just take it directly using QSqlQueryModel:
QSqlQueryModel* model = new QSqlQueryModel();
model->setQuery("select firstname from person"); // display all firstnames -
Hi @mrjj,
I tried the second solution first. When I usedQSqlTableModel *model = new QSqlTableModel(); model->setQuery ("SELECT Name FROM Item" );
I got the following error message:
C:\Programming\Projects\Folkfriends\mainwindow.cpp:42: error: no matching function for call to 'QSqlTableModel::setQuery(const char [22])'
model->setQuery ("SELECT Name FROM Item" );What did I miss?
-
@gabor53 said:
Hmm, even ?
QString q;
q="SELECT Name FROM Item";
model->setQuery (q); -
@mrjj
It gives the same error message for the; model->setQuery (q); ```line....
-
@gabor53
hmm.
Do you have
QT += sql
in the .pro file? -
-
@gabor53
and
#include <QSqlQueryModel>
? -
I have that one too.
-
@gabor53
ok. no idea then.
http://doc.qt.io/qt-5/qsqlquerymodel.htmlWhat version of Qt do you have ? did you compile yourself?
-
I have 5.6 and Yes I compiled myself.
-
Hi,
That's because you are using a QSqlTableModel, you should use a QSqlQueryModel.
QSqlTableModel goal is to make a table accessible.
-
QSqlTableModel
Good catch :)
-
@mrjj
The following worked:model = new QSqlTableModel(this); model->setTable ("Items"); model->select (); ui->tableView->setModel (model);
Thank you for all your help!