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. Display SQLite DB content on ListWidget
QtWS25 Last Chance

Display SQLite DB content on ListWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.7k Views
  • 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.
  • J Offline
    J Offline
    Johanna0701
    wrote on last edited by
    #1

    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.

    Venkatesh VV 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      5
      • J Johanna0701

        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.

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

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

        1 Reply Last reply
        3
        • J Offline
          J Offline
          Johanna0701
          wrote on last edited by
          #4

          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();
                  }
              }
          }```
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

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