Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView does not work with QStandardItemModel

QListView does not work with QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 396 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thomas 63
    wrote on last edited by
    #1

    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' here
    

    What can I do?

    1 Reply Last reply
    0
    • BondrusiekB Offline
      BondrusiekB Offline
      Bondrusiek
      wrote on last edited by
      #2

      @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);

      1 Reply Last reply
      1
      • T Thomas 63 has marked this topic as solved on
      • T Offline
        T Offline
        Thomas 63
        wrote on last edited by
        #3

        Wow, so easy! Thanks a lot!

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved