Accessing objects in different forms
Hello. I have a very basic question.
Let's say I created 2 forms in designer: FormA and FormB. I've added push button named Button into FormA and set it to disbale by default. Now I would like to enable it from FormB. How do I do that? If it was in same form it would be simply Button->setEnabled (TRUE);
How do I do it if it's in different form?
Thanx in advance.
Re: Accessing objects in different forms
you should be using signal and slot for this.
Code:
connect(formAButton,SIGNAL(pressed()),formB,SLOT(enableButton()));
Re: Accessing objects in different forms
But I don't want to make another button just to change one property. Is there any other way?
Re: Accessing objects in different forms
In that case u need to have a reference of your formA in formB and then call a function in formB from formA which will actually enable the button.
Example:
In formB
Code:
formA->enableButton()
In formA
Code:
void FormA::enableButton()
{
button->setEnabled(true);
}
Re: Accessing objects in different forms
It doesn't have to be a button. It has to be a signal :)
For example:
Code:
Q_OBJECT
public:
void someMethod(){ if(something>10) emit someButtonEnableStateChanged(something==20); }
signals:
void someButtonEnableStateChanged(bool);
};
//...
SomeClass *sc = new SomeClass(this);
connect(sc, SIGNAL(someButtonEnableStateChanged(bool)), someotherForm->button78, SLOT(setEnabled(bool)));
Better yet, that class which owns the button should have some slot which will handle those changes and the "source" class should just emit signals like "myStateChanged" and not "pleaseChangeTheStateOfSomeOfTheButtonsOfYours ".
Of course another way is to ignore signals and slots and store a pointer to that button in the class which is to manipulate it.
Re: Accessing objects in different forms
For this code
Code:
formA->enableButton()
I'm getiing "error: expected unqualified-id before ‘->’ token"
Re: Accessing objects in different forms
I suggest you use the method wysota has suggested. I think it a cleaner way to accomplish your task.
If you still want to use the other way and know what the error is, can you please show us your code ?
Re: Accessing objects in different forms
In povezava.ui.h I have:
Code:
void Povezava::Connect_clicked()
{
.
.
.
GlavnoO->enable_lista_tabel();
close();
};
In GlavnoO.ui.h I have:
Code:
void GlavnoO::enable_lista_tabel()
{
lista_tabel->setEnabled(true);
};
Re: Accessing objects in different forms
I think I know where the problem is: GlavnoO is class while I would need to call object of that class. The problem is that I have that object defined in main.cpp and I don't know how to include it.
Re: Accessing objects in different forms
Quote:
Originally Posted by Lebowski
The problem is that I have that object defined in main.cpp and I don't know how to include it.
You can use signals and slots mechanism to connect those two objects.
Re: Accessing objects in different forms
I already have a slot defined:
Code:
void GlavnoO::enable_lista_tabel()
{
lista_tabel->setEnabled(true);
};
How do I create signal in designer? Just adding it in Object Explorer doesn't generate anything really. And adding it to .h file in ".ui/" folder won't work cause all changes there are being ignored.
Re: Accessing objects in different forms
Quote:
Originally Posted by Lebowski
How do I create signal in designer? Just adding it in Object Explorer doesn't generate anything really.
You don't implement signals --- it's enough to declare them within class definition (the rest will be handled by moc).
Then just write:
Code:
emit someSignal( someParameter );
Quote:
Originally Posted by Lebowski
And adding it to .h file in ".ui/" folder won't work cause all changes there are being ignored.
Then try the subclassing approach: http://doc.trolltech.com/3.3/designer-manual-6.html
Re: Accessing objects in different forms
Quote:
Originally Posted by jacek
You don't implement signals --- it's enough to declare them within class definition (the rest will be handled by moc).
Then just write:
Code:
emit someSignal( someParameter );
Is it enough to just write that line of code in (for example) my povezava.ui.h file within some function (slot)?
Re: Accessing objects in different forms
Quote:
Originally Posted by Lebowski
Is it enough to just write that line of code in (for example) my povezava.ui.h file within some function (slot)?
Yes, if you have declared that signal in the Designer. Of course you will also have to make the connection.
Re: Accessing objects in different forms
Signal and slot are now working ok. Thanx for help.
Where do I make connection though? In main.cpp? Cause QT Designer's wizard only allows creation of connections within same form.
Re: Accessing objects in different forms
Quote:
Originally Posted by Lebowski
Where do I make connection though? In main.cpp? Cause QT Designer's wizard only allows creation of connections within same form.
You will have to create the connection by hand. The best place to do it is the one where you have an easy access to both objects, in your case this might be the main() function.
Re: Accessing objects in different forms
It works!!! Thanx a lot :)
Btw in connect I had to use '&' character in front of object names. What does that mean? Does it mean memory address of that object maybe?
Re: Accessing objects in different forms
Yes. Connect needs pointers.