Results 1 to 13 of 13

Thread: how to know which signal is called a certain slot?

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default how to know which signal is called a certain slot?

    Hi,
    There are 5 Classes and from each class I have 4 objects. When each object (each of these 20 objects) change , signal of that is emitted and calls a certain slot. for example:
    Qt Code:
    1. connect(obj1_0, SIGNAL(sig1()), this, SLOT(myslot()));//obj1_0 is a type of Class1
    2. connect(obj1_1, SIGNAL(sig1()), this, SLOT(myslot()));
    3. connect(obj1_2, SIGNAL(sig1()), this, SLOT(myslot()));
    4. connect(obj1_3, SIGNAL(sig1()), this, SLOT(myslot()));
    5.  
    6. connect(obj2_0, SIGNAL(sig2()), this, SLOT(myslot()));//obj2_0 is a type of Class2 deference from Class1
    7. connect(obj2_1, SIGNAL(sig2()), this, SLOT(myslot()));
    8. connect(obj2_2, SIGNAL(sig2()), this, SLOT(myslot()));
    9. connect(obj2_3, SIGNAL(sig2()), this, SLOT(myslot()));
    10. .
    11. .
    12. .
    13. .
    14. connect(obj5_0, SIGNAL(sig5()), this, SLOT(myslot()));//obj5_0 is a type of Class5 deference from Class1, Class2, Class3, Class4
    15. connect(obj5_1, SIGNAL(sig5()), this, SLOT(myslot()));
    16. connect(obj5_2, SIGNAL(sig5()), this, SLOT(myslot()));
    17. connect(obj5_3, SIGNAL(sig5()), this, SLOT(myslot()));
    To copy to clipboard, switch view to plain text mode 

    I am using QSignalMapper for this and defined 5 objects of this class for all of Class1,...,Class5 like obj1_signalmapper, obj2_signalmapper,..., obj5_signalmapper by a "int id". But I need to know which object and from which kind of class send signal(for example know first: obj1_2 send and seccond:from type of Class1). I can know obj id but I can not know this object is for which class because obj1_3 has the same "id" that obj5_3 has.

    Thanks for any help.
    Last edited by Alex22; 10th December 2015 at 20:42.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Inside your slot, QObject::sender() will tell you which instance of which object sent the signal. If Class1 - Class5 are different classes, then you will have to do a qobject_cast< Class1 * >( sender() ) (and so forth) and check for a non-NULL pointer to first see what type of class sent the signal.

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Thanks but this dos not work and for all senders give NULL. it could be because of using obj1_0 = new Class1 in a ForLoop when i=0 and obj1_1 is the same when i=1, .... In other word, obj1_0, obj1_2,..... are defined in loop like this:
    Qt Code:
    1. for(int i=0; i<4; i++)
    2. {
    3. obj1 = new Class1//this produces 4 objects from Class1 (obj1_0, obj1_1,..., obj1_4)
    4. obj2 = new Class2//this produces 4 objects from Class1 (obj1_0, obj1_1,..., obj1_4)
    5. obj3 = new Class3
    6. obj4 = new Class4
    7. obj5 = new Class5
    8. //here i used QSignalMapper and other things
    9. }
    To copy to clipboard, switch view to plain text mode 

    thanks for any help

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    //this produces 4 objects from Class1 (obj1_0, obj1_1,..., obj1_4)
    No it doesn't. It produces 4 instances of Class1, all of which are assigned to the single variable obj1. This code won't even compile, and even if it did, each time through the loop you are reassigning obj1 - obj5 to new instances of the classes and the values from the previous time through the loop are lost (and result in a memory leak).

    Why don't you post your real code instead of making something up so we can see what you actually might be doing wrong instead of having to guess?

  5. #5
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Quote Originally Posted by d_stranz View Post
    No it doesn't. It produces 4 instances of Class1, all of which are assigned to the single variable obj1. This code won't even compile, and even if it did, each time through the loop you are reassigning obj1 - obj5 to new instances of the classes
    thanks. this is an example in Qt assistance for QSignalMapper:
    Qt Code:
    1. ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. signalMapper = new QSignalMapper(this);
    5.  
    6. QGridLayout *gridLayout = new QGridLayout;
    7. for (int i = 0; i < texts.size(); ++i) {
    8. QPushButton *button = new QPushButton(texts[i]);//here in a loop Qt uses that I used too
    9. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    10. signalMapper->setMapping(button, texts[i]);
    11. gridLayout->addWidget(button, i / 3, i % 3);
    12. }
    13.  
    14. connect(signalMapper, SIGNAL(mapped(QString)),
    15. this, SIGNAL(clicked(QString)));
    16.  
    17. setLayout(gridLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

    the values from the previous time through the loop are lost
    Because of using QSignalmapper it could be saved with an id.
    thanks for any help

  6. #6
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    The difference is in the example the pushbuttons are added to a grid layout which takes ownership of the button. Hence no memory leak. d_stranz was pointing at your rough sketch code where that part is missing..

  7. #7
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Quote Originally Posted by Vikram.Saralaya View Post
    The difference is in the example the pushbuttons are added to a grid layout which takes ownership of the button. Hence no memory leak. d_stranz was pointing at your rough sketch code where that part is missing..
    thanks, my code has QgridLayout too. and looks like above example. deferences are: 1- i have PushButton and even CheckBox, ComboBox, QLineEdit, QSpinBox ( these are my 5 Classes) and for each of these calasses i create 4 objects in that loop. 2-then i used a QSignalMapper object for each these classes like: pushbutton_signalmapper, chekbox_signalmapper,..., spnbox_signalmapper . other thing are like the example.
    thanks for more help.

  8. #8
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    You probably need this:
    Qt Code:
    1. void QSignalMapper::setMapping(QObject * sender, QWidget * widget)
    To copy to clipboard, switch view to plain text mode 

    Pass checkbox, combobox etc. which are all QWidgets. Then in the slot you know which widget sent the signal..

  9. #9
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    @Vikram, Thanks but in this way i lost id number of each object for example by this, that you told, i can know sender is from Class1 but which object of this class send? by "void setMapping(QObject * sender, int id)" i can know object number and i can not know this object is from which class. now I could know which object (id) send and can not know this is from which class. One idea is I define 2 objects of QSignalMapper for each class (then i have 10 objects of QSignalMapper like:
    Qt Code:
    1. pushbutton_id_signalMapper->setMapping(QObject * sender, int id)
    2. pushbutton_type_signalMapper->setMapping(QObject * sender, QWidget * widget)
    3.  
    4. chekbox_id_signalMapper->setMapping(QObject * sender, int id)
    5. chekbox_type_signalMapper->setMapping(QObject * sender, QWidget * widget)
    6. .
    7. .
    8. .
    9. spnbox_id_signalMapper->setMapping(QObject * sender, int id)
    10. spnbox_type_signalMapper->setMapping(QObject * sender, QWidget * widget)
    To copy to clipboard, switch view to plain text mode 

    If I used QWidget * widget for identifying the class type, by what function I can know sender type? I mean when for example sender is PushButton then how can I know this:
    Qt Code:
    1. pushbutton_type_signalMapper->whatIsThisFunctionThatTellUsTheClassTypeToUseInsideSlot()//what function?
    To copy to clipboard, switch view to plain text mode 

    thanks for any help!!!

  10. #10
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Quote Originally Posted by Alex22 View Post
    i can know sender is from Class1 but which object of this class send?
    You get QWidget* as a parameter in the slot after mapping. you already know that pushButton, checkbox or lineEdit can send the signal. So:
    Qt Code:
    1. QPushButton *pushBtn = qobject_cast<QPushButton*>(widget);
    2. if(pushBth)
    3. {
    4. qDebug() << "QPushButton sent the signal " << pushBtn->text();
    5. // Do whatever you want.
    6. return;
    7. }
    8.  
    9. QLineEdit *lineEdit = qobject_cast<QLineEdit*>(widget);
    10. if(lineEdit)
    11. {
    12. qDebug() << "QLineEdit sent the signal " << lineEdit->text();
    13. // Do whatever you want.
    14. return;
    15. }
    16.  
    17. // Similarly for other widgets
    To copy to clipboard, switch view to plain text mode 

    PS: If you need id, name etc it doesn't make sense to create a signal mapper for each. Subclass pushbutton, define id in your custom button and then as explained in the code above cast QWidget* to your custom button and customButton->GetId() or some accessor you defined should give you id, name or anything else.

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

    Alex22 (11th December 2015)

  12. #11
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    @Vikram.Saralaya thanks so much!!!
    if I could set an id number for each object without using QSignalMapper, then I will solve my problem. How can I do this? For example there are 100 CheckBox and we want to give number id from 1000 up 1100, is there any setIdNum() function for this Widget and another widgets?

  13. #12
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Use properties with your buttons and checkboxes.
    bool QObject::setProperty(const char * name, const QVariant & value)
    Example:
    Qt Code:
    1. widget->SetProperty("id", 1001);
    To copy to clipboard, switch view to plain text mode 

    Retrieve the id in the slot:
    Qt Code:
    1. int id = widget->property("id").toInt();
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to Vikram.Saralaya for this useful post:

    Alex22 (11th December 2015)

  15. #13
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to know which signal is called a certain slot?

    Quote Originally Posted by Vikram.Saralaya View Post
    Use properties with your buttons and checkboxes.
    bool QObject::setProperty(const char * name, const QVariant & value)
    Example:
    Qt Code:
    1. widget->SetProperty("id", 1001);
    To copy to clipboard, switch view to plain text mode 

    Retrieve the id in the slot:
    Qt Code:
    1. int id = widget->property("id").toInt();
    To copy to clipboard, switch view to plain text mode 
    @Vikram.Saralaya, man this works so nice!!!
    thanks a lot for your great help

Similar Threads

  1. Replies: 3
    Last Post: 1st October 2015, 19:56
  2. Slot never called
    By Kola73 in forum Newbie
    Replies: 2
    Last Post: 27th June 2015, 08:48
  3. Replies: 2
    Last Post: 26th August 2011, 08:51
  4. Qt bug? Signal emitted once, slot called multiple times
    By MattPhillips in forum Qt Programming
    Replies: 22
    Last Post: 1st December 2010, 22:32
  5. SLOT not being called
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2007, 11:30

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.