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

Display Values from database to Qcombobox

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.6k 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.
  • X Offline
    X Offline
    xayn13
    wrote on 25 Apr 2020, 20:49 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Apr 2020, 20:56 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
      • X Offline
        X Offline
        xayn13
        wrote on 25 Apr 2020, 21:47 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.

        M 1 Reply Last reply 25 Apr 2020, 21:48
        0
        • X xayn13
          25 Apr 2020, 21:47

          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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 Apr 2020, 21:48 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
          • X Offline
            X Offline
            xayn13
            wrote on 25 Apr 2020, 21:59 last edited by
            #5

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

            1 Reply Last reply
            0
            • X Offline
              X Offline
              xayn13
              wrote on 25 Apr 2020, 22:56 last edited by
              #6

              actually nevermind, it worked ! thank you !!

              M 1 Reply Last reply 25 Apr 2020, 23:18
              1
              • X xayn13
                25 Apr 2020, 22:56

                actually nevermind, it worked ! thank you !!

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 25 Apr 2020, 23:18 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
                • X Offline
                  X Offline
                  xayn13
                  wrote on 25 Apr 2020, 23:35 last edited by
                  #8

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

                  1 Reply Last reply
                  1
                  • E Offline
                    E Offline
                    eyllanesc
                    wrote on 26 Apr 2020, 02:15 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

                    1/9

                    25 Apr 2020, 20:49

                    • Login

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