Long time with out writing in my blog. But here comes some things I discover when I was playing with QT. Some things I discover (Reading the Documentation!!!) I will like to share with you.
First
How to convert double to Qstring?
Well this is easy we only called the function toDouble();
But wait there is something wrong with this, what will happen if I try to convert a letter or a symbol? Well the function toDouble has something special. This function accepts parameter, a bool and what does this bool do? Well if the conversion goes right it is true, and if it doesn’t converts it is false. So with this it is not necessary any throw exception like normal c++. Here it is a small example.
void MainWindow::convert_todouble() { QMessageBox msgBox; msgBox.setText("This is not a number!!!"); msgBox.exec(); } void MainWindow::on_pushButton_clicked() { double numbera,numberb,total; bool isdouble,isdoubleb; QString ans; numbera=ui->le_number1->text().toDouble(&isdouble); numberb=ui->le_number2->text().toDouble(&isdoubleb); if(isdouble==true && isdoubleb==true) { total=numbera+numberb; ans.setNum(total); ui->le_total->setText(ans); } else { convert_todouble(); } }
If you want to download the file here you have the link.