Problem to convert Float or Double to String in QLineEdit



  • Dear community, I have make an operation to calculate but when the amount is more than 1 million, the QLineEdit don't show all the number. Example : 1000000 = 1e+06.
    This is the code I have written

    @
    void PersoInfo::on_CalculOp_clicked()
    {
    //Permet de calculer une variable float et afficher le resultat
    //Proceder convertir String en float et enfin float en String
    QString str1,str2,str4,str5,str7,str8;

         str1=ui->txt_Prix_Salle->text();
         str2=ui->txt_Duree_Salle->text();
         str4=ui->txt_Prix_Chambre->text();
         str5=ui->txt_Duree_Chambre->text();
         str7=ui->txt_Prix_Restau->text();
         str8=ui->txt_Qte_Restau->text();
    
        float Csal,Ccha,Cres,Net,Tva;
         float Psal = str1.toFloat();
         float Dsal = str2.toFloat();
         float Pcha = str4.toFloat();
         float Dcha = str5.toFloat();
         float Pres = str7.toFloat();
         float Qres = str8.toFloat();
    
    
         Csal = Psal * Dsal;
         Ccha = Pcha * Dcha;
         Cres = Pres * Qres;
         Net = Csal + Ccha + Cres ;
         Tva = ((Net * 18) / 100);
    
         QString str3 = QString::number(Csal);
         ui->txt_Total_Salle->setText(str3);
    
         QString str6 = QString::number(Ccha);
         ui->txt_Total_Chambre->setText(str6);
    
         QString str9 = QString::number(Cres);
         ui->txt_Total_Restau->setText(str9);
    
         QString str0 = QString::number(Net);
         ui->txt_Net_Caisse->setText(str0);
    
         QString str10 = QString::number(Tva);
         ui->txt_Tva_Caisse->setText(str10);
    

    }
    @

    Thanks to help me

    [edit: added missing coding tags @ SGaist]


  • Moderators

    Hi,

    Please encapsulate the code inside the codetags "@@". Otherwise its difficult to understand the code.

    While converting to QString from number you can use "this":http://qt-project.org/doc/qt-5/qstring.html#number-2 overloaded function to display according to your need.
    So to display a million try this:
    @
    float num = 1000000;
    ui->lineEdit->setText(QString::number(num,'g',7));
    @

    Just set the proper precision as per the input number.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why don't you use QSpinBox or QDoubleSpinBox ? This way you don't have to check that your user enter anything else than numbers and you don't need to do all these conversions



  • Please tell me How to use QSpinBox or QDoubleSpinBox. And I have a second problem, my application is saving in Sqlite Database and would like to write code to export the database in CSV and also Print my QLineEdit present on my Widget. How can I do that? Please Help Me


  • Lifetime Qt Champion

    Please, first read the documentation about these two classes. Then follow the QtSQL module for database related stuff and QtPrint for printing.

    The CSV format is pretty simple, you will need QFile and maybe QTextStream to simplify things a bit.



  • Ok, thanks for the tips


Log in to reply
 

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