Add 2 or more columns from MySQL to ComboBox?



  • 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);
    }
    

  • Moderators

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



  • 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());
        }
    

  • Moderators

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



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



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



  • @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 @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);
        }
    }
    


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



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

    Thanks,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.