PDA

View Full Version : Problem Getting User-Defined Signals/Slots to Work



Radiotelephone
2nd August 2009, 23:21
My first attempt at QT programming is a piece of code to caculate the kinetic energy of an airgun pellet. Code as follows:


//Bullet kinetic energy calculator
//June 09
#include <QObject>
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDialog>
#include <math.h>
#include <stdlib.h>

class ke : public QObject
{
Q_OBJECT

public:
void calc_ke();

signals:
void pass_ke(char* ke);

private slots:
void pass_velocity(char *velocity);
//
void pass_mass(const QString &mass);

private:
float fp_mass;
float fp_velocity;

};

void ke::calc_ke()
{
float fp_kinetic_energy;
char kinetic_energy[32];
//double fp_mass = atof (mass);
//double fp_velocity = atof (velocity);
fp_kinetic_energy=fp_mass*(fp_velocity*fp_velocity )/450450.0;

sprintf(kinetic_energy, "%.4f", fp_kinetic_energy);

emit pass_ke(kinetic_energy);
}

void ke::pass_velocity(char* velocity)
{
float fp_velocity;
fp_velocity =(float) atof (velocity);
ke::calc_ke();
}

//void ke::pass_mass(char* mass)
void ke::pass_mass(const QString &mass)
{
float fp_mass;
fp_mass =(float) atof ((char)mass);
ke::calc_ke();
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);



QLabel* mass_label = new QLabel(QObject::tr("Enter mass in grains"));
QLineEdit* mass_lineEdit = new QLineEdit;
mass_label->setBuddy(mass_lineEdit);
QLabel* velocity_label = new QLabel(QObject::tr("Enter velocity in feet/sec"));
QLineEdit* velocity_lineEdit = new QLineEdit;
velocity_label->setBuddy(velocity_lineEdit);

QLabel* ke_label = new QLabel(0);

QWidget *window = new QWidget;
window->setWindowTitle("Bullet Velocity Calculator");

QObject::connect( mass_lineEdit, SIGNAL(textChanged(const QString &)), &ke, SLOT(pass_mass(const QString &)) );

QObject::connect(velocity_lineEdit, SIGNAL(textChanged(const QString &)), &ke, SLOT(pass_velocity(const QString &)));

QObject::connect(&ke, SIGNAL(pass_ke(const QString &)), ke_label, SLOT(setText(const QString &)));

//TEST CODE
QObject::connect( mass_lineEdit, SIGNAL(textChanged(const QString &)), ke_label, SLOT(setText(const QString &)));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(mass_lineEdit);
layout->addWidget(velocity_lineEdit);
window->setLayout(layout);

window->show();

return app.exec();
}

qmake -project, qmake, make yields the following errors:


ke.cpp: In member function ‘void ke::pass_mass(const QString&)’:
ke.cpp:58: error: invalid cast from type ‘const QString’ to type ‘char’
ke.cpp: In function ‘int main(int, char**)’:
ke.cpp:80: error: expected primary-expression before ‘,’ token
ke.cpp:82: error: expected primary-expression before ‘,’ token
ke.cpp:84: error: expected primary-expression before ‘,’ token
make: *** [ke.o] Error 1

I can't figure out where I'm going wrong with the signals/slots. Any ideas appreciated.

nix
3rd August 2009, 07:09
Hi,

Why you lose your time with char * ?

Signals you want to manage give QString as argument, so create slot with a QString as argument.

And then you should use :


mass.toFloat()

instead of


(float) atof ((char)mass)

Take a look to QString Documentation and use this type instead of the old char * .

Remark : Please use Tag Code, your post will be more readable.

wagmare
3rd August 2009, 07:14
ke.cpp: In member function ‘void ke:ass_mass(const QString&)’:
ke.cpp:58: error: invalid cast from type ‘const QString’ to type ‘char’

because in your code

void ke:ass_mass(const QString &mass)
{
float fp_mass;
fp_mass =(float) atof ((char)mass);
ke::calc_ke();
}


u cant type cast a QString like this (float) atof ((char)mass)

use

const char *str;
QString path;
QByteArray ba;
ba = path.toLatin1();
str = ba.data();

to convert QString to const char *
or


int val = path.toInt(&ok, 10);

to convert QString to int


then

QObject::connect( mass_lineEdit, SIGNAL(textChanged(const QString &)), &ke, SLOT(pass_mass(const QString &)) );

remove '&' in &ke
like
QObject::connect( mass_lineEdit, SIGNAL(textChanged(const QString &)), ke, SLOT(pass_mass(const QString &)) );