Results 1 to 18 of 18

Thread: Accessing objects in different forms

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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"

  6. #6
    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 ?

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

  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

    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.

  9. #9
    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.

  10. #10
    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.

  11. #11
    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

  12. #12
    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.

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.