Results 1 to 20 of 20

Thread: Signals/slots between classes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

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

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

  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

    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.

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

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

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

  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

    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.