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.

  • Lifetime Qt Champion

    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.

  • @Johanna0701


    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;
    QListView view;

  • 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();
         ui->listWidget_theme->addItem( a );

  • Lifetime Qt Champion

    you can use
    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