Results 1 to 5 of 5

Thread: how to know which is the class that sends the signal in qt ?

  1. #1
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default how to know which is the class that sends the signal in qt ?

    My question is about how to know which is the class that sends the signal because what i want to do is specified to the class that has sent the signal
    is it possible to do that ?
    this is a prototype of what i intend to do

    Qt Code:
    1. if (signal comes from class 1)
    2. {do specified actions }
    3. else if (comes from class 2)
    4. {do something else }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Sliver_Twist

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to know which is the class that sends the signal in qt ?

    To answer yor question yes, use QObject::sender() and then cast it in to whatever class you want.

    But basing the logic on the sending object is not considered as a good design, so try not to base your logic on the sender.

    May be you could use QSignalMapper instead and base your logic on the formal slot parameter.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to know which is the class that sends the signal in qt ?

    i know the method QSignalMapper but don't konw how to use it with this code
    Qt Code:
    1. connect( ui->welcome, SIGNAL(changeStackedWidgetIndex(int)),
    2. ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    3. connect( ui->credentials, SIGNAL(changeStackedWidgetIndex(int)),
    4. ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    5. connect( ui->config, SIGNAL(changeStackedWidgetIndex(int)),
    6. ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    7. connect( ui->syncwindow, SIGNAL(changeStackedWidgetIndex(int)),
    8. ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    9. connect( ui->loaduser, SIGNAL(changeStackedWidgetIndex(int)),
    10. ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    To copy to clipboard, switch view to plain text mode 
    Sliver_Twist

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to know which is the class that sends the signal in qt ?

    Why would you want to use it with this code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to know which is the class that sends the signal in qt ?

    Qt Code:
    1. QSignalMapper * signalMapper = QSignalMapper(this);
    2.  
    3. connect(ui->welcome, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
    4. signalMapper->setMapping(ui->welcome, 0);
    5.  
    6. connect(ui->credentials, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
    7. signalMapper->setMapping(ui->credentials, 1);
    8.  
    9. connect(ui->config, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
    10. signalMapper->setMapping(ui->config, 2);
    11.  
    12. connect(ui->syncwindow, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
    13. signalMapper->setMapping(ui->syncwindow, 3);
    14.  
    15. connect(ui->loaduser, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
    16. signalMapper->setMapping(ui->loaduser, 4);
    17.  
    18. connect(signalMapper, SIGNAL(mapped(int)), this, SIGNAL(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    sliverTwist (23rd April 2013)

Similar Threads

  1. how to get a class's signal list?
    By wshn13 in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2012, 04:52
  2. QThread sends signal to main thread immediatly
    By BIllNo123 in forum Newbie
    Replies: 7
    Last Post: 27th August 2010, 10:32
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. want to use signal in the class itself..
    By salmanmanekia in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2008, 10:06
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Tags for this Thread

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.