Results 1 to 18 of 18

Thread: Accessing objects in different forms

  1. #1
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing objects in different forms

    you should be using signal and slot for this.

    Qt Code:
    1. connect(formAButton,SIGNAL(pressed()),formB,SLOT(enableButton()));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. formA->enableButton()
    To copy to clipboard, switch view to plain text mode 

    In formA

    Qt Code:
    1. void FormA::enableButton()
    2. {
    3. button->setEnabled(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing objects in different forms

    It doesn't have to be a button. It has to be a signal

    For example:
    Qt Code:
    1. class SomeClass : public QObject{
    2. Q_OBJECT
    3. public:
    4. SomeClass(QObject *p=0) : QObject(p){}
    5. void someMethod(){ if(something>10) emit someButtonEnableStateChanged(something==20); }
    6. signals:
    7. void someButtonEnableStateChanged(bool);
    8. };
    9. //...
    10. SomeClass *sc = new SomeClass(this);
    11. connect(sc, SIGNAL(someButtonEnableStateChanged(bool)), someotherForm->button78, SLOT(setEnabled(bool)));
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Accessing objects in different forms

    For this code
    Qt Code:
    1. formA->enableButton()
    To copy to clipboard, switch view to plain text mode 
    I'm getiing "error: expected unqualified-id before ‘->’ token"

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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 ?

  8. #8
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Accessing objects in different forms

    In povezava.ui.h I have:
    Qt Code:
    1. void Povezava::Connect_clicked()
    2. {
    3. .
    4. .
    5. .
    6. GlavnoO->enable_lista_tabel();
    7. close();
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    In GlavnoO.ui.h I have:
    Qt Code:
    1. void GlavnoO::enable_lista_tabel()
    2. {
    3. lista_tabel->setEnabled(true);
    4. };
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  11. #11
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Accessing objects in different forms

    I already have a slot defined:
    Qt Code:
    1. void GlavnoO::enable_lista_tabel()
    2. {
    3. lista_tabel->setEnabled(true);
    4. };
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Lebowski; 12th April 2006 at 12:55.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. emit someSignal( someParameter );
    To copy to clipboard, switch view to plain text mode 

    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

  13. #13
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. emit someSignal( someParameter );
    To copy to clipboard, switch view to plain text mode 
    Is it enough to just write that line of code in (for example) my povezava.ui.h file within some function (slot)?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  15. #15
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  17. The following user says thank you to jacek for this useful post:

    Lebowski (13th April 2006)

  18. #17
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default 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?

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing objects in different forms

    Yes. Connect needs pointers.

Similar Threads

  1. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  2. accessing form1's objects from form2
    By Philip_Anselmo in forum Newbie
    Replies: 5
    Last Post: 4th May 2006, 22:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.