Results 1 to 13 of 13

Thread: How to connect a signal from class a to a slot in a class b?

  1. #1
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to connect a signal from class a to a slot in a class b?

    Hello
    i have this connection:
    Qt Code:
    1. //Class A
    2. connect( treeWidgetA, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( doSomething(const QModelIndex& ) ) );
    To copy to clipboard, switch view to plain text mode 

    I have to move the doSomething slot in B class too, and i want to connect the B's class treeWidget signal clicked, to the slot doSomething that already exists in class A.is it correct?

    Qt Code:
    1. //Class B
    2. AClass *aClass;//is it correct?
    3. connect( treeWidgetB, SIGNAL( clicked( const QModelIndex& ) ),aClass , SLOT( doSomething(const QModelIndex& ) ) );
    To copy to clipboard, switch view to plain text mode 

    This approach is not working..am a declaring Aclass wrong in the B's class code?

    *the slot doSomething is declared as public slot in class A*

    Thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    you must use operator new for creating pointer to an object, so it should look like this
    Qt Code:
    1. AClass *aClass = new AClass();
    2. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    i've tryed that too.. (declared the aClass within B's Class constructor).Never goes into the slot in the class A..however there are no complaints in the "compiler such as QObject::connect:No such slot etc etc"..just never gets into the slot to execute the code..can't undertand whats going on..

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    did you add Q_OBJECT macro to both classes?

  5. #5
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    yes in both headers..

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    ok. can you prepare compilable example and post it? (if yes, can you put it in archive)

  7. #7
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    ok did it..
    i have a slot in the first form which writes text to the editbox and i just want to use the same slot in form2 to change the editbox in form2
    Attached Files Attached Files

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    so, the problem is in ctor of Form2, you create a new instance of Form
    Qt Code:
    1. ...
    2. Form *frm1=new Form;
    3. ...
    To copy to clipboard, switch view to plain text mode 

    but don't show it.
    so, the right code is
    Qt Code:
    1. ...
    2. Form *frm1=new Form;
    3. frm1->show();
    4. ...
    To copy to clipboard, switch view to plain text mode 
    but, I strongly suggest you to review your code and make it more clearer.

  9. #9
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    no the problem is not the form.The second form shows up correct..The problem is with the slot testSLot() implemented in Form.cpp, that i want to use it and in form2.cpp

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    I guess u want that from Form2 pushbutton, u want to send a signal to the parent form.
    Am i right ?
    In that case u will have to connect the signal slot in OpenForm2() function.

    connect ( frm->pushButton, SIGNAL ( clicked() ), this, SLOT ( testSlot() ) );

  11. #11
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    That's right..now when i click the button in form2 the slot sets the text in form1..however i need a way to use the testSlot to set the text in form2 when i click form2's pushbutton and not form1.Basically i need one slot to do the same thing in two different forms..Is that possible since the components in the 2 forms have the same names?

  12. #12
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to connect a signal from class a to a slot in a class b?

    Then write the same function in Form2 class.

    What you ask is not possible. Its like 2 classes sharing same function !! Is that possible ??

  13. #13
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to connect a signal from class a to a slot in a class b?

    Thats the way i had it..i had 4 functions in A class and exactly the same functions in class B.But with this i had 500 lines of code in class A repeated in class B and i was wondering if there is a way to write the code in one class and use the same code in another class..so nevermind i'll write the same code in the other class too..

    Thanks!

Similar Threads

  1. Signal in base class Slot in Subclass
    By csvivek in forum Newbie
    Replies: 7
    Last Post: 30th March 2008, 16:59
  2. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  3. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  4. Replies: 2
    Last Post: 17th May 2006, 21:01
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.