Results 1 to 4 of 4

Thread: When should I use signal/slot mechanism

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

    Default When should I use signal/slot mechanism

    Hi.
    I love signals/slots mechanism, and im afraid that i can overuse it(or maybe i don't understand it : P )
    For example, I have 2 objects. First one inherits from QPixmapGraphicsItem , second one is QGraphicsScene. When i click on a QPixmap... object , a want to display it in QGraphicsScene. And i have 2 possible solutions. Use signal/slots mechanism, or simply call a method.

    Here is my reimplementation of mousePressEvent

    Qt Code:
    1. connect(this,SIGNAL(projectNewPixmapInViewWidget(const QPixmap &)),mediator->getPToCardVDW(),SLOT(setNewPixmap(const QPixmap &))); //necessary or not ?
    2.  
    3. void cCardBase::mousePressEvent(QGraphicsSceneMouseEvent *event)
    4. {
    5. switch(event->button())
    6. {
    7. case Qt::LeftButton :
    8. //Should I emit signal here ?
    9. //emit projectNewPixmapInViewWidget(this->pixmap());
    10. //or simply
    11. //mediator->getPToCardVDW()->setNewPixmap(this->pixmap());
    12. break;
    13. ..............
    To copy to clipboard, switch view to plain text mode 

    And a slot in QGraphicsScene based object

    Qt Code:
    1. card = new QGraphicsPixmapItem(QPixmap(tr("./graphics/misc/empty.jpg")));
    2.  
    3. void cCardViewDockWidget::setNewPixmap(const QPixmap &pixmap)
    4. {
    5. card->setPixmap(pixmap);
    6. }//setNewPixmap()
    To copy to clipboard, switch view to plain text mode 

    And maybe some pictures








    So, in that case should I emit a signal or call a method ^^'

    Regards

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

    Default Re: When should I use signal/slot mechanism

    I think of signal/slots as being able to reuse existing code more easily. It then doesn't depend on other classes (and doesn't care about them). Other classes can connect to it's signals and decide what methods they want to call as a result of those signals being raised.

    If your class containing the signal already has a relationship with another class, and that class is the only one interested in that signal, then I see no reason to actually use signals/slots in that case. Call the method directly. It'll be faster too.

  3. The following user says thank you to squidge for this useful post:

    Bryku (10th February 2010)

  4. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When should I use signal/slot mechanism

    Even so, the relation ship might only go one way (as with model/view), so signals/slots may still be the way to go.

    In case of your example, I agree with fatjuicymole.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. The following user says thank you to franz for this useful post:

    Bryku (10th February 2010)

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

    Default Re: When should I use signal/slot mechanism

    thnx guys : )

Similar Threads

  1. slow signal-slot-mechanism
    By blm in forum Qt Programming
    Replies: 11
    Last Post: 28th October 2008, 18:10
  2. pointers behaviour in qt and specially signals/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2008, 20:34
  3. can a single statement work in signal/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2008, 09:24
  4. The threaded signal/slot mechanism
    By xbtl in forum Newbie
    Replies: 1
    Last Post: 30th March 2008, 01:07
  5. Replies: 4
    Last Post: 23rd January 2006, 17:51

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.