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?
QtWS25 Last Chance

Add 2 or more columns from MySQL to ComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 3.5k 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.
  • M Offline
    M Offline
    meepo1
    wrote on 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);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • M meepo1

      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);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      • beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on 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());
            }
        
        jsulmJ 1 Reply Last reply
        2
        • beeckscheB beecksche

          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());
              }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

          beeckscheB 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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.

            beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on 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
            2
            • beeckscheB beecksche

              @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 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 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
                3
                • Pradeep KumarP Pradeep Kumar

                  @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 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
                  1
                  • M meepo1

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

                      • Login

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