Results 1 to 7 of 7

Thread: how can I know which object caused a specific event ?

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default how can I know which object caused a specific event ?

    Suppose I have 3 sliders on my form, they all so similar jobs, but with a slight difference based on which category the belong .
    So one way is to create lets say 3 event handlers for their slider_move() event and then go on.
    the other way which I am interested in, is create one event handler, and make all of these 3 use that.

    so the signals must have the objects emitted as well, so that in the slot we can differentiate between multiple different objects

    How is this second one achieved ?
    how can I get the sender in this scenario?
    I checked the signal doesn't have a QObject parameter, and I don't want to re-implement the widgets each time I need such capability)?

    Thanks in advance
    Last edited by Hossein; 21st October 2015 at 07:41.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how can I know which object caused a specific event ?

    Each handler (the "slot" procedure] can call sender() and get a pointer to the object which emitted the just processed signal. sender() returns a QObject * which you dynamic_cast to the real sender class (for example, QSlider). Comparing the pointer to addresses of the possible senders, you get the object which signal is processed. Note: calling sender() outside a handler (for example, in a procedure called by the handler - this is "outside, too) returns nullptr.

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

    Hossein (21st October 2015)

  4. #3
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can I know which object caused a specific event ?

    Quote Originally Posted by Radek View Post
    Each handler (the "slot" procedure] can call sender() and get a pointer to the object which emitted the just processed signal. sender() returns a QObject * which you dynamic_cast to the real sender class (for example, QSlider). Comparing the pointer to addresses of the possible senders, you get the object which signal is processed. Note: calling sender() outside a handler (for example, in a procedure called by the handler - this is "outside, too) returns nullptr.
    Thanks this is what I came up with so far:
    In form load:
    Qt Code:
    1. connect(ui->sliderXaxisMin,SIGNAL(sliderMoved(int)),this,SLOT(on_sliders_sliderMoved(int)));
    2. connect(ui->sliderXaxisMax,SIGNAL(sliderMoved(int)),this,SLOT(on_sliders_sliderMoved(int)));
    3. connect(ui->sliderYaxisMin,SIGNAL(sliderMoved(int)),this,SLOT(on_sliders_sliderMoved(int)));
    4. connect(ui->sliderYaxisMax,SIGNAL(sliderMoved(int)),this,SLOT(on_sliders_sliderMoved(int)));
    To copy to clipboard, switch view to plain text mode 
    and this is the handler:
    Qt Code:
    1. void frmCustomNetwork::on_sliders_sliderMoved(int position)
    2. {
    3. QSlider * slider = qobject_cast<QSlider*>(sender());
    4. double value = 0;
    5. double divisor = 0;
    6.  
    7. if( slider == ui->sliderXaxisMin)
    8. {
    9. value = ui->txtXaxisLower->text().toDouble();
    10. divisor = GetDivisior(value);
    11. ui->txtXaxisLower->setText(QString::number(position/divisor));
    12. }
    13. else if ( slider == ui->sliderXaxisMax)
    14. {
    15. value = ui->txtXaxisUpper->text().toDouble();
    16. divisor = GetDivisior(value);
    17. ui->txtXaxisUpper->setText(QString::number(position/divisor));
    18. }
    19. else if ( slider == ui->sliderYaxisMin)
    20. {
    21. value = ui->txtYaxisLower->text().toDouble();
    22. divisor = GetDivisior(value);
    23. QMessageBox::information(this,"error",QString::number(divisor));
    24. ui->txtYaxisLower->setText(QString::number(position/divisor));
    25. }
    26. else if ( slider == ui->sliderYaxisMax)
    27. {
    28. value = ui->txtYaxisUpper->text().toDouble();
    29. divisor = GetDivisior(value);
    30. ui->txtYaxisUpper->setText(QString::number(position/divisor));
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 


    but the catch is when ever I run the application I get this error :
    Qt Code:
    1. QMetaObject::connectSlotsByName: No matching signal for on_sliders_sliderMoved(int)
    To copy to clipboard, switch view to plain text mode 

    whats wrong with?!

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can I know which object caused a specific event ?


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

    Hossein (21st October 2015)

  7. #5
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can I know which object caused a specific event ?

    Quote Originally Posted by Lesiok View Post
    So I have to change the eventHanlders name so that It is not like the pattern shown there .
    Thanks a gazillion

  8. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how can I know which object caused a specific event ?

    For the record, although the sender()-based solution works, it is considered bad pratice because it breaks modularity.

    You can use the QSignalMapper class to "forward" a signal without parameters to a signal with a parameter identifying the sender. In your case this means that you lose the int parameter of the sliderMoved(int) signal, but you can recover it with QAbstractSlider::sliderPosition().

    Another solution consists in defining a distinct tiny slot for each slider that just calls a common handler with a pointer to the slider as argument.

  9. The following user says thank you to yeye_olive for this useful post:

    Hossein (21st October 2015)

  10. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how can I know which object caused a specific event ?

    Uff, this seems to be some kind of "VS" trick. The logic of the handler seems to be okay, sliderMoved(int) is a legal QSlider signal (so what "no matching signal?), the name of the slot handler is decided by you. No "special form" is prescribed. Sure, frmCustomNetwork must be a Q_OBJECT and the slot handler should be declared in the frmCustomNetwork class header in the public, private or protected slots section. The connect() calls seem to be okay. Supposing the handler is declared in frmCustomNetwork, the code should run.
    In the Creator, you get useful hints when writing connect(). Once you type "SIGNAL(", you get available signals of the signaling class. You should see "sliderMoved(int)" on the list. Once you type "SLOT(", you get available slots of the target class. You should see your signal handler on the list. Selecting from the lists avoids most of errors.

  11. The following user says thank you to Radek for this useful post:

    Hossein (21st October 2015)

Similar Threads

  1. Pass event (QWheelEvent) to a specific widget
    By stefanadelbert in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2021, 03:10
  2. How to know the signal which caused a qstate transition
    By ClintEastwood in forum Qt Programming
    Replies: 6
    Last Post: 24th April 2013, 15:27
  3. Style3sheet problem, QTreeView for specific object
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 21:15
  4. Signal to specific object slot
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 27th December 2007, 15:51
  5. Crash caused by QVariant (mis)use
    By mclark in forum Newbie
    Replies: 2
    Last Post: 31st October 2006, 15:05

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.