Results 1 to 9 of 9

Thread: QLineEdit and select

  1. #1
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QLineEdit and select

    I'm using this code
    Qt Code:
    1. QSqlQuery query_p_total;
    2.  
    3. query_p_total.prepare("SELECT :x * :y");
    4.  
    5. query_p_total.bindValue(":x", ui.peso_por_peca_label->text() );
    6. query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text() );
    7.  
    8. if (!query_p_total.exec()){
    9. QMessageBox::critical(0, tr("Error"),
    10. QString("There is an error with query.exec()!"));
    11. }else{
    12. while (query_p_total.next()) {
    13. ui.peso_total_final_label->setText(query_p_total.value(0).toString());
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit and select

    What does query_p_total.value(0).toDouble() return?

  3. #3
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and select

    returns 121

    Renan
    Last edited by GuL; 9th September 2008 at 17:20.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit and select

    And "qDebug() << query_p_total.value(0)"?

  5. #5
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and select

    this is the return:

    QVariant(double, 121)

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit and select

    In that case that is what the database returns.

  7. #7
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and select

    the hole code:
    Qt Code:
    1. void testeMYSQL2::on_gerar_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName(ui.hostname_lineEdit->text());
    5. db.setDatabaseName(ui.database_lineEdit->text());
    6. db.setUserName(ui.username_lineEdit->text());
    7. db.setPassword(ui.password_lineEdit->text());
    8. db.open();
    9. if (!db.open()){
    10. QMessageBox::critical(0, tr("Error"),
    11. QString("The error:\n%1").arg(db.lastError().text()));
    12. }
    13. else{
    14. QSqlQuery query;
    15.  
    16. if (!query.exec("select Mantas.Tipo, Mantas.Espessura, Mantas.Compr_Final, Mantas.Larg_Final,"
    17. "Mantas.Dens, Mantas.Qtd_Pecas_Final from Mantas where Mantas.ID_Prod = 1010130030000")){
    18. QMessageBox::critical(0, tr("Error"),
    19. QString("There is an error with query.exec()!"));
    20. }else{
    21. while (query.next()) {
    22. ui.prod_label->setText(query.value(0).toString());
    23. ui.espessura_label->setText(query.value(1).toString() + "MM");
    24. ui.compr_final_lineEdit->setText(query.value(2).toString());
    25. ui.larg_final_lineEdit->setText(query.value(3).toString());
    26. ui.dens_lineEdit->setText(query.value(4).toString());
    27. ui.n_pecas_final_lineEdit->setText(query.value(5).toString());
    28. }
    29. }
    30. QSqlQuery query_p;
    31.  
    32. // query_p.prepare(QString("select (%1*%2*%3*%4)/1000000").arg(
    33. // ui.compr_final_lineEdit->text()).arg(
    34. // ui.larg_final_lineEdit->text()).arg(ui.espessura_label->text()).arg(ui.dens_lineEdit->text()));
    35.  
    36. query_p.prepare("SELECT (( :a * :b * :c * :d)/1000000)*1");
    37.  
    38. query_p.bindValue(":a", ui.compr_final_lineEdit->text() );
    39. query_p.bindValue(":b", ui.larg_final_lineEdit->text() );
    40. query_p.bindValue(":c", ui.espessura_label->text() );
    41. query_p.bindValue(":d", ui.dens_lineEdit->text() );
    42.  
    43. if (!query_p.exec()){
    44. QMessageBox::critical(0, tr("Error"),
    45. QString("There is an error with query.exec()!"));
    46. }else{
    47. while (query_p.next()) {
    48. ui.peso_por_peca_label->setText(query_p.value(0).toString());
    49. qDebug() << query_p.value(0);
    50. }
    51. }
    52. QSqlQuery query_p_total;
    53.  
    54. query_p_total.prepare("SELECT :x * :y");
    55.  
    56. query_p_total.bindValue(":x", ui.peso_por_peca_label->text() );
    57. query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text() );
    58.  
    59. if (!query_p_total.exec()){
    60. QMessageBox::critical(0, tr("Error"),
    61. QString("There is an error with query.exec()!"));
    62. }else{
    63. while (query_p_total.next()) {
    64. ui.peso_total_final_label->setText(query_p_total.value(0).toString());
    65. // qDebug() << query_p_total.value(0).toDouble();
    66. qDebug() << query_p_total.value(0);
    67. }
    68. }
    69.  
    70. }
    71. db.close();
    72. }
    To copy to clipboard, switch view to plain text mode 

    and this is my table:
    Qt Code:
    1. create table `Mantas` (
    2.  
    3. `ID_Prod` Decimal (14),
    4.  
    5. `Tipo` varchar (30),
    6.  
    7. `Espessura` double ,
    8.  
    9. `Dens` float ,
    10.  
    11. `Compr_Inicial` double ,
    12.  
    13. `Larg_Inicial` double ,
    14.  
    15. `Qtd_Pecas_Inicial` double ,
    16.  
    17. `Qtd_Pastas` double ,
    18.  
    19. `Pastas_Por_Peca` double ,
    20.  
    21. `Peso_Pasta` float ,
    22.  
    23. `Peso_Total_Inicial` float ,
    24.  
    25. `Qtd_Pastas_Capinha` double ,
    26.  
    27. `Peso_Pasta_Capinha` float ,
    28.  
    29. `Compr_Final` double ,
    30.  
    31. `Larg_Final` double ,
    32.  
    33. `Peso_Por_Peca` float ,
    34.  
    35. `Peso_Total_Final` float ,
    36.  
    37. `Qtd_Pecas_Final` double ,
    38.  
    39. `Quebra` float ,
    40.  
    41. `Encolh` float
    42.  
    43. );
    44.  
    45. insert into `Mantas` (`ID_Prod`, `Tipo`, `Espessura`, `Dens`, `Compr_Inicial`, `Larg_Inicial`, `Qtd_Pecas_Inicial`, `Qtd_Pastas`,
    46. `Pastas_Por_Peca`, `Peso_Pasta`, `Peso_Total_Inicial`, `Qtd_Pastas_Capinha`, `Peso_Pasta_Capinha`, `Compr_Final`, `Larg_Final`,
    47. `Peso_Por_Peca`, `Peso_Total_Final`, `Qtd_Pecas_Final`, `Quebra`, `Encolh`)
    48. values('1010130030000','ENBCO','3','0.30','20000','1900','6','0','0',
    49. '5.30',NULL,NULL,'4.60','14000','1600','0.00','0.00','6','0.20','0.30');
    To copy to clipboard, switch view to plain text mode 

    those are qDebug return in order:

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

    why the second results 121 and the first 20.16 ?
    Last edited by jacek; 11th September 2008 at 20:28. Reason: wrapped too long lines

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and select

    What happens if you use this instead?
    Qt Code:
    1. double total = query_p_total.value(0).toDouble();
    2. ui.peso_total_final_label->setText( QString::number( total, 'f', 2 ) );
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jacek for this useful post:

    GuL (18th September 2008)

  10. #9
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and select

    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:
    Qt Code:
    1. QSqlQuery query_p_total;
    2.  
    3. query_p_total.prepare("SELECT :x * :y");
    4.  
    5. query_p_total.bindValue(":x", ui.peso_por_peca_label->text().toFloat() );
    6. query_p_total.bindValue(":y", ui.n_pecas_final_lineEdit->text().toFloat() );
    7.  
    8. if (!query_p_total.exec()){
    9. QMessageBox::critical(0, tr("Error"),
    10. QString("There is an error with query.exec()!\n%1").arg(query_p_total.lastError().text()));
    11. }else{
    12. while (query_p_total.next()) {
    13. ui.peso_total_final_label->setText(query_p_total.value(0).toString());
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. style sheets select all except something
    By codeslicer in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2008, 00:21
  2. QLineEdit selectAll() in eventFilter
    By mclark in forum Qt Programming
    Replies: 6
    Last Post: 1st February 2008, 08:13
  3. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.