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 Values from database to Qcombobox
Qt 6.11 is out! See what's new in the release blog

Display Values from database to Qcombobox

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.0k 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.
  • xayn13X Offline
    xayn13X Offline
    xayn13
    wrote on last edited by
    #1

    Hi ! I'm a newbie with QT and c++, I have been trying to display values from Qcombobox from the Database, but no avail. I was wondering if someone can take a look and help me.
    Here's a section of my code

    QSqlQuery query;
    query.prepare("SELECT rep FROM Auth");
    query.exec();
    query.first();
    QString a= query.value(0).toString();
    ui->nnid->addItem(a);

    Thank you

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

      Hi and welcome to the forums.

      If you dont get anything in combobox
      then i guess the SqlQuery dont get any results ?

      update:
      you could try

      if ( ! query.first() )
      qDebug() << "no record";

      QString a= query.value(0).toString();
      ui->nnid->addItem(a);

      to see if it can position on first record.

      1 Reply Last reply
      1
      • xayn13X Offline
        xayn13X Offline
        xayn13
        wrote on last edited by
        #3

        Thank you for taking the time to answer me, however that did not work as well. I honestly don't know what I'm doing wrong, because apart from this, I already have a Crud working.

        mrjjM 1 Reply Last reply
        0
        • xayn13X xayn13

          Thank you for taking the time to answer me, however that did not work as well. I honestly don't know what I'm doing wrong, because apart from this, I already have a Crud working.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @xayn13

          Did it show "no record" ?

          or what happens ?

          While you are not checking return values, it does look ok.

          1 Reply Last reply
          0
          • xayn13X Offline
            xayn13X Offline
            xayn13
            wrote on last edited by
            #5

            Sorry I definitely forgot to mention that part.
            no actually nothing showed up !!!

            1 Reply Last reply
            0
            • xayn13X Offline
              xayn13X Offline
              xayn13
              wrote on last edited by
              #6

              actually nevermind, it worked ! thank you !!

              mrjjM 1 Reply Last reply
              1
              • xayn13X xayn13

                actually nevermind, it worked ! thank you !!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @xayn13
                Super :)

                For debugging help , its a good idea to do

                QSqlQuery query;
                query.exec(QString("xxx");
                if (query.next())
                {
                // use it
                } else {
                    qDebug() << "SqLite error:" << query.lastError().text() << ", SqLite error code:" << query.lastError().number();
                }
                

                as it does report good stuff in case of something up with the query.

                1 Reply Last reply
                2
                • xayn13X Offline
                  xayn13X Offline
                  xayn13
                  wrote on last edited by
                  #8

                  that's more or less what I did !
                  Thank you though !!

                  1 Reply Last reply
                  1
                  • eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by eyllanesc
                    #9

                    @xayn13 Another possible solution is to use QSqlQueryModel:

                    QSqlQueryModel *model = new QSqlQueryModel;
                    model->setQuery("SELECT rep FROM Auth");
                    ui->nnid->setModel(model);
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    1

                    • Login

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