Select column Qt ComboBox

0

I have a Connect button in mainwindow.ui

void MainWindow::on_pushButton_clicked()
{
    ConectarDB = QSqlDatabase::addDatabase("QMYSQL");
    ConectarDB.setHostName("localhost");
    ConectarDB.setDatabaseName("Banco");
    ConectarDB.setPort(3306);
    ConectarDB.setUserName("root");
    ConectarDB.setPassword("root");}

It works perfectly, but I have two doubts:

1) How would I make a message appear confirming that the connection was successful, if the button was clicked?

2) Once connected, I have another form with a comboBox in it.

Buscar_por_CURSO::Buscar_por_CURSO(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Buscar_por_CURSO)
{
    ui->setupUi(this);
    mBuscaCurso = new QSqlTableModel(this);
    mBuscaCurso->setTable("Cursos");
    mBuscaCurso->select();
    ui->comboBox->setModel(mBuscaCurso);
}

My table Courses:

Create table Cursos(
ID_curso int not null auto_increment,
Nome_curso varchar (50),
primary key (ID_curso)
)default charset = utf8;

This is what the comboBox looks like in the TrackID , but I would like it to be the TrackName .

How do I resolve this?

Thank you in advance

    
asked by anonymous 03.04.2017 / 09:04

1 answer

0

For problem 1:

void MainWindow::on_pushButton_clicked()
{
    ConectarDB = QSqlDatabase::addDatabase("QMYSQL");
    ConectarDB.setHostName("localhost");
    ConectarDB.setDatabaseName("Banco");
    ConectarDB.setPort(3306);
    ConectarDB.setUserName("root");
    ConectarDB.setPassword("root");
    **if (!ConectarDB.open()){
        QMessageBox::critical(this, "Error", ConectarDB.lastError().text());
        return;}
    else{
        QMessageBox msgBox;
        msgBox.setText("Conexão realizada com sucesso");
        msgBox.exec();}**
}

For problem 2:

Buscar_por_CURSO::Buscar_por_CURSO(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Buscar_por_CURSO)
{
    ui->setupUi(this);
    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("select nome_curso from Cursos");
    ui->comboBox->setModel(model);
}
    
03.04.2017 / 16:42