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. Add 2 or more columns from MySQL to ComboBox?
Forum Updated to NodeBB v4.3 + New Features

Add 2 or more columns from MySQL to ComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 3.5k 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.
  • M Offline
    M Offline
    meepo1
    wrote on 22 May 2017, 23:52 last edited by meepo1
    #1

    Here i show my code

    I want to get 2 columns from MySQL but i only can get one (maybe both) but i cannot set them in the same combobox

    .

    #include "Wind0.h"
    #include "ui_Wind0.h"
    #include <QSqlTableModel>
    #include <QMessageBox>
    #include <QSqlError>
    #include <QSqlQuery>
    #include <QDebug>
    #include <QSqlRecord>
    
    Wind0::Wind0(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Wind0)
    {
        ui->setupUi(this);
    
        mDatabase = QSqlDatabase::addDatabase("QMYSQL");
        mDatabase.setHostName("localhost");
        mDatabase.setDatabaseName("BD1");
        mDatabase.setPort(3306);
        mDatabase.setUserName("root");
        mDatabase.open();
    }
    
    Wind0::~Wind0()
    {
        delete ui;
    }
    void Wind0::on_pushButton_clicked()
    {
        QSqlQuery query;
        query.prepare("SELECT Name, Group FROM Teacher");
        query.exec();
        query.first();
        aux = query.value(0).toString();
        ui->comboBox->addItem(aux);
    }
    
    J 1 Reply Last reply 23 May 2017, 04:47
    0
    • M meepo1
      22 May 2017, 23:52

      Here i show my code

      I want to get 2 columns from MySQL but i only can get one (maybe both) but i cannot set them in the same combobox

      .

      #include "Wind0.h"
      #include "ui_Wind0.h"
      #include <QSqlTableModel>
      #include <QMessageBox>
      #include <QSqlError>
      #include <QSqlQuery>
      #include <QDebug>
      #include <QSqlRecord>
      
      Wind0::Wind0(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Wind0)
      {
          ui->setupUi(this);
      
          mDatabase = QSqlDatabase::addDatabase("QMYSQL");
          mDatabase.setHostName("localhost");
          mDatabase.setDatabaseName("BD1");
          mDatabase.setPort(3306);
          mDatabase.setUserName("root");
          mDatabase.open();
      }
      
      Wind0::~Wind0()
      {
          delete ui;
      }
      void Wind0::on_pushButton_clicked()
      {
          QSqlQuery query;
          query.prepare("SELECT Name, Group FROM Teacher");
          query.exec();
          query.first();
          aux = query.value(0).toString();
          ui->comboBox->addItem(aux);
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 May 2017, 04:47 last edited by
      #2

      @meepo1 I moved your post from "C++ GURUS" to "General and Desktop" forum as your question is not related to C++.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • B Offline
        B Offline
        beecksche
        wrote on 23 May 2017, 05:35 last edited by beecksche
        #3

        Hi @meepo1 ,
        you only read the first record.

        I think you should have a look at the QSqlQuery documentation, useful informations are also http://doc.qt.io/qt-5.8/sql-sqlstatements.html, or go through the examples http://doc.qt.io/qt-5.8/examples-sql.html

        I would change the code to something like that.

        QSqlQuery query("SELECT Name, Group FROM Teacher");
        int nameID = query.record().indexOf("Name");
        int groupID = query.record().indexOf("Group");
        while (query.next()) {
              ui->comboBox->addItem(query.value(nameID).toString());
              ui->comboBox->addItem(query.value(groupID).toString());
            }
        
        J 1 Reply Last reply 23 May 2017, 05:43
        2
        • B beecksche
          23 May 2017, 05:35

          Hi @meepo1 ,
          you only read the first record.

          I think you should have a look at the QSqlQuery documentation, useful informations are also http://doc.qt.io/qt-5.8/sql-sqlstatements.html, or go through the examples http://doc.qt.io/qt-5.8/examples-sql.html

          I would change the code to something like that.

          QSqlQuery query("SELECT Name, Group FROM Teacher");
          int nameID = query.record().indexOf("Name");
          int groupID = query.record().indexOf("Group");
          while (query.next()) {
                ui->comboBox->addItem(query.value(nameID).toString());
                ui->comboBox->addItem(query.value(groupID).toString());
              }
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 23 May 2017, 05:43 last edited by jsulm
          #4

          @beecksche I think he wants to have two columns in the combobox: one for name and one for group. So each line in the combobox consists of two columns.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 23 May 2017, 06:37
          1
          • J jsulm
            23 May 2017, 05:43

            @beecksche I think he wants to have two columns in the combobox: one for name and one for group. So each line in the combobox consists of two columns.

            B Offline
            B Offline
            beecksche
            wrote on 23 May 2017, 06:37 last edited by
            #5

            @jsulm
            Yeah maybe! Not clear for me in the question.
            To add two different columns in one QComboBox seperately doesn't make sense for me also.

            Or using a QAbstractItemModel to set the items of the QComboBox could also be a solution.

            M 1 Reply Last reply 23 May 2017, 18:08
            2
            • B beecksche
              23 May 2017, 06:37

              @jsulm
              Yeah maybe! Not clear for me in the question.
              To add two different columns in one QComboBox seperately doesn't make sense for me also.

              Or using a QAbstractItemModel to set the items of the QComboBox could also be a solution.

              M Offline
              M Offline
              meepo1
              wrote on 23 May 2017, 18:08 last edited by meepo1
              #6

              @beecksche @jsulm Oh so sorry it was not clear enough
              When i said "columns" i meant "database attribute". I am trying to get 2 records from 2 differents attributes (Name and Group) from one table (Teacher).
              I want to set the teacher's name and his Group number in my combobox.
              My combobox should show:
              William 01
              Achilles 01
              Pietro 02

              1 Reply Last reply
              1
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on 23 May 2017, 18:33 last edited by
                #7

                @meepo1 ,

                while adding into combobox, glub them together into one string or may be stringlist and use additem(0 or addItems() method.

                Thanks,

                Pradeep Kumar
                Qt,QML Developer

                M 1 Reply Last reply 23 May 2017, 21:46
                3
                • Pradeep KumarP Pradeep Kumar
                  23 May 2017, 18:33

                  @meepo1 ,

                  while adding into combobox, glub them together into one string or may be stringlist and use additem(0 or addItems() method.

                  Thanks,

                  M Offline
                  M Offline
                  meepo1
                  wrote on 23 May 2017, 21:46 last edited by
                  #8

                  @Pradeep-Kumar @beecksche @jsulm I have a last question, why when i use query.first() the query starts at second row while when i do not use it, the query starts at first row?

                  void Wind0::on_pushButton_clicked()
                  {
                      QSqlQuery query;
                      query.prepare("SELECT Name, Group FROM Teacher");
                      query.exec();
                      query.first();
                      while(query.next()){
                          int nameID = query.record().indexOf("Name");
                          int groupID = query.record().indexOf("Group");
                          QString a = query.value(nameID).toString();
                          QString b = query.value(groupID).toString();
                          n = a+" "+"("+b+")";
                          ui->comboBox->addItem(n);
                      }
                  }
                  
                  Venkatesh VV 1 Reply Last reply 24 May 2017, 03:34
                  1
                  • M meepo1
                    23 May 2017, 21:46

                    @Pradeep-Kumar @beecksche @jsulm I have a last question, why when i use query.first() the query starts at second row while when i do not use it, the query starts at first row?

                    void Wind0::on_pushButton_clicked()
                    {
                        QSqlQuery query;
                        query.prepare("SELECT Name, Group FROM Teacher");
                        query.exec();
                        query.first();
                        while(query.next()){
                            int nameID = query.record().indexOf("Name");
                            int groupID = query.record().indexOf("Group");
                            QString a = query.value(nameID).toString();
                            QString b = query.value(groupID).toString();
                            n = a+" "+"("+b+")";
                            ui->comboBox->addItem(n);
                        }
                    }
                    
                    Venkatesh VV Offline
                    Venkatesh VV Offline
                    Venkatesh V
                    wrote on 24 May 2017, 03:34 last edited by
                    #9

                    @meepo1

                    Hi...
                    Why it is retrieves second row is because, before while loop you are calling query.first() method it focuses the first row, after that in while loop again you are calling query.next() so obviously it points to second row. In your case query.first() is not required.

                    1 Reply Last reply
                    7
                    • Pradeep KumarP Offline
                      Pradeep KumarP Offline
                      Pradeep Kumar
                      wrote on 24 May 2017, 05:43 last edited by Pradeep Kumar
                      #10

                      @meepo1
                      Good to hear u got answer, for the topic from other members as well , in the forum.

                      Thanks,

                      Pradeep Kumar
                      Qt,QML Developer

                      1 Reply Last reply
                      1

                      1/10

                      22 May 2017, 23:52

                      • Login

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