Results 1 to 3 of 3

Thread: Problem Getting User-Defined Signals/Slots to Work

  1. #1
    Join Date
    Aug 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem Getting User-Defined Signals/Slots to Work

    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:ass_velocity(char* velocity)
    {
    float fp_velocity;
    fp_velocity =(float) atof (velocity);
    ke::calc_ke();
    }

    //void ke:ass_mass(char* mass)
    void ke:ass_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:ass_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.

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Problem Getting User-Defined Signals/Slots to Work

    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 :

    Qt Code:
    1. mass.toFloat()
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. (float) atof ((char)mass)
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem Getting User-Defined Signals/Slots to Work

    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
    Qt Code:
    1. void ke:ass_mass(const QString &mass)
    2. {
    3. float fp_mass;
    4. fp_mass =(float) atof ((char)mass);
    5. ke::calc_ke();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    use
    Qt Code:
    1. const char *str;
    2. QString path;
    3. ba = path.toLatin1();
    4. str = ba.data();
    To copy to clipboard, switch view to plain text mode 

    to convert QString to const char *
    or

    Qt Code:
    1. int val = path.toInt(&ok, 10);
    To copy to clipboard, switch view to plain text mode 

    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 &)) );
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Problem with plugin signals/slots
    By DiamonDogX in forum Qt Programming
    Replies: 8
    Last Post: 5th June 2009, 17:01
  2. Problem with QtBrowserPlugin - examples dont work.
    By robert_ugo in forum Installation and Deployment
    Replies: 0
    Last Post: 19th March 2009, 20:07
  3. Problem using SIGNALS/SLOTS
    By JimDaniel in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2007, 05:59
  4. Replies: 4
    Last Post: 26th June 2007, 20:19
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.