PDA

View Full Version : Accessing objects in different forms



Lebowski
7th April 2006, 12:09
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.

munna
7th April 2006, 12:16
you should be using signal and slot for this.



connect(formAButton,SIGNAL(pressed()),formB,SLOT(e nableButton()));

Lebowski
7th April 2006, 12:18
But I don't want to make another button just to change one property. Is there any other way?

munna
7th April 2006, 12:27
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



formA->enableButton()


In formA



void FormA::enableButton()
{
button->setEnabled(true);
}

wysota
7th April 2006, 12:29
It doesn't have to be a button. It has to be a signal :)

For example:

class SomeClass : public QObject{
Q_OBJECT
public:
SomeClass(QObject *p=0) : QObject(p){}
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.

Lebowski
7th April 2006, 12:43
For this code


formA->enableButton()

I'm getiing "error: expected unqualified-id before ‘->’ token"

munna
7th April 2006, 12:47
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 ?

Lebowski
11th April 2006, 10:53
In povezava.ui.h I have:

void Povezava::Connect_clicked()
{
.
.
.
GlavnoO->enable_lista_tabel();
close();

};


In GlavnoO.ui.h I have:


void GlavnoO::enable_lista_tabel()
{
lista_tabel->setEnabled(true);
};

Lebowski
11th April 2006, 11:03
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.

jacek
11th April 2006, 15:30
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.

Lebowski
12th April 2006, 12:51
I already have a slot defined:

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.

jacek
12th April 2006, 13:13
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:
emit someSignal( someParameter );


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

Lebowski
12th April 2006, 13:19
You don't implement signals --- it's enough to declare them within class definition (the rest will be handled by moc).

Then just write:
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)?

jacek
12th April 2006, 13:25
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.

Lebowski
12th April 2006, 14:07
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.

jacek
12th April 2006, 14:10
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.

Lebowski
13th April 2006, 09:52
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?

wysota
13th April 2006, 11:20
Yes. Connect needs pointers.