Signals and Slots questions
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?
Code:
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.
Re: Signals and Slots questions
The SLOT can only contain the function signature. It can not accept arguments.
Code:
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.
Re: Signals and Slots questions
Quote:
Originally Posted by
Janek
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?
Code:
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:
Code:
{
Q_OBJECT
public:
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:
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():
Code:
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:
Code:
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.
Re: Signals and Slots questions
Thank You very much, that clears up a lot, and of course I have to read a lot more about Qt ;)