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
Forum Updated to NodeBB v4.3 + New Features

Display SQLite DB content on ListWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.7k Views 2 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.
  • J Offline
    J Offline
    Johanna0701
    wrote on 13 Sept 2017, 10:55 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.

    V 1 Reply Last reply 14 Sept 2017, 04:02
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Sept 2017, 11:10 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
        13 Sept 2017, 10:55

        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.

        V Offline
        V Offline
        Venkatesh V
        wrote on 14 Sept 2017, 04:02 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 14 Sept 2017, 12:57 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
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 14 Sept 2017, 13:07 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

            4/5

            14 Sept 2017, 12:57

            • Login

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