Display SQLite DB content on ListWidget



  • How can I display the content of my SQLiteDB on a ListWidget?
    I tried several ways, but when I want to add it as a new item, I get the message, that QString (or QSqlQuery) can't be converted to QListWidgetItem.


  • Qt Champions 2016

    Hi
    you call
    ui->listWidget->addItem( QString );

    when you loop over the QSqlQuery results

    However, why such involving way ?

    If you use a list view and this model, its almost for free.
    http://doc.qt.io/qt-5/qsqlquerymodel.html#details



  • @Johanna0701

    Hi,

    As @mrjj said use QListView for dispaly the contents.

    For this you have to do like this,
    QSqlQuery query;
    query.prepare("Your query");
    QSqlQueryModel model;
    model.setQuery(query);
    QListView view;
    view.setModel(model);



  • Hi! Thanks for the answers!
    Actually I also wanted to make the items checkable, that's why I started with a ListWidget.
    However, I don't get any error messages for now, but the program is not doing anything when I execute and click on the ListWidget.
    Check out the code:

    /void Hochladen::on_listWidget_theme_clicked(const QModelIndex &index)
    {
    
        NeuesThema conn; //Zur Verbindung mit der Themen Datenbank
    
        QSqlQuery query;
        conn.connOpen(); //Öffnen der Verbindung
    
         query.prepare("select * from themen");
        if (query.exec()) {
    
            if(query.next()) {
    
        QString a = query.boundValue(0).toString();
    
        //reduced the code for now:
        //ui->listWidget_theme->addItem( QString );
        //QListWidgetItem *item = new QListWidgetItem();
        //item->setCheckState(Qt::Unchecked);
        
         ui->listWidget_theme->addItem( a );
    
         conn.connClose();
            }
        }
    }```

  • Qt Champions 2016

    Hi
    you can use
    http://doc.qt.io/qt-5/qsqlquery.html#lastError
    to see if something wrong and its
    also recommended
    to use the debugger to step into the function
    and see what gets executed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.