PDA

View Full Version : connect - how to make function recieving argument



pitterb
12th January 2009, 17:33
Hello,

I'm new in Qt programming. I've made a simple program:

calculator.h:


#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QtGui/QWidget>
#include "ui_calculator.h"

class Calculator : public QWidget
{
Q_OBJECT

public:
QString text();
Calculator(QWidget *parent = 0);
~Calculator();

private:
Ui::CalculatorClass ui;
public slots:
void add_digit_0(QString &text);
};

#endif // CALCULATOR_H


calculator.cpp:

#include "calculator.h"


Calculator::Calculator(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect( ui.button_0, SIGNAL( clicked() ), this, SLOT( add_digit_0(text) ) );
}

Calculator::~Calculator()
{

}
void Calculator::add_digit_0(QString &text)
{

text+='0';

ui.display->setText( text );
}


'dispaly' is QLineEdit and 'button_0' is a QPushButton object.

Compiler shows no errors, but when I run a program nothing happens (there is no text in display unit). I tested it a thousand times and it seems that SLOT( add_digit(text) ) does not want to recieve any arguments, because when I write function like...


void Calculator::add_digit_0()
{
ui.display->setText("TEXT");
}

...everything works.

Any Ideas how to make it work?

Thanks for help.

PS
Sorry for the topic name, but I have no idea what name should it be.

jpn
12th January 2009, 18:05
See QSignalMapper. Pay attention to the example in the detailed description.

You cannot put statements or variables inside SIGNAL() or SLOT() macros. See signals and slots for more details how they work.