首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算BMI程序

计算BMI程序
EN

Stack Overflow用户
提问于 2012-05-14 12:48:05
回答 1查看 1.4K关注 0票数 0

这是一个通过输入体重和身高来计算体重指数的程序。

代码语言:javascript
复制
#ifndef BMIVIEWER_H
#define BMIVIEWER_H
#include <QWidget>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QLCDNumber>
#include <QErrorMessage>
#include <QString>
#include <QMessageBox>

class BmiViewer : public QWidget {
   Q_OBJECT

public:

    BmiViewer();
    void calculateBmi();

private:
 QLineEdit* heightEntry;
 QLineEdit* weightEntry;
 QLCDNumber* result;
 QErrorMessage* error;
};

#endif // BMIVIEWER_H

bmiviewer.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#include <QApplication>
#include "bmiviewer.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    BmiViewer w;
    w.show();

    return a.exec();
}

当我编译它时,它在bmiviewer.cpp:29中输出: Object::connect: No slot BmiViewer::calculateBmi()

它显示了界面,但没有进行任何计算。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-14 12:49:41

添加这行

代码语言:javascript
复制
public slots:

在此之前

代码语言:javascript
复制
void calculateBmi();

您从未将bmi函数声明为插槽,因此无法连接到它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10577620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档