// Janek Kozicki 2007 - a small example of using units with QT4 GUI #include #include #include "calculatorform.h" #include using namespace boost::units; CalculatorForm::CalculatorForm(QWidget *parent) : QWidget(parent) { QUiLoader loader; QFile file(":/forms/calculatorform.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = loader.load(&file, this); file.close(); acc_input = qFindChild(this, "acc_input"); mass_input = qFindChild(this, "mass_input"); acc_unit_box = qFindChild(this, "acc_combo"); mass_unit_box = qFindChild(this, "mass_combo"); force_txt = qFindChild(this, "force_txt"); force_unit_box = qFindChild(this, "force_combo"); ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// // adding conversion values by hand. But see note [1] on the bottom of this file // force_unit_box->addItem("N" , 1.0); force_unit_box->addItem("kN" , 1000.0); force_unit_box->addItem("dyne", 0.0001 ); // why this is not working? // force_unit_box->addItem("dyne", (1.0*CGS::dyne/SI::newton).value() ); // or this? // force_unit_box->addItem("dyne", static_cast(1.0*CGS::dyne/SI::newton) ); mass_unit_box->addItem("kg" , 1.0); mass_unit_box->addItem("g" , 0.001); acc_unit_box->addItem("m/s^2" , 1.0); acc_unit_box->addItem("km/s^2", 1000); acc_unit_box->addItem("gal" , 0.01 ); // or without a cast? - most preferable solution (dimensionless should implicitly convert to double) // acc_unit_box->addItem("gal" , (1.0*CGS::gal/(SI::meter/(SI::second*SI::second))) ); ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// QMetaObject::connectSlotsByName(this); connect(mass_input , SIGNAL(valueChanged(double) ), this, SLOT(calc())); connect(acc_input , SIGNAL(valueChanged(double) ), this, SLOT(calc())); connect(force_unit_box, SIGNAL(currentIndexChanged(int)), this, SLOT(calc())); connect(acc_unit_box , SIGNAL(currentIndexChanged(int)), this, SLOT(calc())); connect(mass_unit_box , SIGNAL(currentIndexChanged(int)), this, SLOT(calc())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout); setWindowTitle(tr("units test")); } quantity CalculatorForm::newtons_law(const quantity& m, const quantity& a) { return m*a; } void CalculatorForm::calc() { force_txt->setText(QString::number( newtons_law( mass_input->value() * SI::kilogram * mass_factor() , acc_input->value() * (SI::meter/(SI::second*SI::second)) * acc_factor() ) / SI::newton / force_factor() )); } // [1] // in the real world scenario those functions will reduce to an array of conversion // values, specified by the user from File->Preferences in the software's GUI, // where he will add new units he wants to use. // double CalculatorForm::mass_factor() { return mass_unit_box->itemData(mass_unit_box->currentIndex()).toDouble(); } double CalculatorForm::acc_factor() { return acc_unit_box->itemData(acc_unit_box->currentIndex()).toDouble(); } double CalculatorForm::force_factor() { return force_unit_box->itemData(force_unit_box->currentIndex()).toDouble(); }