QListView does not work with QStandardItemModel
-
Hi all
I tried to use all the manuals and they say that I should use a QStandardItemModel to deal with a QListView.
The QStandardItemModel is filled in main.cpp:QStandardItemModel globalEtiketten; int main(int argc, char *argv[]){ std::string pfad = QDir::currentPath().toStdString() + "/schubagDOCupload.db"; sqlite3 *database; sqlite3_open(pfad.c_str(), &database); sqlite3_stmt *statement1; sqlset = "SELECT * FROM etiketten"; stcheck1 = sqlite3_prepare_v2(database, sqlset.c_str(), -1, &statement1, nullptr); if(stcheck1 == SQLITE_OK){ globalEtiketten.setHorizontalHeaderLabels(QStringList() << "Projekt" << "Hersteller" << "Seriennummer" << "Kommentar"); while (sqlite3_step(statement1) == SQLITE_ROW){ QList<QStandardItem*> newRow; QStandardItem* proj = new QStandardItem(QString("%1").arg(QString(reinterpret_cast<const char*>(sqlite3_column_text(statement1, 0))))); QStandardItem* herst = new QStandardItem(QString("%2").arg(QString(reinterpret_cast<const char*>(sqlite3_column_text(statement1, 1))))); QStandardItem* serial = new QStandardItem(QString("%3").arg(QString(reinterpret_cast<const char*>(sqlite3_column_text(statement1, 2))))); QStandardItem* komm = new QStandardItem(QString("%4").arg(QString(reinterpret_cast<const char*>("Kommentar")))); newRow.append(proj); newRow.append(herst); newRow.append(serial); newRow.append(komm); globalEtiketten.appendRow(newRow); } } sqlite3_finalize(statement1); sqlite3_close(database);But when I want to set the model, I get the following error:
ui->etikettenView->setModel(globalEtiketten);mainwindow.cpp:31:33: No viable conversion from 'QStandardItemModel' to 'QAbstractItemModel *' qtableview.h:68:39: passing argument to parameter 'model' hereWhat can I do?
-
@Thomas-63 hi!
Look at definition:
virtual void setModel(QAbstractItemModel *model)You have a normal value not a pointer:
QStandardItemModel globalEtiketten;
Please try use:
ui->etikettenView->setModel(&globalEtiketten); -
T Thomas 63 has marked this topic as solved on
-
Hi,
I would like to point out some things to avoid issues in the future.
Static QObject objects like you are using should be avoided as the internals of Qt are not initialised until the QApplication object is created.
Furthermore, global static like that should also be avoided as they create hidden state that gets hard to reason about.
Finally, you should leverage Qt's SQL module. This would simplify your database handling as SQLite support is built in. You can then also take advantage of the QSqlQueryModel which would simplify your code one step further.