PDA

View Full Version : QLineEdit and select



GuL
9th September 2008, 15:44
I'm using this code

QSqlQuery query_p_total;

query_p_total.prepare("SELECT :x * :y");

query_p_total.bindValue(":x", ui.peso_por_peca_label->text() );
query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text() );

if (!query_p_total.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
while (query_p_total.next()) {
ui.peso_total_final_label->setText(query_p_total.value(0).toString());
}
}

ui.peso_por_peca_label->text() ) has value 20.16 and ui.n_pecas_final_lineEdit->text() has value 6, then ui.peso_total_final_label should show me 120.96, but it is showing me 121. When I try to select 20.16 * 6 in the MYSQL console I get the right result, so I think there is something with QLineEdit, but what could it be? Or what else could it be?

Renan

wysota
9th September 2008, 16:21
What does query_p_total.value(0).toDouble() return?

GuL
9th September 2008, 17:05
returns 121

Renan

wysota
9th September 2008, 21:08
And "qDebug() << query_p_total.value(0)"?

GuL
10th September 2008, 12:45
this is the return:

QVariant(double, 121)

wysota
10th September 2008, 13:06
In that case that is what the database returns.

GuL
10th September 2008, 13:30
the hole code:

void testeMYSQL2::on_gerar_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(ui.hostname_lineEdit->text());
db.setDatabaseName(ui.database_lineEdit->text());
db.setUserName(ui.username_lineEdit->text());
db.setPassword(ui.password_lineEdit->text());
db.open();
if (!db.open()){
QMessageBox::critical(0, tr("Error"),
QString("The error:\n%1").arg(db.lastError().text()));
}
else{
QSqlQuery query;

if (!query.exec("select Mantas.Tipo, Mantas.Espessura, Mantas.Compr_Final, Mantas.Larg_Final,"
"Mantas.Dens, Mantas.Qtd_Pecas_Final from Mantas where Mantas.ID_Prod = 1010130030000")){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
while (query.next()) {
ui.prod_label->setText(query.value(0).toString());
ui.espessura_label->setText(query.value(1).toString() + "MM");
ui.compr_final_lineEdit->setText(query.value(2).toString());
ui.larg_final_lineEdit->setText(query.value(3).toString());
ui.dens_lineEdit->setText(query.value(4).toString());
ui.n_pecas_final_lineEdit->setText(query.value(5).toString());
}
}
QSqlQuery query_p;

// query_p.prepare(QString("select (%1*%2*%3*%4)/1000000").arg(
// ui.compr_final_lineEdit->text()).arg(
// ui.larg_final_lineEdit->text()).arg(ui.espessura_label->text()).arg(ui.dens_lineEdit->text()));

query_p.prepare("SELECT (( :a * :b * :c * :d)/1000000)*1");

query_p.bindValue(":a", ui.compr_final_lineEdit->text() );
query_p.bindValue(":b", ui.larg_final_lineEdit->text() );
query_p.bindValue(":c", ui.espessura_label->text() );
query_p.bindValue(":d", ui.dens_lineEdit->text() );

if (!query_p.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
while (query_p.next()) {
ui.peso_por_peca_label->setText(query_p.value(0).toString());
qDebug() << query_p.value(0);
}
}
QSqlQuery query_p_total;

query_p_total.prepare("SELECT :x * :y");

query_p_total.bindValue(":x", ui.peso_por_peca_label->text() );
query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text() );

if (!query_p_total.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!"));
}else{
while (query_p_total.next()) {
ui.peso_total_final_label->setText(query_p_total.value(0).toString());
// qDebug() << query_p_total.value(0).toDouble();
qDebug() << query_p_total.value(0);
}
}

}
db.close();
}

and this is my table:

create table `Mantas` (

`ID_Prod` Decimal (14),

`Tipo` varchar (30),

`Espessura` double ,

`Dens` float ,

`Compr_Inicial` double ,

`Larg_Inicial` double ,

`Qtd_Pecas_Inicial` double ,

`Qtd_Pastas` double ,

`Pastas_Por_Peca` double ,

`Peso_Pasta` float ,

`Peso_Total_Inicial` float ,

`Qtd_Pastas_Capinha` double ,

`Peso_Pasta_Capinha` float ,

`Compr_Final` double ,

`Larg_Final` double ,

`Peso_Por_Peca` float ,

`Peso_Total_Final` float ,

`Qtd_Pecas_Final` double ,

`Quebra` float ,

`Encolh` float

);

insert into `Mantas` (`ID_Prod`, `Tipo`, `Espessura`, `Dens`, `Compr_Inicial`, `Larg_Inicial`, `Qtd_Pecas_Inicial`, `Qtd_Pastas`,
`Pastas_Por_Peca`, `Peso_Pasta`, `Peso_Total_Inicial`, `Qtd_Pastas_Capinha`, `Peso_Pasta_Capinha`, `Compr_Final`, `Larg_Final`,
`Peso_Por_Peca`, `Peso_Total_Final`, `Qtd_Pecas_Final`, `Quebra`, `Encolh`)
values('1010130030000','ENBCO','3','0.30','20000', '1900','6','0','0',
'5.30',NULL,NULL,'4.60','14000','1600','0.00','0.0 0','6','0.20','0.30');


those are qDebug return in order:

QVariant(double, 20.16)
QVariant(double, 121)

why the second results 121 and the first 20.16 ?

jacek
11th September 2008, 20:30
What happens if you use this instead?

double total = query_p_total.value(0).toDouble();
ui.peso_total_final_label->setText( QString::number( total, 'f', 2 ) );

GuL
18th September 2008, 15:22
Thanks jacek!

That solves my problem.

I have done another thing, but it solves half of my problem ( Now there are too much decimals). I'm going to use your solution. If someone want's here is the code:

QSqlQuery query_p_total;

query_p_total.prepare("SELECT :x * :y");

query_p_total.bindValue(":x", ui.peso_por_peca_label->text().toFloat() );
query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text().toFloat() );

if (!query_p_total.exec()){
QMessageBox::critical(0, tr("Error"),
QString("There is an error with query.exec()!\n%1").arg(query_p_total.lastError().text()));
}else{
while (query_p_total.next()) {
ui.peso_total_final_label->setText(query_p_total.value(0).toString());
}
}