Results 1 to 3 of 3

Thread: Simple QObject::connect question

  1. #1
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Simple QObject::connect question

    Hi. I'm new to QT and I have a good-programming-style-type problem. I want to connect 2 objects. Until now , i wrote some simple applications , and connected objects that one contains another. For example:

    Qt Code:
    1. class c : public QWidget
    2. {
    3. private:
    4. QPushButton *button;
    5. public:
    6. c()
    7. {
    8. ...
    9. connect(button,SIGNAL(signal()),this,SLOT(slot()));
    10. ...
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 

    And it was simple and easy.

    But what if i want to connect to objects that none of them is containing another ? In "connect" i have to pass pointers to sender and reciever. Should I pass the pointer to another object in a constructor ? For example:
    Qt Code:
    1. class ObjectA : public QWidget
    2. {
    3. ...
    4. public slots:
    5. void slot();
    6. ...
    7. };
    8.  
    9. class ObjectB : public QWidget
    10. {
    11. ...
    12. signals:
    13. void signal();
    14. public:
    15. ObjectB(ObjectA *p)
    16. {
    17. ...
    18. connect(this,SIGNAL(signal()),p,SLOT(slot()));
    19. ...
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 

    Is that a good way to do that ? It works, but I believe something is wrong with that. What if i want to connect one object with twenty anothers , so i neet do pass 20 pointers in a constructor ?

  2. #2
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple QObject::connect question

    Find out which higher level class in you design manages or is closest related to ObjectB and let that class code (constructor maybe?) call connect(objectB, SIGNAL(signal()), objectA, SLOT(slot())).
    No need to pass 20 pointers in ctor

    However if ObjectA itself is closely related to ObjectB (for example if you store reference to ObjectA as ObjectB member) then you might as well do as in your example...

  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Simple QObject::connect question

    You can also connect in the next outer containing entity. For example you can connect in the main-method two object that are created there:

    Qt Code:
    1. int main(int argc, char** argv) {
    2. QApplication app(argc, argv);
    3. QPushButton button("Quit");
    4. QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
    5. button.show();
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. Simple question about QtConcurrent
    By vcp in forum Qt Programming
    Replies: 6
    Last Post: 15th September 2009, 21:14
  2. Simple namespace question
    By JPNaude in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2009, 09:31
  3. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  4. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

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.