Results 1 to 20 of 20

Thread: Signals/slots between classes

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Signals/slots between classes

    I'm stuck at this.
    I want to connect a slot, dialogAccepted(); , to a QDialogButtonBox "buttonBox" ,accepted() signal. My slot is in the class Widget and the signal comes from class Dialog(which is a public QDialog).
    widget.cpp:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include "dialog.h"
    6.  
    7. namespace Ui
    8. {
    9. class Widget;
    10. }
    11.  
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Widget(QWidget *parent = 0);
    18. ~Widget();
    19.  
    20.  
    21. private:
    22. Ui::Widget *ui;
    23. Dialog dialog;
    24.  
    25.  
    26.  
    27. private slots:
    28. void dialogAccepted();
    29. };
    30.  
    31. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signals/slots between classes

    Connect your signal before you show your dialog:
    Qt Code:
    1. connect(dialog1, SIGNAL(whatever), this, SLOT(yourSlot));
    2. dialog1->show();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Well I get the following error:
    Qt Code:
    1. F:/Documents/QT Projects/ANClock/widget.cpp:21: error: no matching function for call to `Widget::connect(Dialog&, const char*, Widget* const, const char*)'
    To copy to clipboard, switch view to plain text mode 
    This is the connect line:
    Qt Code:
    1. connect(dialog, SIGNAL(accepted()),
    2. this, SLOT(tempUsage()));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    try

    &dialog

  5. #5
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals/slots between classes

    Actually it looks like you put variable names in connect while you should connect only prototypes. Something like:

    Qt Code:
    1. class myCustomWidget1: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. myCustomWidget1(QObject * parent = 0);
    8. ~myCustomWidget1();
    9.  
    10. signal:
    11.  
    12. drawMyPicture(int x, int y, const QPicture& pict);
    13.  
    14. };
    15.  
    16. class myCustomWidget2: public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21.  
    22. myCustomWidget2(QObject * parent = 0);
    23. ~myCustomWidget2();
    24.  
    25. public slots:
    26.  
    27. void drawSomething(int x, int y, const QPicture& pict);
    28.  
    29. };
    30.  
    31. // Somewhere in your code
    32. ...
    33. myCustomWidget1* widget1 = new myCustomWidget1(this);
    34. myCustomWidget2* widget2 = new myCustomWidget2(this);
    35.  
    36. connect(widget1, SIGNAL(drawMyPicture(int, int, const QPicture&)),
    37. widget2, SLOT(drawSomething(int, int, const QPicture&)));
    To copy to clipboard, switch view to plain text mode 

    Hope that helps.
    Last edited by Tanuki-no Torigava; 22nd November 2009 at 18:17.

  6. #6
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals/slots between classes

    PS.
    Qt Code:
    1. connect(classOne, SIGNAL(classOneSignal(int x)), classTwo, SLOT(classTwoSlot(int x)));
    To copy to clipboard, switch view to plain text mode 

    will not produce any error but will not work while this one

    Qt Code:
    1. connect(classOne, SIGNAL(classOneSignal(int)), classTwo, SLOT(classTwoSlot(int)));
    To copy to clipboard, switch view to plain text mode 

    works fine.

  7. #7
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals/slots between classes

    And just notice that you close the namespace Ui. So your declaration is incorrect. It should be
    Qt Code:
    1. class Ui::Widget: public QWidget;
    To copy to clipboard, switch view to plain text mode 
    Also it is a bit strange declaration. Is it designer-made class?

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by Tanuki-no Torigava View Post
    PS.
    Qt Code:
    1. connect(classOne, SIGNAL(classOneSignal(int x)), classTwo, SLOT(classTwoSlot(int x)));
    To copy to clipboard, switch view to plain text mode 
    will not produce any error but will not work while this one
    Actually, that WOULD produce an error, but at runtime when connect() is called, rather than compile time. You'll get an error stating that there's no such signal.

  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by fatjuicymole View Post
    try

    &dialog
    That's exactly what I did! I don't know why that works??

  10. #10
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by Tanuki-no Torigava View Post
    And just notice that you close the namespace Ui. So your declaration is incorrect. It should be
    Qt Code:
    1. class Ui::Widget: public QWidget;
    To copy to clipboard, switch view to plain text mode 
    Also it is a bit strange declaration. Is it designer-made class?
    That's code generated from QtCreator.

  11. #11
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals/slots between classes

    Actually, that WOULD produce an error, but at runtime when connect() is called, rather than compile time. You'll get an error stating that there's no such signal.
    I mean compile time error. Actually I do prefer to see compile time error instead of looking through the console. So you are half right. Run time error != build time error.

    About dialog. Looks like you draw in designer, right? if so you need multiple inheritance where the first class is exactly the base class of your widget (QDialog, QScrollArea, etc) and second is your Ui based class.

    Qt Code:
    1. // Login details dialog
    2. class LoginDetails : public QDialog, public Ui::SyncDialog
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7.  
    8. LoginDetails(QWidget * parent = 0, Qt::WindowFlags f = 0);
    9. ~LoginDetails();
    10. }
    11.  
    12. // And in the constructor of this class very urgent to keep the initialization sequence and don't forget to initialize Ui as well
    13.  
    14. LoginDetails::LoginDetails(
    15. QWidget* parent,
    16. Qt::WindowFlags flags)
    17. : QDialog(parent, flags)
    18. {
    19. setupUi(this); // should go first!!!
    20.  
    21. // Your init
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Hope that helps

  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by Tanuki-no Torigava View Post
    I mean compile time error. Actually I do prefer to see compile time error instead of looking through the console. So you are half right. Run time error != build time error.
    I thought I clearly stated that in my post:
    Actually, that WOULD produce an error, but at runtime when connect() is called, rather than compile time. You'll get an error stating that there's no such signal.
    It was a "Just for your information". I know it's not a compile time error, but you might not have known about it and thought it was useful. Considering connect() takes a char *, it's impossible for it to know at compile time that a signal doesn't exist. You can put whatever you like for the signal and slot and it'll be happy at compile time.

  13. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by been_1990 View Post
    That's exactly what I did! I don't know why that works??
    It works because Dialog is created on the stack rather than the heap. It would be "Dialog *" if it was created on the heap, and then you'd need to use "dialog = new Dialog()" somewhere too. You could then also use connect(dialog, instead of connect(&dialog,

  14. #14
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by been_1990 View Post
    Well I get the following error:
    Qt Code:
    1. F:/Documents/QT Projects/ANClock/widget.cpp:21: error: no matching function for call to `Widget::connect(Dialog&, const char*, Widget* const, const char*)'
    To copy to clipboard, switch view to plain text mode 
    This is the connect line:
    Qt Code:
    1. connect(dialog, SIGNAL(accepted()),
    2. this, SLOT(tempUsage()));
    To copy to clipboard, switch view to plain text mode 
    This error says that there is no such 'connect' method? why? Because every connect needs pointers to an objects, not references or anything else. And as you see in your compile error:
    Widget::connect(Dialog&, ...
    the compiler is right in this case (as always) because it says you want to pass a reference to your dialog, not a pointer, and there is no suitable method.

    So because you have in your header file:
    Qt Code:
    1. Dialog dialog;
    To copy to clipboard, switch view to plain text mode 
    You have to use C/C++ '&' operator to get the pointer (address):
    Qt Code:
    1. connect(&dialog,...
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  15. #15
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by Tanuki-no Torigava View Post
    I mean compile time error. Actually I do prefer to see compile time error instead of looking through the console. So you are half right. Run time error != build time error.

    About dialog. Looks like you draw in designer, right? if so you need multiple inheritance where the first class is exactly the base class of your widget (QDialog, QScrollArea, etc) and second is your Ui based class.

    Qt Code:
    1. // Login details dialog
    2. class LoginDetails : public QDialog, public Ui::SyncDialog
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7.  
    8. LoginDetails(QWidget * parent = 0, Qt::WindowFlags f = 0);
    9. ~LoginDetails();
    10. }
    11.  
    12. // And in the constructor of this class very urgent to keep the initialization sequence and don't forget to initialize Ui as well
    13.  
    14. LoginDetails::LoginDetails(
    15. QWidget* parent,
    16. Qt::WindowFlags flags)
    17. : QDialog(parent, flags)
    18. {
    19. setupUi(this); // should go first!!!
    20.  
    21. // Your init
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Hope that helps
    You lost me here... Let me see if I understood right, I should use Multiple Inheritance instead of Single Inheritance? The code generated by QtCreator seems to be single inheritance, I never changed that, but is that necessary?
    Yes I use the designer to create the forms..

  16. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Multiple Inheritance is generally a bad idea. You should use single inheritance unless you have a good reason. This way all your ui objects are behind the 'ui' object rather than exposed and polluting the same class as your implementation. Multiple inheritance also makes it more messy if another class includes your classes header file.

  17. #17
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by fatjuicymole View Post
    It works because Dialog is created on the stack rather than the heap. It would be "Dialog *" if it was created on the heap, and then you'd need to use "dialog = new Dialog()" somewhere too. You could then also use connect(dialog, instead of connect(&dialog,
    Heap? Stack?

  18. #18
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

  19. #19
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Quote Originally Posted by fatjuicymole View Post
    Multiple Inheritance is generally a bad idea. You should use single inheritance unless you have a good reason. This way all your ui objects are behind the 'ui' object rather than exposed and polluting the same class as your implementation. Multiple inheritance also makes it more messy if another class includes your classes header file.
    I read at QT archives that QT Creator uses single inheritance because that compiles much quicker and etc, etc ...
    But there's this QT beginners tutorial that says multiple inheritance is the preferred way to write Qt applications.
    Whatever.... So single inheritance is better?

  20. #20
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals/slots between classes

    Use whichever you prefer. I prefer single inheritance as it keep the ui private, but some prefer multiple so they can access the ui directly.

Similar Threads

  1. 2 classes header file inclusion probleml !!
    By sujan.dasmahapatra in forum General Programming
    Replies: 4
    Last Post: 15th October 2009, 19:31
  2. Replies: 2
    Last Post: 13th March 2009, 11:23
  3. Professional Classes & Objects Structure
    By webstylemedia in forum Newbie
    Replies: 4
    Last Post: 4th August 2008, 10:50
  4. Adding nonQt classes to QtApplication
    By codebehind in forum Newbie
    Replies: 11
    Last Post: 23rd April 2007, 21:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.