Problems with buttons

  • Hi to everyone,

    I'm trying to do a program. I have a little problem with the buttons. I had three buttons and I want that when I press one of them save a string in a variable, but the string is going to be different depending on the button that you clicked. I need to do this because I have to save this string in a database.

    Could someone help me please? thank yoou!!

  • @patcs It would be easier to answer if you could show the source code you have now.

  • Hi,

    As u mentioned, u need to store string values to the database when u click on the all three pushbutton,

    1. what are the string values, where do u get the values,?
    2. if all the values are different, then store in 3 different variables, and then when clicked the respective button pass the variable to database.


  • I could solve it with this code:

    void MainWindow::insertUser()
    QString dni_usuario = ui->lineEditDNI->text();
    QString nombre_usuario = ui->lineEditNombre->text();
    QString apellido_usuario = ui->lineEditPrimerApellido->text();
    QString apellido2_usuario = ui->lineEditSegundoApellido->text();
    QString edad_usuario = ui->lineEditEdad->text();
    QString notas_usuario = ui->textEdit->toPlainText();
    int estres_usuario = ui->horizontalSlider->value();
    QString fecha_sesion = ui->dateEdit->text();
    QString hora_sesion = ui->timeEdit->text();

    QString duracion_sesion = "";
        duracion_sesion = ui->radioButton->text();
    }else if(ui->radioButton_2->isChecked()){
        duracion_sesion = ui->radioButton_2->text();
    }else if(ui->radioButton_3->isChecked()){
        duracion_sesion = ui->radioButton_3->text();
    QString elementos_sesion = "";
    if(ui->checkBox->isChecked() && ui->checkBox_2->isChecked() && ui->checkBox_3->isChecked()){
        elementos_sesion = "Luces, Colores, Sonidos";
    }else if(ui->checkBox->isChecked() && ui->checkBox_2->isChecked()){
        elementos_sesion = "Luces, Colores";
    }else if(ui->checkBox->isChecked() && ui->checkBox_3->isChecked()){
        elementos_sesion = "Luces, Sonidos";
    }else if(ui->checkBox_2->isChecked() && ui->checkBox_3->isChecked()){
        elementos_sesion = "Colores, Sonidos";
    }else if(ui->checkBox->isChecked()){
        elementos_sesion = "Luces";
    }else if(ui->checkBox_2->isChecked()){
        elementos_sesion = "Colores";
    }else if(ui->checkBox_3->isChecked()){
        elementos_sesion = "Sonidos";
    QString consulta;
    consulta.append("INSERT INTO usuarios( dni, nombre, apellido, apellido2,edad, notas, estres, valoracion, duracion, elementos, fecha, hora)"
                        "values(:dni, :nombre, :apellido, :apellido2, :edad , :notas, :estres , :valoracion, :duracion, :elementos, :fecha, :hora);");
    QSqlQuery insertar;
    insertar.bindValue(":dni", dni_usuario);
    insertar.bindValue(":nombre", nombre_usuario);
    insertar.bindValue(":apellido", apellido_usuario);
    insertar.bindValue(":apellido2", apellido2_usuario);
    insertar.bindValue(":edad", edad_usuario);
    insertar.bindValue(":notas", notas_usuario);
    insertar.bindValue(":estres", estres_usuario);
    insertar.bindValue(":valoracion", valoracion_sesion);
    insertar.bindValue(":duracion", duracion_sesion);
    insertar.bindValue(":elementos", elementos_sesion);
    insertar.bindValue(":fecha", fecha_sesion);
    insertar.bindValue(":hora", hora_sesion);
        qDebug()<<"El USUARIO se ha insertado correctamente.";
        qDebug()<<"El USUARIO NO se ha insertado correctamente.";


    void MainWindow::on_buenaVal_clicked()
    valoracion_sesion = "Buena";

    void MainWindow::on_normalVal_clicked()
    valoracion_sesion = "Normal";

    void MainWindow::on_malaVal_clicked()
    valoracion_sesion = "Mala";

Log in to reply

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