Text from database to QStandardItemModel
-
Hi,
I'm trying to display string from a database into a tableView using StandardItemModel.Item->setData (NameD = query.value (0).toString (),Qt::DisplayRole); qDebug() << "Name(1) " << NameD; smodel->setItem (row,0,NameD);
The code generates a long error message:
C:\Programming\Projects\Folkfriends\mainwindow.cpp:54: error: no matching function for call to 'QStandardItemModel::setItem(int&, int, QString&)'
smodel->setItem (row,0,NameD);
^
C:\Qt\5.5\mingw492_32\include\QtGui\qstandarditemmodel.h:357: note: no known conversion for argument 3 from 'QString' to 'QStandardItem*'
C:\Qt\5.5\mingw492_32\include\QtGui\qstandarditemmodel.h:420: note: candidate expects 2 arguments, 3 providedHow can I use setItem with a QString (I think that causes the trouble) and howto avoid this error?
Thank you. -
look this:
Qt5.6.0
void QStandardItemModel::setItem(int row, int column, QStandardItem *item);
maybe you can use it like this:
setItem(row,0,&NameD); -
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
ok. no idea then.
http://doc.qt.io/qt-5/qsqlquerymodel.htmlWhat version of Qt do you have ? did you compile yourself?
-
Hi,
That's because you are using a QSqlTableModel, you should use a QSqlQueryModel.
QSqlTableModel goal is to make a table accessible.