PDA

View Full Version : Signals and Slots questions



Janek
29th December 2009, 18:00
1) I have 2 classes, first is MyForm, and second classTwo. MyForm have private object of type classTwo named objTwo - Is that a good idea? or better to create both of it in main.c as coexistent?

2) In MyForm constructor I try to connect SIGNAL from MyForm to SLOT from classTwo - it's not working, why?

connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX(widget.edit->text())));

As a workaround I declared new SLOT in MyForm which call objTwo.slotX() - this way it works, but Im sure this is bad practice, or isn't it?

The real problem comes when I want to connect SIGNAL from classTwo to MyForm class (in MyForm constructor). The only way I see is to cross include class headers, but I do want to make it properly, so obviously i miss something, as cross including is bad practice?

I need to send something to classTwo from MyForm and then wait for signal to read answer back.

Please help.

squidge
29th December 2009, 20:25
The SLOT can only contain the function signature. It can not accept arguments.



connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX()));


If you check the output window, you will notice an error message being printed with your version.

faldzip
29th December 2009, 20:42
1) I have 2 classes, first is MyForm, and second classTwo. MyForm have private object of type classTwo named objTwo - Is that a good idea? or better to create both of it in main.c as coexistent?

2) In MyForm constructor I try to connect SIGNAL from MyForm to SLOT from classTwo - it's not working, why?

connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotX(widget.edit->text())));


in SIGNAL() and SLOT() macros you give a signature of a method so the name of a method an type names of arguments, for example:
when you have class:


class A : public QObject
{
Q_OBJECT
public:
A(QObject *parent = 0);
public slots:
void myMethod(int a, const QString str);
};

and you want to connect some signal to the slot myMethod you have to write it like this:


SLOT(myMethod(int, QString))

as you see just names of types, so you can't pass any arguments to slots in a connect() statement.
whats more you can't connect signal with no arguments (like clicked()) with a slot which has arguments, in general, you can't connect signal with less arguments then slot which you connect to.

So in your situation solution looks like:
1. Make a slot with no arguemnts, lets say slotClicked()
2. connect with signal clicked():


connect(widget.buttonX, SIGNAL(clicked()), objTwo, SLOT(slotClicked()));
3. in slotClicked() do something with that thing what you want.
4. but I dont know why you have a slot in different class to process text from a lineEdit() in different class... maybe make that slot in MyForm like this:


void MyForm::slotClicked()
{
objTwo->doSomething(widget.edit->text());
}

that would be the easiest way.


P.S. Ohh and of course try reading some documentation and examples before trying to code, because all I said is written there with more examples and more clearly.

Janek
29th December 2009, 21:47
Thank You very much, that clears up a lot, and of course I have to read a lot more about Qt ;)