[SOLVED] Signals and Slots beginner problem
-
Just a rough code..
[quote author="Dn588" date="1396377011"]
QObject::connect(m_calculate, SIGNAL(clicked()), bmi, SLOT(calcBmi(m_weight, m_height));
[/quote]This is wrong way,You cannot connect in such way.
[quote author="Dn588" date="1396377011"]
I connected the calculate button with the calcBmi slot from another class called bmi. How could I pass the values in the weight and height QdoubleSpinBoxes to the calcBmi slot?also if the calcBmi slot/function calculated the bmi how could I send the bmi value to the bmi QLineEdit in the bmiGui class? [/quote]Do not make slot in bmi class.make a simple calcbmi() function in bmi class.
e.g;
QObject::connect(m_calculate, SIGNAL(clicked()), this, SLOT(BtnClicked()));void bmiGui::BtnClicked()
{
bmi obj; //make an object of bmi class
double weight = m_weight.value();
double height = m_height.value();
double result = obj.calcBmi(weight,height); //calcBmi function in bmi class that returns value after calculating
m_bmi.setText(QString::number(result));
}[quote author="Dn588" date="1396377011"]
As far as I understand all the bmiGui widgets are private member variables of bmiGui so the bmi class doesn't have access to them? [/quote]No this is not the reason, you are not passing gui you dealing with values.
hope it helps. -
Thanks ScotR and IamSumit I won't forget that in future:)
My calculate_clicked slot now looks like this:
@
{
bmi obj;
weight = m_weight->value();
height = m_height->value();
retVal = obj.calcBmi(weight, height);
bmi.setText(bmiVal);
}
@Now i'm getting an error "No matching function bmi::bmi() candidates are bmi::bmi(double double) and bmi::bmi(const bmi&)
but when I instantiate bmiObj I pass weight and height as parameters which are both of type double.
Any idea what I am doing wrong?[edit: added coding tags SGaist]
-
Hi ScottR Sorry I completely forgot about code formatting...
My bmi class looks like this:
@
bmi.h
#ifndef BMI_H
#define BMI_Hclass bmi
{
public:
bmi(double weight, double height);
double calcBmi(double weight, double height);
private:
double m_weight, m_height;
};#endif // BMI_H
bmi.cpp
#include "bmi.h"
bmi::bmi(double weight, double height) : m_weight(weight), m_height(height)
{}
double bmi::calcBmi(doubleweight, double height) {
return weight / (height * height);
}
@ -
You have two choice either pass the values in constructor or in function(as i gave you code)
[quote author="Dn588" date="1396468975"]Hi ScottR Sorry I completely forgot about code formatting...My bmi class looks like this:
@
bmi.h
#ifndef BMI_H
#define BMI_Hclass bmi
{
public:
bmi(double weight, double height);
double calcBmi(double weight, double height);
private:
double m_weight, m_height;
};#endif // BMI_H
bmi.cpp
#include "bmi.h"
bmi::bmi(double weight, double height) : m_weight(weight), m_height(height)
{}
double bmi::calcBmi(doubleweight, double height) {
return weight / (height * height);
}
@[/quote]Here you should use a simple constructor without parameter.
e.g;
instead of
bmi(double weight, double height); write bmi(); only
edit your code in both .h and .cpp file for simple parameterless constructor.hope it helps
-
This most probably means that your moc file is not up to date. Try to clean the project, run qmake and build again.
And be sure that your classes using signal and slots have the Q_OBJECT macro (before running qmake).
Edit: it may also be caused by a forgotten dependance in your .pro file. Could you give a more detailed compiler output?