0

I have database and i want to put data to my combobox:

     QSqlQueryModel *model = new QSqlQueryModel (ui->comboBox);
     model->setQuery ("SELECT country_name FROM Country");
     ui->comboBox->setModel(model);

But i don't know how to code my mainwindow.h and mainwindow.cpp files What slot for combobox should i use, what void? it is so simple, but a can't find anything about this problem.

1 Answers1

0

so, thanks for your answers...

I had pasted the code right into the:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{  
    ui->setupUi(this);
}

it is in the .cpp file

I guess, nobody knew about it...

Full code is:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("MyDB");
    db.setUserName("root");
    db.setPassword("123456789");
    if(!db.open())
    {
        qDebug() << "Error connecting";
    }
    else
    {
        qDebug() << "Connected";
        QSqlQueryModel *model = new QSqlQueryModel (ui->comboBox);
        model->setQuery ("SELECT country_name FROM Country");
        ui->comboBox->setModel(model);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}