Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to use Signals and Slots to connect the various buttons to the appropriat

ID: 3648212 • Letter: I

Question

I have to use Signals and Slots to connect the various buttons to the appropriate functions in the program below.


I am not sure how to start this.


The program/code that has to be used to write it is the Qt Creator/QT with c++.


Explanation / Answer

#ifndef BMIVIEWER_H #define BMIVIEWER_H #include #include #include #include #include #include #include #include #include class BmiViewer : public QWidget { Q_OBJECT public: BmiViewer(); slots; void calculateBmi(); private: QLineEdit* heightEntry; QLineEdit* weightEntry; QLCDNumber* result; QErrorMessage* error; }; #endif // BMIVIEWER_H //bmiviewer.cpp #include "bmiviewer.h" BmiViewer::BmiViewer(){ setWindowTitle("MMI Calculator"); QGridLayout* layout = new QGridLayout; QLabel* inputWeightRequest = new QLabel ("Enter weight in Kg:"); weightEntry = new QLineEdit; QLabel* inputHeightRequest = new QLabel ("Enter height in meters:"); heightEntry = new QLineEdit; QPushButton* calc = new QPushButton ("Calculate"); QLabel* labelbmi = new QLabel ("BMI"); result = new QLCDNumber; result->setSegmentStyle(QLCDNumber::Flat); result->setDigitCount(8); layout->addWidget (inputWeightRequest, 0,0); layout->addWidget (weightEntry, 0,1); layout->addWidget (inputHeightRequest, 1,0); layout->addWidget (heightEntry, 1,1); layout->addWidget (calc, 2,1); layout->addWidget (labelbmi, 3,0); layout->addWidget (result, 3,1); setLayout(layout); //connect signals and slots connect(calc,SIGNAL(clicked()),this, SLOT(calculateBmi())); } void BmiViewer::calculateBmi(){ int wanswer=0; int hanswer=0; double bmi; QString iStr = weightEntry->text(); QString iStrh = heightEntry->text(); bool ok; wanswer = iStr.toInt(&ok); hanswer = iStrh.toInt(&ok); if (!ok) { error = new QErrorMessage(this); error->showMessage("Please enter a valid integer"); return; } //calculate BMI bmi=wanswer/(hanswer*hanswer); result->display(bmi); } //main.cpp #include #include "bmiviewer.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); BmiViewer w; w.show(); return a.exec(); }